Skip to content

Approximating a Basis

lpetronika edited this page Feb 16, 2026 · 3 revisions

Approximating a Basis

react-state-basis is a runtime diagnostic tool. It uses temporal heuristics and graph theory to figure out which state variables in your app are actually independent, by sampling behavior at runtime. This page covers what data the engine collects, what it can't see, and the constraints of the heuristics.

Data Collection Parameters

What the engine observes:

  • Quantized Update Pulses: Which browser frames (Ticks) a state transition happened in, sampled via requestAnimationFrame.
  • Causal Topology ($G$): A directed adjacency matrix mapping Sources (Effects, Events) to Targets (State updates).
  • Signal Roles ($U$ vs $W$): Whether a signal is a Local Basis Vector (useState/useReducer) or a Global Subspace Anchor (createContext).
  • Temporal Patterns: Recurring update sequences within a circular 50-tick sliding window.

What the engine cannot observe:

  • Semantic Intent: The tool sees behavioral patterns. It has no idea what a variable is for in your business logic.
  • Static Source Analysis: Relationships come from runtime behavior, not from reading your dependency arrays or source code.
  • High-Latency Events: If an async delay exceeds the active interaction window ($\approx 10s$), the engine treats those signals as independent. It can't hold onto everything forever without leaking memory.

Heuristic Development Path

Capability v0.4.x (Correlation) v0.5.x (Decomposition) v0.6.x (Graph Era)
Redundancy Logic Lead-Lag Subspace-Aware Topological (Sibling Check)
Causality Math Raw Correlation Role-Filtered Spectral Influence ($Ax = \lambda x$)
Memory Model Ring Buffer Atomic Snapshots Sparse Adjacency Matrix
Analysis Trigger Scheduled (Ticks) Reactive (Dirty) On-Demand (Ranker)
Audit Scope Flat Signals Direct Sum ($U \oplus W$) Prime Movers (Root Causes)

Each era is a new layer, not a replacement. The engine runs all three models together: signal data from v0.4, role definitions from v0.5, and the topology from v0.6 all feed into each other.

Environmental Constraints

The browser is non-deterministic, and that affects analysis accuracy:

  1. Microtask Scheduling: Browser task prioritization can create coincidental update patterns. Basis uses Signal Roles to protect "System Transactions" (where multiple global contexts update together) from being incorrectly flagged as redundant.
  2. Phase Jitter: Jitter in async execution (timers, network I/O) can weaken correlation strength. The 0.88 similarity threshold was tuned empirically to filter this noise while still catching real structural synchronization.
  3. Graph Pruning: The topological graph enforces a Time-To-Live (TTL) on implicit event nodes. Patterns are captured within the active interaction window, and old causal links are pruned to free memory.

Quality Standards

The goal is Diagnostic Accuracy.

The Metric: The tool is doing its job if it identifies architectural patterns at runtime, like Global Event Fragmentation or Boolean Explosion, that would normally require a manual deep-dive code review to find.

What matters most is the Signal-to-Noise Ratio. Basis uses Conditional Independence Checks to filter out "Sibling" updates (variables that just happen to be triggered by the same event), so that only real Causal Leaks (actually daisy-chained updates) get flagged as Priority 1 issues.

Technical Guidelines for Developers

  • Refactor by Rank: Start with the Prime Movers (Priority #1 in the report). Fixing a root cause like a Global Event often resolves downstream sync issues automatically.
  • Role-Based Refactoring: Use the tool to find local hooks that are "shadowing" global context, then replace them with direct consumption.
  • Interpreting Correlation: High correlation means a behavioral relationship exists. The tool suggests the refactor path based on the detected role: Duplicate State (merge) or Context Mirroring (delete).

Summary: react-state-basis is a diagnostic tool for identifying patterns that static analysis can't see. It gives you a data-driven signal for where your architecture can be simplified.

Clone this wiki locally