diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b495291a..4d22bedc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ All notable changes to TEPP are documented here. The format follows Keep a Chang ## [Unreleased] +- `analysis_engine` jointly binds posterior-draw OLS loading means (`recover_loading_point_estimate_mean`) and Rubin (1996) total variance (`combine_draw_level_ols_loadings`) to the `rubin_loading_uncertainty_v1` analysis-run output profile. Observations unavailable at the request cutoff are excluded; the digest-bound `tepp.rubin_loading_uncertainty.v1` artifact records the point-estimate mean and Rubin `Q̄`/`Ū`/`B`/`T` and refuses Mislevy person-level plausible-value claims. This is not a new ESEM/DSEM estimator, not CWC, not a Driver p.16 `std` restore, and not persistence. - Removed the repository-local hourly PR-maintenance caller now covered by the central required scheduler, retired stale workflow registrations, narrowed documentation triggers, keyed PR concurrency by fixed workflow name, repository, and pull-request number without cancelling non-PR runs, and combined line/branch coverage on one sequential runner while preserving both 100% gates and diagnostics. - `event_core` adds bounded Allen interval-consistency classification, atomic path-consistency closure, contradiction/resource refusals, and an explicit dependency-error fallback without claiming unrestricted global satisfiability. diff --git a/Cargo.lock b/Cargo.lock index 454a7d612..28a0f0cb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,7 @@ dependencies = [ "corpus_split", "event_core", "membership_core", + "psychometric_core", "relation_graph", "serde", "serde_json", diff --git a/crates/analysis_engine/Cargo.toml b/crates/analysis_engine/Cargo.toml index 7322212b2..2ada0c3be 100644 --- a/crates/analysis_engine/Cargo.toml +++ b/crates/analysis_engine/Cargo.toml @@ -15,6 +15,7 @@ publish = false [dependencies] event_core = { path = "../event_core", version = "0.2.0" } +psychometric_core = { path = "../psychometric_core", version = "0.2.0" } serde = { workspace = true } serde_json = { workspace = true } sha2 = { workspace = true } diff --git a/crates/analysis_engine/src/lib.rs b/crates/analysis_engine/src/lib.rs index 72bd5854c..eb86f4564 100644 --- a/crates/analysis_engine/src/lib.rs +++ b/crates/analysis_engine/src/lib.rs @@ -8,13 +8,17 @@ //! through [`tepp_api`]. It deliberately does not claim latent-variable or topic //! estimation authority; those estimators remain separate scientific crates. //! estimation authority; it invokes estimators through their scientific crate -//! contracts and preserves their artifact meaning. +//! contracts and preserves their artifact meaning. Rubin loading uncertainty +//! is invoked through [`psychometric_core`] and is not Mislevy person-level +//! plausible-value pooling. mod case_deletion_refit; mod lineage_criterion; +mod rubin_loading_artifact; mod topic_context_posterior; mod topic_lineage_artifact; +use psychometric_core::PsychometricError; use serde::Serialize; use sha2::{Digest, Sha256}; use std::collections::BTreeSet; @@ -46,6 +50,13 @@ pub use lineage_criterion::{ LineageCriterionFit, LineageCriterionFitError, LineageCriterionObservation, fit_lineage_criterion_posteriors, }; +/// Rubin loading-uncertainty artifact and execution contracts. +pub use rubin_loading_artifact::{ + RUBIN_LOADING_ARTIFACT_BYTE_LIMIT, RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION, + RUBIN_LOADING_MODEL_CONTRACT_VERSION, RUBIN_LOADING_OUTPUT_PROFILE, RubinLoadingObservation, + RubinLoadingUncertaintyArtifact, RubinLoadingUncertaintyExecution, + execute_rubin_loading_uncertainty_run, +}; /// Bounded posterior topic-context producer contract and record types. pub use topic_context_posterior::{ TOPIC_CONTEXT_POSTERIOR_BYTE_LIMIT, TOPIC_CONTEXT_POSTERIOR_SCHEMA_VERSION, @@ -248,6 +259,10 @@ pub enum AnalysisEngineError { TopicMeasurement(TopicMeasurementError), /// A topic-lineage artifact violated its bounded schema or count invariants. InvalidTopicLineageArtifact, + /// A psychometric recovery rejected the offered coordinates. + Psychometric(PsychometricError), + /// A Rubin loading-uncertainty artifact violated its bounded schema. + InvalidRubinLoadingUncertaintyArtifact, } impl fmt::Display for AnalysisEngineError { @@ -262,6 +277,10 @@ impl fmt::Display for AnalysisEngineError { Self::LimitExceeded => "analysis corpus exceeded its execution bound", Self::TopicMeasurement(error) => return error.fmt(formatter), Self::InvalidTopicLineageArtifact => "invalid topic lineage artifact", + Self::Psychometric(error) => return error.fmt(formatter), + Self::InvalidRubinLoadingUncertaintyArtifact => { + "invalid Rubin loading-uncertainty artifact" + } }; formatter.write_str(message) } @@ -281,6 +300,12 @@ impl From for AnalysisEngineError { } } +impl From for AnalysisEngineError { + fn from(error: PsychometricError) -> Self { + Self::Psychometric(error) + } +} + /// Execute the cutoff-safe temporal evidence readiness analysis. /// /// Evidence whose `available_time` is later than the request cutoff is excluded @@ -415,6 +440,7 @@ mod tests { AnalysisEngineError, AnalysisEvidenceUnit, MAX_ANALYSIS_IDENTIFIER_BYTES, MAX_EVIDENCE_UNITS, TopicMeasurementError, add_membership_count, execute_analysis_run, }; + use psychometric_core::PsychometricError; use temporal_core::{AvailableTime, EventTime}; use tepp_api::{AnalysisRunAccepted, AnalysisRunRequest, AnalysisRunTerminalState, ApiError}; @@ -681,6 +707,14 @@ mod tests { AnalysisEngineError::InvalidTopicLineageArtifact, "invalid topic lineage artifact", ), + ( + AnalysisEngineError::Psychometric(PsychometricError::InsufficientDraws), + "Rubin total variance requires at least two complete-data draws", + ), + ( + AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact, + "invalid Rubin loading-uncertainty artifact", + ), ]; for (error, message) in messages { assert_eq!(error.to_string(), message); @@ -689,6 +723,11 @@ mod tests { assert_eq!(converted.to_string(), "invalid API wire payload"); let from_topic: AnalysisEngineError = TopicMeasurementError::DidNotConverge.into(); assert_eq!(from_topic.to_string(), "topic estimator did not converge"); + let from_psych: AnalysisEngineError = PsychometricError::InsufficientDraws.into(); + assert_eq!( + from_psych.to_string(), + "Rubin total variance requires at least two complete-data draws" + ); assert_eq!( add_membership_count(u64::MAX, 1), Err(AnalysisEngineError::ArithmeticOverflow) diff --git a/crates/analysis_engine/src/rubin_loading_artifact.rs b/crates/analysis_engine/src/rubin_loading_artifact.rs new file mode 100644 index 000000000..3133963b0 --- /dev/null +++ b/crates/analysis_engine/src/rubin_loading_artifact.rs @@ -0,0 +1,596 @@ +//! Digest-bound Rubin loading uncertainty as an analysis-run profile. + +use psychometric_core::{ + IndicatorKind, PsychometricError, combine_draw_level_ols_loadings, + recover_loading_point_estimate_mean, +}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use temporal_core::{AvailableTime, KnowledgeCutoff}; +use tepp_api::{ + AnalysisResultSummary, AnalysisRunAccepted, AnalysisRunRequest, AnalysisRunTerminalResult, +}; + +use crate::{ + AnalysisEngineError, MAX_EVIDENCE_UNITS, format_digest, require_receipt_identity, + valid_identifier, +}; + +/// Versioned schema for a completed Rubin loading-uncertainty artifact. +pub const RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION: &str = "tepp.rubin_loading_uncertainty.v1"; +/// Model contract required by the Rubin loading-uncertainty execution path. +pub const RUBIN_LOADING_MODEL_CONTRACT_VERSION: &str = "rubin_loading_uncertainty_v1"; +/// Analysis-run output profile required for a Rubin loading-uncertainty artifact. +pub const RUBIN_LOADING_OUTPUT_PROFILE: &str = "rubin_loading_uncertainty_v1"; +/// Maximum canonical artifact JSON size. +pub const RUBIN_LOADING_ARTIFACT_BYTE_LIMIT: usize = 256 * 1024; +const RUBIN_LOADING_MAX_DRAWS: usize = 256; +const RUBIN_LOADING_MAX_MATRIX_CELLS: usize = 1_000_000; +const RUBIN_LOADING_INFERENCE_STATUS: &str = "rubin_combined_ols_loadings_not_mislevy_pv"; +const RUBIN_LOADING_STATISTIC_COUNT: u64 = 5; + +/// One already-mapped factor score with complete-data indicator draws. +#[derive(Clone, Debug, PartialEq)] +pub struct RubinLoadingObservation { + snapshot_id: String, + factor_score: f64, + indicator_draws: Vec, + available_time: AvailableTime, +} + +impl RubinLoadingObservation { + /// Bind one factor score and its complete-data indicator draws to immutable + /// snapshot and availability provenance. + /// + /// # Errors + /// + /// Returns [`AnalysisEngineError::InvalidEvidence`] when the snapshot + /// identity or numeric input is invalid, and [`AnalysisEngineError::LimitExceeded`] + /// when one observation exceeds the application draw ceiling. + pub fn new( + snapshot_id: impl Into, + factor_score: f64, + indicator_draws: Vec, + available_time: AvailableTime, + ) -> Result { + let snapshot_id = snapshot_id.into(); + if !valid_identifier(&snapshot_id) + || !factor_score.is_finite() + || indicator_draws.is_empty() + || indicator_draws.iter().any(|value| !value.is_finite()) + { + return Err(AnalysisEngineError::InvalidEvidence); + } + if indicator_draws.len() > RUBIN_LOADING_MAX_DRAWS { + return Err(AnalysisEngineError::LimitExceeded); + } + Ok(Self { + snapshot_id, + factor_score, + indicator_draws, + available_time, + }) + } + + /// Return the immutable source snapshot identity. + #[must_use] + pub fn snapshot_id(&self) -> &str { + &self.snapshot_id + } + + /// Return the already-mapped factor score. + #[must_use] + pub const fn factor_score(&self) -> f64 { + self.factor_score + } + + /// Return the complete-data indicator draws in source order. + #[must_use] + pub fn indicator_draws(&self) -> &[f64] { + &self.indicator_draws + } + + /// Return the availability clock used for cutoff eligibility. + #[must_use] + pub const fn available_time(&self) -> AvailableTime { + self.available_time + } +} + +/// Completed, bounded Rubin loading-uncertainty result. +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +#[serde(deny_unknown_fields)] +pub struct RubinLoadingUncertaintyArtifact { + /// Exact versioned schema identity. + pub schema_version: String, + /// Opaque accepted-run identity. + pub run_id: String, + /// Immutable source snapshot identity. + pub snapshot_id: String, + /// Historical evidence cutoff used by the combination. + pub knowledge_cutoff: String, + /// Eligible observations after cutoff. + pub observation_count: u64, + /// Complete-data draws combined by Rubin `T`. + pub draw_count: u64, + /// Observations excluded because availability was after the cutoff. + pub excluded_after_cutoff_count: u64, + /// Admitted indicator-kind wire name. + pub indicator_kind: String, + /// Robust arithmetic mean of per-draw OLS loadings. Not Rubin `T`. + pub point_estimate_mean: f64, + /// Rubin mean complete-data loading `Q̄`. + pub mean_loading: f64, + /// Mean complete-data sampling variance `Ū`. + pub within_variance: f64, + /// Between-draw variance `B`. + pub between_variance: f64, + /// Total variance `T = Ū + (1 + 1/m) B`. + pub total_variance: f64, + /// Fixed claim boundary for consumer copy. + pub inference_status: String, +} + +fn require_artifact_byte_limit(payload_len: usize) -> Result<(), AnalysisEngineError> { + if payload_len > RUBIN_LOADING_ARTIFACT_BYTE_LIMIT { + return Err(AnalysisEngineError::LimitExceeded); + } + Ok(()) +} + +impl RubinLoadingUncertaintyArtifact { + /// Parse and fully validate a bounded artifact JSON payload. + /// + /// # Errors + /// + /// Returns [`AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact`] + /// when the schema, identifiers, counts, variances, or claim boundary fail. + pub fn from_json(payload: &str) -> Result { + require_artifact_byte_limit(payload.len())?; + let artifact: Self = serde_json::from_str(payload) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + artifact.validate()?; + Ok(artifact) + } + + /// Serialize canonical validated artifact JSON. + /// + /// # Errors + /// + /// Returns a typed validation, size, or serialization failure. + pub fn to_json(&self) -> Result { + self.validate()?; + let payload = + serde_json::to_string(self).map_err(|_| AnalysisEngineError::SerializationFailure)?; + require_artifact_byte_limit(payload.len())?; + Ok(payload) + } + + /// Return the lowercase SHA-256 digest of canonical artifact JSON. + /// + /// # Errors + /// + /// Returns a typed validation, size, or serialization failure. + pub fn sha256(&self) -> Result { + self.to_json() + .map(|json| format_digest(Sha256::digest(json.into_bytes()))) + } + + fn validate(&self) -> Result<(), AnalysisEngineError> { + let cutoff = KnowledgeCutoff::parse_rfc3339(&self.knowledge_cutoff) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let observation_count = usize::try_from(self.observation_count) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let draw_count = usize::try_from(self.draw_count) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let excluded_after_cutoff_count = usize::try_from(self.excluded_after_cutoff_count) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let total_observation_population = observation_count + .checked_add(excluded_after_cutoff_count) + .ok_or(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let matrix_cells = observation_count + .checked_mul(draw_count) + .ok_or(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + + if self.schema_version != RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION + || !valid_identifier(&self.run_id) + || !valid_identifier(&self.snapshot_id) + || cutoff.to_rfc3339() != self.knowledge_cutoff + || observation_count < 2 + || total_observation_population > MAX_EVIDENCE_UNITS + || !(2..=RUBIN_LOADING_MAX_DRAWS).contains(&draw_count) + || matrix_cells > RUBIN_LOADING_MAX_MATRIX_CELLS + || !admitted_indicator_kind(&self.indicator_kind) + || !self.point_estimate_mean.is_finite() + || !self.mean_loading.is_finite() + || !self.within_variance.is_finite() + || self.within_variance < 0.0 + || !self.between_variance.is_finite() + || self.between_variance < 0.0 + || !self.total_variance.is_finite() + || self.total_variance < 0.0 + || self.inference_status != RUBIN_LOADING_INFERENCE_STATUS + { + return Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact); + } + + let draw_count_u32 = u32::try_from(draw_count) + .map_err(|_| AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact)?; + let expected_total = self.within_variance + + (1.0 + 1.0 / f64::from(draw_count_u32)) * self.between_variance; + if !expected_total.is_finite() || expected_total != self.total_variance { + return Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact); + } + Ok(()) + } +} + +/// One completed Rubin loading-uncertainty artifact and terminal result. +#[derive(Clone, Debug, PartialEq)] +pub struct RubinLoadingUncertaintyExecution { + /// Digest-bound completed combination artifact. + pub artifact: RubinLoadingUncertaintyArtifact, + /// Terminal result carrying the artifact identity, digest, and schema. + pub terminal_result: AnalysisRunTerminalResult, +} + +struct EligibleRubinRows { + factor_scores: Vec, + indicator_draws: Vec>, + excluded_after_cutoff_count: u64, +} + +fn admitted_indicator_kind(label: &str) -> bool { + matches!(label, "alr" | "ilr" | "logistic_normal") +} + +fn admit_observations_at_cutoff( + observations: &[RubinLoadingObservation], + snapshot_id: &str, + knowledge_cutoff: KnowledgeCutoff, +) -> Result { + if observations.len() > MAX_EVIDENCE_UNITS { + return Err(AnalysisEngineError::LimitExceeded); + } + let mut eligible = Vec::new(); + let mut excluded_after_cutoff_count = 0_u64; + for observation in observations { + if observation.snapshot_id != snapshot_id { + return Err(AnalysisEngineError::InvalidEvidence); + } + if observation.available_time.instant() <= knowledge_cutoff.instant() { + eligible.push(observation); + } else { + excluded_after_cutoff_count = excluded_after_cutoff_count + .checked_add(1) + .ok_or(AnalysisEngineError::ArithmeticOverflow)?; + } + } + if eligible.is_empty() { + return Err(AnalysisEngineError::Psychometric( + PsychometricError::InvalidNumericInput, + )); + } + let draw_count = eligible[0].indicator_draws.len(); + if draw_count > RUBIN_LOADING_MAX_DRAWS { + return Err(AnalysisEngineError::LimitExceeded); + } + let matrix_cells = eligible + .len() + .checked_mul(draw_count) + .ok_or(AnalysisEngineError::ArithmeticOverflow)?; + if matrix_cells > RUBIN_LOADING_MAX_MATRIX_CELLS { + return Err(AnalysisEngineError::LimitExceeded); + } + + let mut factor_scores = Vec::with_capacity(eligible.len()); + let mut indicator_draws = vec![Vec::with_capacity(eligible.len()); draw_count]; + for observation in eligible { + if observation.indicator_draws.len() != draw_count { + return Err(AnalysisEngineError::Psychometric( + PsychometricError::InvalidNumericInput, + )); + } + factor_scores.push(observation.factor_score); + for (draw_index, value) in observation.indicator_draws.iter().enumerate() { + indicator_draws[draw_index].push(*value); + } + } + #[rustfmt::skip] + let rows = EligibleRubinRows { factor_scores, indicator_draws, excluded_after_cutoff_count }; + Ok(rows) +} + +/// Execute cutoff-safe Rubin loading uncertainty as one analysis-run profile. +/// +/// The caller supplies already-mapped factor scores and complete-data indicator +/// draws. The robust point-estimate helper and Rubin combination are invoked as +/// separate protected scientific contracts because their accumulation policies +/// are intentionally not interchangeable. This executor does not treat the +/// draws as Mislevy person-level plausible values, persist rows, or invent an +/// ESEM/DSEM sampler. +/// +/// # Errors +/// +/// Returns a request/receipt/snapshot/cutoff/profile error, resource admission +/// refusal, psychometric recovery failure, or invalid artifact error. +#[rustfmt::skip] +pub fn execute_rubin_loading_uncertainty_run(request: &AnalysisRunRequest, accepted: &AnalysisRunAccepted, snapshot_id: &str, knowledge_cutoff: KnowledgeCutoff, kind: IndicatorKind, observations: &[RubinLoadingObservation], completed_at: impl Into) -> Result { + request.to_json()?; + accepted.to_json()?; + require_receipt_identity(request, accepted)?; + if request.snapshot_id != snapshot_id { + return Err(AnalysisEngineError::SnapshotMismatch); + } + if !valid_identifier(snapshot_id) { + return Err(AnalysisEngineError::InvalidEvidence); + } + let request_cutoff = KnowledgeCutoff::parse_rfc3339(&request.knowledge_cutoff) + .map_err(|_| AnalysisEngineError::InvalidEvidence)?; + if request_cutoff.instant() != knowledge_cutoff.instant() + || request.model_contract_version != RUBIN_LOADING_MODEL_CONTRACT_VERSION + || request.output_profile != RUBIN_LOADING_OUTPUT_PROFILE + { + return Err(AnalysisEngineError::InvalidEvidence); + } + + let eligible = admit_observations_at_cutoff(observations, snapshot_id, knowledge_cutoff)?; + let point_estimate_mean = recover_loading_point_estimate_mean( + &eligible.factor_scores, + &eligible.indicator_draws, + kind, + )?; + let combined = + combine_draw_level_ols_loadings(&eligible.factor_scores, &eligible.indicator_draws, kind)?; + let observation_count = u64::try_from(eligible.factor_scores.len()) + .map_err(|_| AnalysisEngineError::ArithmeticOverflow)?; + let draw_count = u64::try_from(combined.draw_count) + .map_err(|_| AnalysisEngineError::ArithmeticOverflow)?; + #[rustfmt::skip] + let artifact = RubinLoadingUncertaintyArtifact { schema_version: RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION.into(), run_id: accepted.run_id.clone(), snapshot_id: snapshot_id.to_owned(), knowledge_cutoff: knowledge_cutoff.to_rfc3339(), observation_count, draw_count, excluded_after_cutoff_count: eligible.excluded_after_cutoff_count, indicator_kind: kind.as_str().to_owned(), point_estimate_mean, mean_loading: combined.mean_loading, within_variance: combined.within_variance, between_variance: combined.between_variance, total_variance: combined.total_variance, inference_status: RUBIN_LOADING_INFERENCE_STATUS.into() }; + let digest = artifact.sha256()?; + let summary = AnalysisResultSummary::new( + "rubin_loading_uncertainty", + observation_count, + RUBIN_LOADING_STATISTIC_COUNT, + "validated", + )?; + let artifact_id = format!("rubin_loading_uncertainty_artifact_{}", &digest[..16]); + let terminal_result = AnalysisRunTerminalResult::succeeded( + request, + accepted, + artifact_id, + digest, + RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION, + completed_at, + summary, + )?; + Ok(RubinLoadingUncertaintyExecution { + artifact, + terminal_result, + }) +} + +#[cfg(test)] +mod tests { + use super::{ + RUBIN_LOADING_ARTIFACT_BYTE_LIMIT, RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION, + RUBIN_LOADING_INFERENCE_STATUS, RUBIN_LOADING_MAX_DRAWS, RUBIN_LOADING_MAX_MATRIX_CELLS, + RubinLoadingUncertaintyArtifact, require_artifact_byte_limit, + }; + use crate::{AnalysisEngineError, MAX_EVIDENCE_UNITS}; + + fn artifact() -> RubinLoadingUncertaintyArtifact { + RubinLoadingUncertaintyArtifact { + schema_version: RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION.into(), + run_id: "run-1".into(), + snapshot_id: "snapshot-1".into(), + knowledge_cutoff: "2026-08-01T00:00:00Z".into(), + observation_count: 3, + draw_count: 2, + excluded_after_cutoff_count: 0, + indicator_kind: "alr".into(), + point_estimate_mean: 0.8, + mean_loading: 0.8, + within_variance: 0.0, + between_variance: 0.02, + total_variance: 0.03, + inference_status: RUBIN_LOADING_INFERENCE_STATUS.into(), + } + } + + fn assert_invalid(artifact: &RubinLoadingUncertaintyArtifact) { + assert_eq!( + artifact.to_json(), + Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact) + ); + } + + #[test] + fn artifact_round_trip_and_size_bounds_fail_closed() { + let artifact = artifact(); + let payload = artifact.to_json().expect("json"); + assert_eq!( + RubinLoadingUncertaintyArtifact::from_json(&payload), + Ok(artifact.clone()) + ); + assert_eq!(artifact.sha256().expect("digest").len(), 64); + assert_eq!( + RubinLoadingUncertaintyArtifact::from_json("{}"), + Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact) + ); + assert_eq!( + RubinLoadingUncertaintyArtifact::from_json( + &"x".repeat(RUBIN_LOADING_ARTIFACT_BYTE_LIMIT + 1) + ), + Err(AnalysisEngineError::LimitExceeded) + ); + assert_eq!( + require_artifact_byte_limit(RUBIN_LOADING_ARTIFACT_BYTE_LIMIT), + Ok(()) + ); + assert_eq!( + require_artifact_byte_limit(RUBIN_LOADING_ARTIFACT_BYTE_LIMIT + 1), + Err(AnalysisEngineError::LimitExceeded) + ); + } + + #[test] + fn maximal_valid_artifact_stays_inside_the_wire_envelope() { + let mut artifact = artifact(); + artifact.run_id = "\\".repeat(256); + artifact.snapshot_id = "\\".repeat(256); + artifact.draw_count = u64::try_from(RUBIN_LOADING_MAX_DRAWS).expect("draw bound"); + artifact.observation_count = + u64::try_from(RUBIN_LOADING_MAX_MATRIX_CELLS / RUBIN_LOADING_MAX_DRAWS) + .expect("observation bound"); + artifact.excluded_after_cutoff_count = u64::try_from(MAX_EVIDENCE_UNITS) + .expect("population bound") + - artifact.observation_count; + artifact.point_estimate_mean = f64::MAX; + artifact.mean_loading = -f64::MAX; + artifact.within_variance = 0.0; + artifact.between_variance = 0.0; + artifact.total_variance = 0.0; + let payload = artifact.to_json().expect("maximal valid json"); + assert!(payload.len() < RUBIN_LOADING_ARTIFACT_BYTE_LIMIT); + assert_eq!( + RubinLoadingUncertaintyArtifact::from_json(&payload), + Ok(artifact) + ); + } + + #[test] + fn artifact_metadata_tampering_fails_closed() { + let artifact = artifact(); + let invalid_artifacts = [ + { + let mut value = artifact.clone(); + value.schema_version.clear(); + value + }, + { + let mut value = artifact.clone(); + value.run_id.clear(); + value + }, + { + let mut value = artifact.clone(); + value.snapshot_id.clear(); + value + }, + { + let mut value = artifact.clone(); + value.knowledge_cutoff = "invalid".into(); + value + }, + { + let mut value = artifact.clone(); + value.knowledge_cutoff = "2026-08-01T01:00:00+01:00".into(); + value + }, + { + let mut value = artifact.clone(); + value.observation_count = 1; + value + }, + { + let mut value = artifact.clone(); + value.observation_count = u64::try_from(MAX_EVIDENCE_UNITS).expect("bound") + 1; + value + }, + { + let mut value = artifact.clone(); + value.excluded_after_cutoff_count = u64::MAX; + value + }, + { + let mut value = artifact.clone(); + value.draw_count = 1; + value + }, + { + let mut value = artifact.clone(); + value.draw_count = u64::try_from(RUBIN_LOADING_MAX_DRAWS).expect("bound") + 1; + value + }, + { + let mut value = artifact.clone(); + value.observation_count = + u64::try_from(RUBIN_LOADING_MAX_MATRIX_CELLS / RUBIN_LOADING_MAX_DRAWS + 1) + .expect("bound"); + value.draw_count = u64::try_from(RUBIN_LOADING_MAX_DRAWS).expect("bound"); + value + }, + { + let mut value = artifact.clone(); + value.indicator_kind = "raw_proportion".into(); + value + }, + { + let mut value = artifact.clone(); + value.point_estimate_mean = f64::NAN; + value + }, + { + let mut value = artifact.clone(); + value.mean_loading = f64::INFINITY; + value + }, + { + let mut value = artifact.clone(); + value.within_variance = -0.1; + value + }, + { + let mut value = artifact.clone(); + value.within_variance = f64::NAN; + value + }, + { + let mut value = artifact.clone(); + value.between_variance = f64::NEG_INFINITY; + value + }, + { + let mut value = artifact.clone(); + value.between_variance = -0.1; + value + }, + { + let mut value = artifact.clone(); + value.total_variance = f64::NAN; + value + }, + { + let mut value = artifact.clone(); + value.total_variance = -0.1; + value + }, + { + let mut value = artifact.clone(); + value.total_variance = 0.04; + value + }, + { + let mut value = artifact.clone(); + value.inference_status.clear(); + value + }, + ]; + for invalid in invalid_artifacts { + assert_invalid(&invalid); + } + } + + #[test] + fn from_json_rejects_finite_but_inconsistent_rubin_components() { + let mut artifact = artifact(); + artifact.total_variance = 0.04; + let payload = serde_json::to_string(&artifact).expect("unchecked json"); + assert_eq!( + RubinLoadingUncertaintyArtifact::from_json(&payload), + Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact) + ); + } +} diff --git a/crates/analysis_engine/tests/rubin_loading_execution_contract.rs b/crates/analysis_engine/tests/rubin_loading_execution_contract.rs new file mode 100644 index 000000000..e1b75ac79 --- /dev/null +++ b/crates/analysis_engine/tests/rubin_loading_execution_contract.rs @@ -0,0 +1,549 @@ +//! End-to-end contract for cutoff-safe Rubin loading uncertainty. + +use analysis_engine::{ + AnalysisEngineError, MAX_EVIDENCE_UNITS, RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION, + RUBIN_LOADING_MODEL_CONTRACT_VERSION, RUBIN_LOADING_OUTPUT_PROFILE, RubinLoadingObservation, + RubinLoadingUncertaintyArtifact, execute_rubin_loading_uncertainty_run, +}; +use psychometric_core::{IndicatorKind, PsychometricError}; +use temporal_core::{AvailableTime, KnowledgeCutoff}; +use tepp_api::{AnalysisRunAccepted, AnalysisRunRequest, AnalysisRunTerminalState}; + +const SNAPSHOT_ID: &str = "snapshot-rubin-loading"; + +fn available(stamp: &str) -> AvailableTime { + AvailableTime::parse_rfc3339(stamp).expect("available") +} + +fn cutoff() -> KnowledgeCutoff { + KnowledgeCutoff::parse_rfc3339("2026-08-01T00:00:00Z").expect("cutoff") +} + +fn observation( + snapshot_id: &str, + factor_score: f64, + indicator_draws: Vec, + available_at: &str, +) -> RubinLoadingObservation { + RubinLoadingObservation::new( + snapshot_id, + factor_score, + indicator_draws, + available(available_at), + ) + .expect("observation") +} + +fn noiseless_rows() -> Vec { + vec![ + observation(SNAPSHOT_ID, -1.0, vec![-0.7, -0.9], "2026-07-01T00:00:00Z"), + observation(SNAPSHOT_ID, 0.0, vec![0.0, 0.0], "2026-07-01T00:00:00Z"), + observation(SNAPSHOT_ID, 1.0, vec![0.7, 0.9], "2026-07-01T00:00:00Z"), + ] +} + +fn request() -> AnalysisRunRequest { + AnalysisRunRequest { + contract_version: 1, + idempotency_key: "rubin-loading-idem".into(), + tenant_workspace_id: "tenant-workspace".into(), + snapshot_id: SNAPSHOT_ID.into(), + knowledge_cutoff: "2026-08-01T00:00:00Z".into(), + model_contract_version: RUBIN_LOADING_MODEL_CONTRACT_VERSION.into(), + output_profile: RUBIN_LOADING_OUTPUT_PROFILE.into(), + } +} + +fn accepted(request: &AnalysisRunRequest) -> AnalysisRunAccepted { + AnalysisRunAccepted::new("run-rubin-loading", "accepted", &request.idempotency_key) + .expect("accepted") +} + +fn execute( + request: &AnalysisRunRequest, + accepted: &AnalysisRunAccepted, + snapshot_id: &str, + knowledge_cutoff: KnowledgeCutoff, + kind: IndicatorKind, + observations: &[RubinLoadingObservation], +) -> Result { + execute_rubin_loading_uncertainty_run( + request, + accepted, + snapshot_id, + knowledge_cutoff, + kind, + observations, + "2026-08-02T00:00:00Z", + ) +} + +#[test] +fn noiseless_draws_emit_digest_bound_point_mean_and_rubin_t() { + let request = request(); + let accepted = accepted(&request); + let rows = noiseless_rows(); + let execution = execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ) + .expect("execution"); + + assert_eq!( + execution.artifact.schema_version, + RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION + ); + assert_eq!(execution.artifact.observation_count, 3); + assert_eq!(execution.artifact.draw_count, 2); + assert_eq!(execution.artifact.excluded_after_cutoff_count, 0); + assert_eq!(execution.artifact.indicator_kind, "alr"); + assert!((execution.artifact.point_estimate_mean - 0.8).abs() < 1e-12); + assert!((execution.artifact.mean_loading - 0.8).abs() < 1e-12); + assert!(execution.artifact.within_variance.abs() < 1e-12); + assert!(execution.artifact.between_variance > 0.0); + let expected_total = execution.artifact.within_variance + + (1.0 + 1.0 / 2.0) * execution.artifact.between_variance; + assert!((execution.artifact.total_variance - expected_total).abs() < 1e-15); + assert_eq!( + execution.artifact.inference_status, + "rubin_combined_ols_loadings_not_mislevy_pv" + ); + assert_eq!( + execution.terminal_result.run_state, + AnalysisRunTerminalState::Succeeded + ); + assert_eq!( + execution + .terminal_result + .summary + .as_ref() + .expect("summary") + .validation_status, + "validated" + ); + assert_eq!( + execution.terminal_result.result_sha256.as_deref(), + Some(execution.artifact.sha256().expect("digest").as_str()) + ); + assert_eq!( + execution.terminal_result.result_schema_version.as_deref(), + Some(RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION) + ); + assert_eq!(rows[0].snapshot_id(), SNAPSHOT_ID); + assert!((rows[0].factor_score() + 1.0).abs() < f64::EPSILON); + assert_eq!(rows[0].indicator_draws(), &[-0.7, -0.9]); + assert_eq!(rows[0].available_time(), available("2026-07-01T00:00:00Z")); +} + +#[test] +fn future_unavailable_rows_do_not_change_historical_replay() { + let request = request(); + let accepted = accepted(&request); + let baseline = execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &noiseless_rows(), + ) + .expect("baseline"); + + let mut rows = noiseless_rows(); + rows.push(observation( + SNAPSHOT_ID, + 2.0, + vec![10.0, 10.0, 10.0], + "2026-08-15T00:00:00Z", + )); + let replay = execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ) + .expect("historical replay"); + + assert_eq!(replay.artifact.observation_count, baseline.artifact.observation_count); + assert_eq!(replay.artifact.excluded_after_cutoff_count, 1); + assert_eq!(replay.artifact.point_estimate_mean, baseline.artifact.point_estimate_mean); + assert_eq!(replay.artifact.mean_loading, baseline.artifact.mean_loading); + assert_eq!(replay.artifact.total_variance, baseline.artifact.total_variance); +} + +#[test] +fn cross_snapshot_rows_fail_before_scientific_composition() { + let request = request(); + let accepted = accepted(&request); + let mut rows = noiseless_rows(); + rows.push(observation( + "other-snapshot", + 2.0, + vec![10.0, 10.0, 10.0], + "2026-08-15T00:00:00Z", + )); + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ), + Err(AnalysisEngineError::InvalidEvidence) + ); +} + +#[test] +fn equivalent_rfc3339_cutoff_instants_bind_identically() { + let mut request = request(); + request.knowledge_cutoff = "2026-08-01T01:00:00+01:00".into(); + let accepted = accepted(&request); + let execution = execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &noiseless_rows(), + ) + .expect("same instant"); + assert_eq!(execution.artifact.knowledge_cutoff, "2026-08-01T00:00:00Z"); +} + +#[test] +fn robust_point_estimate_is_not_replaced_by_naive_rubin_mean() { + let request = request(); + let accepted = accepted(&request); + let rows = vec![ + observation( + SNAPSHOT_ID, + -1.0, + vec![-1.0e16, -1.0, 1.0e16], + "2026-07-01T00:00:00Z", + ), + observation( + SNAPSHOT_ID, + 0.0, + vec![0.0, 0.0, 0.0], + "2026-07-01T00:00:00Z", + ), + observation( + SNAPSHOT_ID, + 1.0, + vec![1.0e16, 1.0, -1.0e16], + "2026-07-01T00:00:00Z", + ), + ]; + let execution = execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ) + .expect("execution"); + assert_eq!(execution.artifact.mean_loading, 0.0); + assert!(execution.artifact.point_estimate_mean.abs() > 0.1); + assert_ne!( + execution.artifact.point_estimate_mean.to_bits(), + execution.artifact.mean_loading.to_bits() + ); +} + +#[test] +fn artifact_refuses_inconsistent_rubin_total_and_unreachable_counts() { + let artifact = RubinLoadingUncertaintyArtifact { + schema_version: RUBIN_LOADING_ARTIFACT_SCHEMA_VERSION.into(), + run_id: "run-rubin-loading".into(), + snapshot_id: SNAPSHOT_ID.into(), + knowledge_cutoff: "2026-08-01T00:00:00Z".into(), + observation_count: 3, + draw_count: 2, + excluded_after_cutoff_count: 0, + indicator_kind: "alr".into(), + point_estimate_mean: 0.8, + mean_loading: 0.8, + within_variance: 0.0, + between_variance: 0.02, + total_variance: 0.04, + inference_status: "rubin_combined_ols_loadings_not_mislevy_pv".into(), + }; + assert_eq!( + artifact.to_json(), + Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact) + ); + + let mut oversized = artifact; + oversized.total_variance = 0.03; + oversized.observation_count = u64::try_from(MAX_EVIDENCE_UNITS).expect("bound") + 1; + assert_eq!( + oversized.to_json(), + Err(AnalysisEngineError::InvalidRubinLoadingUncertaintyArtifact) + ); +} + +#[test] +fn execution_refuses_snapshot_profile_and_cutoff_mismatch() { + let request = request(); + let accepted = accepted(&request); + let rows = noiseless_rows(); + assert_eq!( + execute( + &request, + &accepted, + "other-snapshot", + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ), + Err(AnalysisEngineError::SnapshotMismatch) + ); + for invalid_request in [ + { + let mut value = request.clone(); + value.knowledge_cutoff = "2026-08-02T00:00:00Z".into(); + value + }, + { + let mut value = request.clone(); + value.model_contract_version = "other-model".into(); + value + }, + { + let mut value = request.clone(); + value.output_profile = "other-profile".into(); + value + }, + ] { + assert_eq!( + execute( + &invalid_request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ), + Err(AnalysisEngineError::InvalidEvidence) + ); + } +} + +#[test] +fn constructor_and_empty_cutoff_fail_closed() { + let request = request(); + let accepted = accepted(&request); + assert_eq!( + RubinLoadingObservation::new( + "", + 1.0, + vec![1.0, 2.0], + available("2026-07-01T00:00:00Z"), + ), + Err(AnalysisEngineError::InvalidEvidence) + ); + assert_eq!( + RubinLoadingObservation::new( + SNAPSHOT_ID, + f64::NAN, + vec![1.0, 2.0], + available("2026-07-01T00:00:00Z"), + ), + Err(AnalysisEngineError::InvalidEvidence) + ); + assert_eq!( + RubinLoadingObservation::new( + SNAPSHOT_ID, + 1.0, + vec![], + available("2026-07-01T00:00:00Z"), + ), + Err(AnalysisEngineError::InvalidEvidence) + ); + assert_eq!( + RubinLoadingObservation::new( + SNAPSHOT_ID, + 1.0, + vec![1.0, f64::NAN], + available("2026-07-01T00:00:00Z"), + ), + Err(AnalysisEngineError::InvalidEvidence) + ); + assert_eq!( + RubinLoadingObservation::new( + SNAPSHOT_ID, + 1.0, + vec![1.0; 257], + available("2026-07-01T00:00:00Z"), + ), + Err(AnalysisEngineError::LimitExceeded) + ); + + let mut early_request = request.clone(); + early_request.knowledge_cutoff = "2026-06-01T00:00:00Z".into(); + let too_early = KnowledgeCutoff::parse_rfc3339("2026-06-01T00:00:00Z").expect("cutoff"); + assert_eq!( + execute( + &early_request, + &accepted, + SNAPSHOT_ID, + too_early, + IndicatorKind::AdditiveLogRatio, + &noiseless_rows(), + ), + Err(AnalysisEngineError::Psychometric( + PsychometricError::InvalidNumericInput + )) + ); +} + +#[test] +fn execution_refuses_raw_proportion_single_draw_and_unequal_lengths() { + let request = request(); + let accepted = accepted(&request); + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::RawProportion, + &noiseless_rows(), + ), + Err(AnalysisEngineError::Psychometric( + PsychometricError::RawProportionForbidden + )) + ); + + let single_draw = vec![ + observation(SNAPSHOT_ID, -1.0, vec![-0.7], "2026-07-01T00:00:00Z"), + observation(SNAPSHOT_ID, 1.0, vec![0.7], "2026-07-01T00:00:00Z"), + ]; + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &single_draw, + ), + Err(AnalysisEngineError::Psychometric( + PsychometricError::InsufficientDraws + )) + ); + + let unequal = vec![ + observation(SNAPSHOT_ID, -1.0, vec![-0.7, -0.9], "2026-07-01T00:00:00Z"), + observation(SNAPSHOT_ID, 1.0, vec![0.7, 0.9, 1.1], "2026-07-01T00:00:00Z"), + ]; + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &unequal, + ), + Err(AnalysisEngineError::Psychometric( + PsychometricError::InvalidNumericInput + )) + ); +} + +#[test] +fn matrix_resource_budget_fails_before_scientific_fit() { + let request = request(); + let accepted = accepted(&request); + let rows = vec![ + observation( + SNAPSHOT_ID, + 1.0, + vec![1.0; 256], + "2026-07-01T00:00:00Z", + ); + 3_907 + ]; + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &rows, + ), + Err(AnalysisEngineError::LimitExceeded) + ); +} + +#[test] +fn execution_refuses_receipt_mismatch_and_oversized_corpus() { + let request = request(); + let accepted = accepted(&request); + let wrong_receipt = + AnalysisRunAccepted::new("run-rubin-loading", "accepted", "other-key").expect("accepted"); + assert_eq!( + execute( + &request, + &wrong_receipt, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &noiseless_rows(), + ) + .expect_err("receipt"), + AnalysisEngineError::Api(tepp_api::ApiError::InvalidWirePayload) + ); + + let oversized = vec![ + observation( + SNAPSHOT_ID, + 1.0, + vec![1.0, 2.0], + "2026-07-01T00:00:00Z", + ); + MAX_EVIDENCE_UNITS + 1 + ]; + assert_eq!( + execute( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &oversized, + ), + Err(AnalysisEngineError::LimitExceeded) + ); +} + +#[test] +fn execution_refuses_invalid_completion_time() { + let request = request(); + let accepted = accepted(&request); + assert_eq!( + execute_rubin_loading_uncertainty_run( + &request, + &accepted, + SNAPSHOT_ID, + cutoff(), + IndicatorKind::AdditiveLogRatio, + &noiseless_rows(), + "invalid", + ), + Err(AnalysisEngineError::Api( + tepp_api::ApiError::InvalidWirePayload + )) + ); +} diff --git a/crates/psychometric_core/src/error.rs b/crates/psychometric_core/src/error.rs index 4ab2695e0..9c06bbe83 100644 --- a/crates/psychometric_core/src/error.rs +++ b/crates/psychometric_core/src/error.rs @@ -1384,6 +1384,25 @@ mod tests { PsychometricError::InitialObservedMeanIsNotEvolvedObservedMean.to_string(), "first-occasion observed mean is not the evolved observed mean" ); + assert_eq!( + PsychometricError::StandardisedManifestVarianceRequiresPositiveManifestVariance + .to_string(), + "standardised measurement-error variance requires strictly positive measurement-error variance" + ); + assert_eq!( + PsychometricError::UnstandardisedManifestVarianceIsNotStandardisedManifestVariance + .to_string(), + "unstandardised measurement-error variance is not standardised measurement-error variance" + ); + assert_eq!( + PsychometricError::StandardisedManifestTraitVarianceIsNotStandardisedManifestVariance + .to_string(), + "standardised manifest-trait variance is not standardised measurement-error variance" + ); + assert_eq!( + PsychometricError::ObservedVarianceIsNotStandardisedManifestVariance.to_string(), + "observed-indicator variance is not standardised measurement-error variance" + ); } #[test] diff --git a/docs/TRACEABILITY.md b/docs/TRACEABILITY.md index 2b783c2ab..6502e9e82 100644 --- a/docs/TRACEABILITY.md +++ b/docs/TRACEABILITY.md @@ -58,6 +58,7 @@ The full APA 7th standards/literature register remains `docs/research/standards- | versioned service/API contracts and exports | PRD; API contract; ADR 0011/0013 | `tepp_api` analysis-run/export/JSON-LD/GraphML contracts on protected main (PR #21); HTTP service remaining accepted-target | partial | | versioned service/API contracts and exports | PRD; API contract; ADR 0011/0013 | `tepp_api` analysis-run/export/JSON-LD/GraphML contracts on protected main (PR #21); LineageWeave loopback contracts and request-bound terminal result are composed on the active product branch; production TLS remaining | partial | | executable cutoff-safe analysis runs | ADR 0012/0022; temporal research; API terminal-result contract | `analysis_engine` availability cutoff, snapshot binding, multiple-membership aggregation, digest-bound readiness artifact, and `tepp.trsl_topic_lineage.v1` execution through `topic_measurement`; synthetic recovery plus tamper/non-convergence tests and exact coverage on the active product branch | active-PR | +| cutoff-safe Rubin loading uncertainty | ADR 0005/0034; Rubin (1996) | `analysis_engine` `rubin_loading_uncertainty_v1` jointly binds `psychometric_core` draw-mean OLS loadings and Rubin `T` to a digest-bound `tepp.rubin_loading_uncertainty.v1` artifact; not Mislevy person-level plausible values, not implemented-main | active-PR | | immutable split/run/reproducibility manifests | ADR 0013; ERD | `tepp_api` reproducibility manifest contract on protected main; `persistence_postgres` append-only SQL insert/lookup for `reproducibility_manifest`, `corpus_split_manifest`, `model_run`, and `model_artifact` (migration `0003`); full physical ERD constraints remaining | partial | | multilingual shared latent semantic space | PRD; ADR 0004; ADR 0020 | `semantic_core` span-grounded units (active-PR); concept dictionary and shared latent estimator remaining | active-PR | | TRSL-TM temporal/relational topic posterior and backend compatibility | ADR 0012; ADR 0004 | `topic_measurement` stable ALR/ILR coordinates and bounded CPU `f64` reference estimator on protected main; `model_selection` fitted candidate-`K` scoring on this PR; calibrated posterior promotion, method effects, persistence, and accelerated backends remaining | partial | diff --git a/docs/adr/0034-rubin-loading-uncertainty-analysis-run.md b/docs/adr/0034-rubin-loading-uncertainty-analysis-run.md new file mode 100644 index 000000000..ae51dcc81 --- /dev/null +++ b/docs/adr/0034-rubin-loading-uncertainty-analysis-run.md @@ -0,0 +1,137 @@ +# ADR 0034 — Rubin loading uncertainty as an analysis-run output profile + +**Decision status:** Proposed +**Implementation maturity:** active-PR — composed on this branch; not implemented-main +**Date:** 2026-08-31 +**Last reviewed:** 2026-09-14 +**Supersedes:** None; complements ADR 0005 (ESEM/DSEM interpretation) and ADR 0022 (cutoff-safe analysis-run execution). +**Figma File ID:** N/A — this increment changes a Rust service crate and has no user-interface surface. +**Storybook inventory:** N/A — no reusable web object or interaction changed. + +## Context + +Protected main owns two deliberately different `psychometric_core` contracts: +`recover_loading_point_estimate_mean` computes a scaled, compensated mean of +posterior-draw OLS loading point estimates, while +`combine_draw_level_ols_loadings` computes Rubin (1996) `Q̄`, `Ū`, `B`, and +`T = Ū + (1 + 1/m)B` for complete-data OLS loadings. The latter's ordinary +loading accumulation is not a substitute for the former's robust point +estimate under large cancellation. + +Operators still cannot request the joint result as a historical, digest-bound +Analysis Run output. The original branch also left several application +contracts weaker than the profile name implied: row availability lacked an +immutable snapshot identity, request/executor cutoffs were compared as RFC +3339 text, draw/matrix materialization was unbounded, artifact counts could +claim unreachable executions, serialized `T` was not checked against its +components, and terminal provider validation reused the scientific inference +label. + +## Decision + +Add `rubin_loading_uncertainty_v1` to `analysis_engine` as an application +composition over the protected-main scientific owners. The executor: + +- requires every `RubinLoadingObservation` to carry the requested immutable + `snapshot_id` and typed `AvailableTime`; +- rejects cross-snapshot observations and excludes same-snapshot observations + with `AvailableTime > KnowledgeCutoff` before matrix/scientific admission; +- parses request cutoffs and compares temporal instants, while persisted + artifacts retain one canonical RFC 3339 cutoff representation; +- invokes `recover_loading_point_estimate_mean` for the robust point estimate + and `combine_draw_level_ols_loadings` independently for Rubin `Q̄/Ū/B/T`; +- limits the current application representation to at most 256 complete-data + draws and 1,000,000 admitted observation-by-draw cells before transposition. + These are resource envelopes, not psychometric validity recommendations; +- bounds total raw observation population with `MAX_EVIDENCE_UNITS` and makes + imported artifact counts obey the same reachable envelope; +- validates imported `T` by recomputing the exact binary64 expression used by + the scientific owner. Canonical JSON round-tripping preserves the component + values, so exact equality is the chosen wire-integrity policy rather than a + tolerance that could admit a different scientific result; +- applies the 256 KiB artifact envelope to both untrusted `from_json` and + canonical `to_json`, with a maximal-valid escaping proof for the output + direction; +- propagates artifact/digest errors instead of asserting that accepted request + identifiers make serialization infallible; +- emits terminal `AnalysisResultSummary.validation_status = "validated"` and + keeps `rubin_combined_ols_loadings_not_mislevy_pv` solely as the artifact's + scientific inference boundary. + +This remains draw-level OLS combination. It is not person-level plausible-value +pooling, an ESEM/DSEM sampler, CWC, persistence, or a causal estimator. + +## Historical replay invariant + +For a fixed requested snapshot and knowledge cutoff, adding evidence that only +becomes available after that cutoff must not change the earlier admitted +factor-score/draw matrix or its scientific result. Such rows may change only +the excluded-after-cutoff count. A row from another immutable snapshot is not +historical censoring; it is a provenance violation and fails closed even when +its availability is later than the cutoff. + +## Alternatives considered + +1. Use `combine_draw_level_ols_loadings.mean_loading` for both fields — rejected + because it bypasses the protected robust point-estimate contract and can + differ under large cancellation. +2. Compare cutoff strings — rejected because legal RFC 3339 representations of + one instant must not alter historical execution. +3. Drop late rows without snapshot provenance — rejected because unrelated + snapshot data could be silently attributed to the requested run. +4. Accept any finite nonnegative serialized `T` — rejected because a digest- + bound uncertainty artifact must be internally consistent with its own + components and draw count. +5. Leave draws unbounded and rely on `MAX_EVIDENCE_UNITS` — rejected because + matrix materialization is a separate multiplicative resource dimension. +6. Put Rubin arithmetic into `analysis_engine` — rejected because reusable + scientific arithmetic remains `psychometric_core`-owned. + +## Scientific acceptance boundary + +Known-truth/noiseless fixtures and edge contracts are regression evidence, not +commercial scientific acceptance. Issue #503 owns repeated true-loading +recovery, bias/RMSE with Monte Carlo uncertainty, an explicitly justified +interval construction and empirical coverage, attempted/recovered/failed +denominators, design sensitivity, and leakage-safe historical evaluation. +Neither LLM judgment nor synthetic unit fixtures may close that gap. + +Primary authority for the current combining rule remains: + +Rubin, D. B. (1996). Multiple imputation after 18+ years. *Journal of the +American Statistical Association, 91*(434), 473–489. +https://doi.org/10.1080/01621459.1996.10476908 + +Repository research authority is `docs/research/rubin-total-variance.md`. + +## Consequences + +The profile has a narrower, auditable temporal and resource boundary, and its +artifact can no longer claim a Rubin total inconsistent with its serialized +components. Consumers can distinguish provider validation from the scientific +claim boundary and can distinguish the robust point estimate from Rubin `Q̄`. +The profile remains Draft/Proposed and not implemented-main while #503 and the +normal exact-head merge gates remain unresolved. + +## Verification + +Required exact-head verification includes: + +```text +cargo fmt --all -- --check +cargo test -p analysis_engine +cargo clippy -p analysis_engine --all-targets -- -D warnings +python3 scripts/validate_documentation.py +``` + +Regression contracts cover equivalent cutoff instants, future-evidence replay, +cross-snapshot refusal, robust-point versus naive-mean cancellation, exact and +exceeded draw/resource bounds, inconsistent Rubin totals, artifact count +bounds, and terminal provider/domain-status separation. + +## Rollback and supersession + +Rollback removes the `rubin_loading_uncertainty_v1` profile. No persisted +schema migration is introduced. Supersede only with an ADR that preserves the +scientific owner split, temporal provenance, resource admission, and the +Rubin-versus-Mislevy claim boundary. diff --git a/docs/adr/README.md b/docs/adr/README.md index 1254c8079..8f043ab4c 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -30,6 +30,7 @@ Read [`ADR_POLICY.md`](ADR_POLICY.md) first. **Decision status and implementatio | [0022](0022-deterministic-analysis-run-execution.md) | Deterministic cutoff-safe analysis-run execution | Accepted | active-PR | Closes the first executable product path from accepted run to digest-bound terminal result without claiming estimator authority. | | [0024](0024-lineage-pair-criterion-and-project-journey-posterior.md) | Independent Event Lineage pair criterion and posterior Project Journey | Proposed | active-PR | Strict artifacts preserve criterion/event-time draws, branches, ties, and CPU/GPU receipts without claiming the scientific estimator is complete. | | [0025](0025-macos-native-rust-mlx-metal-boundary.md) | macOS-native Rust-owned MLX Metal execution | Accepted | accepted-target | Compose authenticates to a native host service; Linux never claims Metal, and actual backend/parity receipts fail closed. | +| [0034](0034-rubin-loading-uncertainty-analysis-run.md) | Rubin loading uncertainty as an analysis-run output profile | Accepted | active-PR | Binds draw-mean OLS loadings and Rubin `T` to `rubin_loading_uncertainty_v1`; not Mislevy person-level plausible values. 0026–0033 remain on other live PRs. | | [0023](0023-lineage-criterion-anchor-contract.md) | TEPP-owned Event Lineage criterion anchor | Accepted | active-PR | PR #237 publishes the strict accepted/rejected artifact and identities; estimator execution remains fail-closed future work. | | [0024](0024-independent-topic-importance-anchor.md) | Posterior topic-context producer contract | Accepted | contract-only active-PR | Strict DTO/schema only; the current estimator does not emit it. fast-mlsirm owns case-deletion influence. | | [0001](0001-rust-first-modular-msa.md) | Rust-first numerical core and CPU `f64` reference | Accepted | partial | ADR 0011 owns cross-service/MSA authority; 0001 retains numerical/backend authority. | @@ -140,6 +141,7 @@ Use the narrowest owning ADR when decisions overlap: - **accepted-run execution and terminal artifact production:** ADR 0022. - **independent lineage criterion and posterior Project Journey:** ADR 0023. - **macOS-native Rust-owned MLX Metal execution:** ADR 0024. +- **Rubin loading-uncertainty analysis-run output profile:** ADR 0034. ## Change and supersession rule diff --git a/docs/doctoring/rubin-loading-uncertainty-analysis-run.md b/docs/doctoring/rubin-loading-uncertainty-analysis-run.md new file mode 100644 index 000000000..95a58691b --- /dev/null +++ b/docs/doctoring/rubin-loading-uncertainty-analysis-run.md @@ -0,0 +1,31 @@ +# Rubin loading-uncertainty analysis-run bind + +**Review date:** 2026-09-14 +**Active slice:** GAP-006 / issue #169 operator-visible composition +**Scientific acceptance owner:** #503 +**Implementation maturity:** active-PR — not implemented-main + +Protected main owns the reusable numerical contracts. `recover_loading_point_estimate_mean` performs the robust point-estimate aggregation, while `combine_draw_level_ols_loadings` owns Rubin (1996) `Q̄`, `Ū`, `B`, and `T`. This Analysis Run profile composes those owners without copying either arithmetic path. + +The review found that the predecessor branch was materially weaker than its cutoff-safe claim. It lacked per-row immutable snapshot provenance, compared cutoffs as timestamp text, admitted an unbounded draw dimension, accepted artifact counts outside the executable population, did not verify serialized Rubin `T`, used the Rubin mean as the supposedly robust point estimate, asserted artifact hashing could not fail, and wrote the scientific inference label into provider `validation_status`. + +## Repair lineage + +- RED `152cb3913d0920850d9a53e13cb6b4edfff26fb7` makes equivalent RFC 3339 cutoff instants, provider/domain status separation, robust point-estimate cancellation, inconsistent Rubin totals, count bounds, and the draw ceiling executable contracts. +- Causal repair `dd46f8ebac0e1bc110648152453dc9fce87bef23` adds per-observation immutable `snapshot_id`, instant-based cutoff binding, cross-snapshot refusal, a 256-draw / 1,000,000 admitted-cell application resource envelope, reachable artifact count validation, exact binary64 Rubin-total integrity, symmetric 256 KiB artifact wire admission, protected-main robust point-estimate invocation, error propagation, and terminal `validation_status = "validated"`. +- Contract migration `8f0e743348290d65d15e048216cccb1f5953f2af` updates the integration surface to explicit snapshot provenance and adds historical replay, cross-snapshot, resource-matrix, point-estimate, cutoff-instant, and terminal-status regressions. +- ADR `a4a0ab0120c9222418f8042af68149e57d8cfe38` returns ADR 0034 from premature `Accepted` branch authority to `Proposed` and records the scientific/resource alternatives. + +The historical replay contract is explicit: same-snapshot rows with `AvailableTime > KnowledgeCutoff` do not enter the earlier scientific matrix. Cross-snapshot rows are provenance violations and fail closed rather than being censored. Persisted artifacts use canonical cutoff text, while request/executor binding is by temporal instant. + +The 256-draw and 1,000,000 matrix-cell ceilings are application resource limits for the current representation. They are not psychometric recommendations and must not be cited as scientific sample-size or imputation-count guidance. + +## Evidence boundary + +The current deterministic fixtures verify contracts and known identities. They do not establish profile-level recovery or interval coverage. Issue #503 requires repeated true-loading recovery, bias/RMSE with Monte Carlo uncertainty, an explicitly justified interval construction before any coverage claim, empirical coverage with Monte Carlo uncertainty, attempted/recovered/failed denominators, design sensitivity, and leakage-safe historical evaluation. + +Rubin, D. B. (1996). Multiple imputation after 18+ years. *Journal of the American Statistical Association, 91*(434), 473–489. https://doi.org/10.1080/01621459.1996.10476908 + +Repository authority: `docs/research/rubin-total-variance.md`. + +Exact-head required workflows, owned-production line/branch coverage, resolved current findings, a qualifying independent approval, conflict-resolving successor inheritance, and #503 or equivalent checked-in scientific evidence remain required before Ready/release claims. Predecessor receipts do not transfer.