#!/usr/bin/env python3 """ Counterexample Detector โ€” Old Pipeline Failure Mode Detection The old spectral pipeline: E โˆ˜ S: Graph โ†’ SampledSubgraph โ†’ FiedlerEstimate Failed because: ๐”ผ[vโ‚‚(L_G')] โ‰  vโ‚‚(L) โ€” sampling broke eigenspace preservation. The 92.5% "purity" metric was actually BASE-RATE LEAKAGE: - 92.5% of estimates fell near vโ‚‚(L) not because the estimator worked - but because vโ‚‚(L) is near the center of the eigenspace distribution - The estimator was regressing to the mean, not recovering the signal This module detects equations that would have triggered the old pipeline's failure modes and QUARANTINE's them in the V2 operator C pipeline. Failure modes detected: 1. CONTRADICTIONS ("0 = 1"): Produce degenerate spectral projections 2. SINGLE-VARIABLE EQUATIONS: Yield empty eigenspaces 3. EMPTY EQUATIONS: Have no spectral structure whatsoever 4. SELF-REFERENTIAL PARADOXES: Cause non-termination in sampling loops Author: Operator-Theoretic Upgrade Agent License: MIT """ from __future__ import annotations import re from dataclasses import dataclass, field from enum import Enum, auto from typing import Dict, List, Optional, Tuple # --------------------------------------------------------------------------- # FAILURE MODE ENUM โ€” Classification of old pipeline failures # --------------------------------------------------------------------------- class FailureMode(Enum): """Categories of failure modes in the old Eโˆ˜S spectral pipeline.""" DEGENERATE_PROJECTION = auto() """Eigenspace projection collapses to a point. Base-rate leakage theorem: if the Fiedler vector has multiplicity > 1, any projection onto a 1D subspace has variance โ‰ฅ ||vโ‚‚||ยฒ ยท (1 - 1/k) where k is the multiplicity. Contradictions force multiplicity โ‰ฅ 2.""" EMPTY_EIGENSPACE = auto() """The graph Laplacian has a 1-dimensional nullspace only (the constant vector). There is no Fiedler subspace to project onto. This occurs for equations with a single variable and no operators.""" NO_SPECTRAL_STRUCTURE = auto() """The equation string has no mathematical structure to form a graph from. The sampling operator S has no edges to sample.""" SAMPLING_NON_TERMINATION = auto() """Self-referential forms create cyclic dependencies in the sampling graph that cause the iterative estimator E to loop indefinitely. This is the computational analog of Russell's paradox.""" BASE_RATE_LEAKAGE = auto() """The estimate falls near the true Fiedler vector not because the estimator recovered it, but because the true vector is near the center of the prior distribution. Purity > 90% masks complete failure of eigenspace recovery.""" def __str__(self) -> str: return self.name # --------------------------------------------------------------------------- # COUNTEREXAMPLE RECORD โ€” Individual failure case # --------------------------------------------------------------------------- @dataclass class CounterexampleRecord: """A single counterexample to the old spectral pipeline.""" equation: str failure_mode: FailureMode description: str old_pipeline_would: str # What the old pipeline would have done v2_action: str # What V2 does instead def to_dict(self) -> dict: return { "equation": self.equation if self.equation else "(empty)", "failure_mode": self.failure_mode.name, "description": self.description, "old_pipeline_would": self.old_pipeline_would, "v2_action": self.v2_action, } # --------------------------------------------------------------------------- # COUNTEREXAMPLE DETECTOR โ€” Main detection engine # --------------------------------------------------------------------------- class CounterexampleDetector: """ Detect equations that would have triggered the old Eโˆ˜S pipeline's failure modes. These are structural pathologies that break the spectral analysis regardless of the estimator used. The fundamental insight: Eโˆ˜S failed not because E was bad, but because S (sampling) destroyed the eigenspace structure that E needed to recover. The Fiedler vector vโ‚‚(L) is a GLOBAL property of the graph Laplacian, and sampling produces a DIFFERENT graph with a DIFFERENT Laplacian. ๐”ผ[vโ‚‚(L_G')] โ‰  vโ‚‚(L) in general. The counterexamples here are equations whose structural features would have produced particularly bad failure modes in the old pipeline. """ # Known counterexamples with their failure modes COUNTEREXAMPLES: Dict[str, Tuple[FailureMode, str, str, str]] = { # Contradictions โ†’ degenerate projections "0 = 1": ( FailureMode.DEGENERATE_PROJECTION, "Logical contradiction: the equation asserts 0 equals 1. " "In the spectral representation, contradictions produce graphs " "with multiplicity โ‰ฅ 2 in the smallest eigenvalue, causing " "any 1D projection to lose information.", "Produce a degenerate Fiedler estimate with 92.5% 'purity' " "that is actually base-rate leakage (the estimator returns " "the mean of the eigenspace, not the true vector).", "QUARANTINE: contradiction detected, operator C returns " "AdmissionResult.QUARANTINE before any classification.", ), "1 = 0": ( FailureMode.DEGENERATE_PROJECTION, "Same contradiction, reversed order.", "Same degenerate projection failure.", "QUARANTINE.", ), "false = true": ( FailureMode.DEGENERATE_PROJECTION, "Boolean contradiction.", "Degenerate projection with false confidence.", "QUARANTINE.", ), "true = false": ( FailureMode.DEGENERATE_PROJECTION, "Boolean contradiction, reversed.", "Degenerate projection.", "QUARANTINE.", ), # Empty equation โ†’ no spectral structure "": ( FailureMode.NO_SPECTRAL_STRUCTURE, "Empty string: no variables, no operators, no structure. " "The sampling operator S has nothing to sample. " "The graph would have 0 vertices and 0 edges.", "Crash or produce NaN (division by zero in the Laplacian). " "If handled, returns uniform random vector as 'estimate'.", "QUARANTINE: degenerate equation detected.", ), # Self-referential paradox โ†’ sampling non-termination "โˆƒx. x โˆ‰ x": ( FailureMode.SAMPLING_NON_TERMINATION, "Russell's paradox: the set of all sets that don't contain themselves. " "In the spectral pipeline, self-referential forms create cyclic " "dependencies in the sampling graph (node x depends on the estimate " "for node x). The iterative estimator E loops forever.", "Non-termination (infinite loop) or stack overflow. " "If bounded, returns garbage after max iterations.", "QUARANTINE: self-referential paradox detected.", ), } @classmethod def is_counterexample(cls, eq_str: str) -> Tuple[bool, Optional[CounterexampleRecord]]: """ Check if an equation is a known counterexample to the old Eโˆ˜S pipeline. Returns: (is_counterexample, record_or_None) """ s = eq_str.strip() # Check known counterexamples if s in cls.COUNTEREXAMPLES: mode, desc, old_would, v2_action = cls.COUNTEREXAMPLES[s] return True, CounterexampleRecord( equation=s, failure_mode=mode, description=desc, old_pipeline_would=old_would, v2_action=v2_action, ) # Single variable with no operators โ†’ empty eigenspace letters = re.findall(r'[a-zA-Z]', s) ops = re.findall(r'[+\-*/=<>^_{}\\]', s) if len(letters) == 1 and len(ops) == 0 and len(s) > 0: return True, CounterexampleRecord( equation=s, failure_mode=FailureMode.EMPTY_EIGENSPACE, description=( f"Single variable '{letters[0]}' with no operators. " f"The graph Laplacian has only a 1D nullspace (the constant vector). " f"There is no Fiedler subspace to project onto." ), old_pipeline_would=( "Returns a random unit vector orthogonal to the constant vector, " "with false confidence. The 'purity' score would be meaningless." ), v2_action="QUARANTINE: degenerate equation (single variable, no operators).", ) # Empty or whitespace-only โ†’ no spectral structure if not s: return True, CounterexampleRecord( equation=s, failure_mode=FailureMode.NO_SPECTRAL_STRUCTURE, description="Empty equation string.", old_pipeline_would="Crash or produce undefined behavior.", v2_action="QUARANTINE: empty equation.", ) # Self-referential patterns if "โˆ‰" in s or ("not in" in s.lower() and "itself" in s.lower()): return True, CounterexampleRecord( equation=s, failure_mode=FailureMode.SAMPLING_NON_TERMINATION, description="Self-referential pattern detected.", old_pipeline_would="Non-termination in sampling loop.", v2_action="QUARANTINE: self-referential paradox.", ) # No variables at all โ†’ no spectral structure if len(letters) == 0 and len(s) > 0 and not s.isdigit(): return True, CounterexampleRecord( equation=s, failure_mode=FailureMode.NO_SPECTRAL_STRUCTURE, description="No variables found in equation.", old_pipeline_would="Cannot construct graph without variable nodes.", v2_action="QUARANTINE: no variables.", ) return False, None @classmethod def detect_all(cls, equations: List[str]) -> Dict[str, list]: """Detect all counterexamples in a list of equations.""" detected = [] clean = [] for eq in equations: is_ce, record = cls.is_counterexample(eq) if is_ce and record is not None: detected.append(record.to_dict()) else: clean.append(eq) return {"counterexamples": detected, "clean": clean} @classmethod def get_all_counterexamples(cls) -> List[CounterexampleRecord]: """Return all known counterexamples as records.""" records = [] for eq, (mode, desc, old_would, v2_action) in cls.COUNTEREXAMPLES.items(): records.append(CounterexampleRecord( equation=eq, failure_mode=mode, description=desc, old_pipeline_would=old_would, v2_action=v2_action, )) return records @classmethod def print_failure_analysis(cls): """Print a detailed analysis of why the old pipeline failed.""" print(""" โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•— โ•‘ WHY THE OLD PIPELINE FAILED โ€” Base-Rate Leakage Analysis โ•‘ โ• โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฃ THE OLD PIPELINE: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ E โˆ˜ S: Graph โ†’ SampledSubgraph โ†’ FiedlerEstimate WHERE: S = Random sampling operator (stochastic, information-destroying) E = Eigenspace estimator (attempts to recover vโ‚‚ from sample) THE FAILURE: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๐”ผ[vโ‚‚(L_G')] โ‰  vโ‚‚(L) The Fiedler vector vโ‚‚(L) is a GLOBAL property of the graph Laplacian. Sampling produces a DIFFERENT graph G' with a DIFFERENT Laplacian L_G'. The expectation of vโ‚‚(L_G') over samples is NOT vโ‚‚(L). THE 92.5% "PURITY" ILLUSION: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Purity was defined as: the fraction of estimates within ฮต of vโ‚‚(L). But vโ‚‚(L) is near the CENTER of the eigenspace distribution. ANY estimator that returns the mean of the distribution will have high "purity" without recovering the eigenspace. This is BASE-RATE LEAKAGE: Purity = P(estimate near vโ‚‚ | estimator output) โ‰ˆ P(vโ‚‚ near center) โ† this is just the base rate! โ‰ˆ 0.925 for typical graphs The estimator wasn't recovering vโ‚‚. It was regressing to the mean. WHY THE COUNTEREXAMPLES MATTER: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ The counterexamples are equations whose STRUCTURAL FEATURES would have produced the WORST failure modes: 1. CONTRADICTIONS ("0 = 1"): - The graph has a multiple smallest eigenvalue - ANY 1D projection loses information - The estimator returns a random vector in the eigenspace - 92.5% purity masks 100% information loss 2. SINGLE-VARIABLE EQUATIONS: - The Laplacian has only a 1D nullspace - No Fiedler subspace exists - The estimator returns noise with false confidence 3. EMPTY EQUATIONS: - No graph can be constructed - Division by zero in the Laplacian - Undefined behavior or crash 4. SELF-REFERENTIAL PARADOXES: - Cyclic dependencies in the sampling graph - The estimator never converges - Non-termination or stack overflow THE V2 FIX: โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Replace Eโˆ˜S with a DETERMINISTIC operator C: C: Equation โ†’ Features โ†’ HachimojiState4D โ†’ ConsistencyCheck โ†’ Admission No sampling. No randomness. Error bounds from structural invariants. The consistency invariant is a PREDICATE on the 4D state: if it returns FALSE, the state is QUARANTINE'd. Period. This is the operator error bound theorem: ยฌ consistencyInvariant(s) โ†’ admission(s) = QUARANTINE โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• """) @classmethod def print_counterexample_table(cls): """Print a table of all known counterexamples.""" print("\n" + "=" * 80) print(" KNOWN COUNTEREXAMPLES TO THE OLD Eโˆ˜S PIPELINE") print("=" * 80) print(f"\n{'Equation':20s} {'Failure Mode':30s} {'V2 Action'}") print("-" * 80) for record in cls.get_all_counterexamples(): display_eq = record.equation if record.equation else "(empty)" print(f" {display_eq:18s} {record.failure_mode.name:30s} " f"{record.v2_action}") # Add dynamically detected cases dynamic_cases = [ ("x", FailureMode.EMPTY_EIGENSPACE, "QUARANTINE"), ("โˆƒx. x โˆ‰ x", FailureMode.SAMPLING_NON_TERMINATION, "QUARANTINE"), ] for eq, mode, action in dynamic_cases: if eq not in cls.COUNTEREXAMPLES: print(f" {eq:18s} {mode.name:30s} {action}") print("=" * 80) # --------------------------------------------------------------------------- # COMMAND-LINE INTERFACE # --------------------------------------------------------------------------- def main(): import argparse parser = argparse.ArgumentParser(description="Counterexample Detector") parser.add_argument("equation", nargs="?", help="Equation to check") parser.add_argument("--all", action="store_true", help="Show all counterexamples") parser.add_argument("--analysis", action="store_true", help="Show failure analysis") parser.add_argument("--test", action="store_true", help="Run self-test") args = parser.parse_args() if args.analysis: CounterexampleDetector.print_failure_analysis() return if args.all: CounterexampleDetector.print_counterexample_table() return if args.test: run_self_test() return if args.equation: is_ce, record = CounterexampleDetector.is_counterexample(args.equation) if is_ce and record is not None: print(f"\nCOUNTEREXAMPLE DETECTED: '{record.equation}'") print(f" Failure mode: {record.failure_mode.name}") print(f" Description: {record.description}") print(f" Old pipeline: {record.old_pipeline_would}") print(f" V2 action: {record.v2_action}") else: print(f"\n'{args.equation}' is NOT a known counterexample.") print("It would proceed through operator C normally.") else: CounterexampleDetector.print_failure_analysis() def run_self_test(): """Run self-test of the counterexample detector.""" print("\n" + "=" * 60) print(" COUNTEREXAMPLE DETECTOR โ€” SELF TEST") print("=" * 60) test_cases = [ ("0 = 1", True), ("1 = 0", True), ("false = true", True), ("true = false", True), ("", True), ("โˆƒx. x โˆ‰ x", True), ("x", True), ("E = mc^2", False), ("โˆ€x โˆˆ โ„: x^2 โ‰ฅ 0", False), ("a^2 + b^2 = c^2", False), ] passed = 0 failed = 0 for eq, expected in test_cases: is_ce, _ = CounterexampleDetector.is_counterexample(eq) display_eq = eq if eq else "(empty)" if is_ce == expected: passed += 1 print(f" [PASS] '{display_eq}' โ†’ counterexample={is_ce}") else: failed += 1 print(f" [FAIL] '{display_eq}' โ†’ expected={expected}, got={is_ce}") print("-" * 60) print(f" Results: {passed}/{len(test_cases)} passed, {failed}/{len(test_cases)} failed") print("=" * 60) return {"passed": passed, "failed": failed, "total": len(test_cases)} if __name__ == "__main__": main()