|
| 1 | +# ZHIR Bifurcation Detection Implementation Summary |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation adds bifurcation potential detection to the ZHIR (Mutation) operator according to **AGENTS.md §U4a (Bifurcation Dynamics)**. When structural acceleration ∂²EPI/∂t² exceeds threshold τ, ZHIR detects and records the bifurcation potential through telemetry flags. |
| 6 | + |
| 7 | +## Theoretical Basis |
| 8 | + |
| 9 | +### From AGENTS.md §U4a: |
| 10 | + |
| 11 | +> **Physics**: ∂²EPI/∂t² > τ requires control |
| 12 | +> **Requirement**: If {OZ, ZHIR}, include {THOL, IL} |
| 13 | +> **Why**: Uncontrolled bifurcation → chaos |
| 14 | +
|
| 15 | +### Implication for ZHIR: |
| 16 | + |
| 17 | +ZHIR, as an operator that can induce **high structural acceleration**, must: |
| 18 | +1. Verify if ∂²EPI/∂t² > τ (bifurcation threshold) |
| 19 | +2. If threshold exceeded, activate bifurcation detection mechanism |
| 20 | +3. Record event for validation of grammar U4a |
| 21 | + |
| 22 | +## Implementation Approach: Option B (Conservative) |
| 23 | + |
| 24 | +We implemented **Option B** - detection without creation - as the conservative first approach: |
| 25 | + |
| 26 | +### What ZHIR Does: |
| 27 | +- ✅ Computes ∂²EPI/∂t² from EPI history using finite difference |
| 28 | +- ✅ Compares against threshold τ |
| 29 | +- ✅ Sets telemetry flags when threshold exceeded |
| 30 | +- ✅ Logs informative message |
| 31 | +- ✅ Records event in graph for analysis |
| 32 | + |
| 33 | +### What ZHIR Does NOT Do: |
| 34 | +- ❌ Does NOT create structural variants |
| 35 | +- ❌ Does NOT create new nodes or edges |
| 36 | +- ❌ Does NOT modify graph structure |
| 37 | +- ❌ Does NOT spawn sub-EPIs (that's THOL's role) |
| 38 | + |
| 39 | +## Code Changes |
| 40 | + |
| 41 | +### 1. Mutation Class Enhancement (`src/tnfr/operators/definitions.py`) |
| 42 | + |
| 43 | +#### Added Methods: |
| 44 | + |
| 45 | +```python |
| 46 | +def __call__(self, G: TNFRGraph, node: Any, **kw: Any) -> None: |
| 47 | + """Apply ZHIR with bifurcation potential detection.""" |
| 48 | + # Compute structural acceleration |
| 49 | + d2_epi = self._compute_epi_acceleration(G, node) |
| 50 | + |
| 51 | + # Get threshold |
| 52 | + tau = kw.get("tau") or G.graph.get("BIFURCATION_THRESHOLD_TAU", 0.5) |
| 53 | + |
| 54 | + # Apply base operator |
| 55 | + super().__call__(G, node, **kw) |
| 56 | + |
| 57 | + # Detect bifurcation potential if acceleration exceeds threshold |
| 58 | + if d2_epi > tau: |
| 59 | + self._detect_bifurcation_potential(G, node, d2_epi=d2_epi, tau=tau) |
| 60 | + |
| 61 | +def _compute_epi_acceleration(self, G: TNFRGraph, node: Any) -> float: |
| 62 | + """Calculate ∂²EPI/∂t² using finite difference approximation.""" |
| 63 | + history = G.nodes[node].get("epi_history", []) |
| 64 | + if len(history) < 3: |
| 65 | + return 0.0 |
| 66 | + |
| 67 | + # Finite difference: d²EPI/dt² ≈ (EPI_t - 2*EPI_{t-1} + EPI_{t-2}) |
| 68 | + epi_t = float(history[-1]) |
| 69 | + epi_t1 = float(history[-2]) |
| 70 | + epi_t2 = float(history[-3]) |
| 71 | + d2_epi = epi_t - 2.0 * epi_t1 + epi_t2 |
| 72 | + |
| 73 | + return abs(d2_epi) |
| 74 | + |
| 75 | +def _detect_bifurcation_potential(self, G: TNFRGraph, node: Any, |
| 76 | + d2_epi: float, tau: float) -> None: |
| 77 | + """Detect and record bifurcation potential.""" |
| 78 | + # Set telemetry flags |
| 79 | + G.nodes[node]["_zhir_bifurcation_potential"] = True |
| 80 | + G.nodes[node]["_zhir_d2epi"] = d2_epi |
| 81 | + G.nodes[node]["_zhir_tau"] = tau |
| 82 | + |
| 83 | + # Record event |
| 84 | + G.graph.setdefault("zhir_bifurcation_events", []).append({ |
| 85 | + "node": node, |
| 86 | + "d2_epi": d2_epi, |
| 87 | + "tau": tau, |
| 88 | + "timestamp": len(G.nodes[node].get("glyph_history", [])), |
| 89 | + }) |
| 90 | + |
| 91 | + # Log information |
| 92 | + logger.info( |
| 93 | + f"Node {node}: ZHIR bifurcation potential detected " |
| 94 | + f"(∂²EPI/∂t²={d2_epi:.3f} > τ={tau}). " |
| 95 | + f"Consider applying THOL for controlled bifurcation or IL for stabilization." |
| 96 | + ) |
| 97 | +``` |
| 98 | + |
| 99 | +## Configuration |
| 100 | + |
| 101 | +### Threshold Configuration (Priority Order): |
| 102 | + |
| 103 | +1. **Explicit parameter**: `Mutation()(G, node, tau=0.3)` |
| 104 | +2. **Canonical config**: `G.graph["BIFURCATION_THRESHOLD_TAU"] = 0.5` |
| 105 | +3. **Operator-specific**: `G.graph["ZHIR_BIFURCATION_THRESHOLD"] = 0.5` |
| 106 | +4. **Default**: `0.5` |
| 107 | + |
| 108 | +### Default Rationale: |
| 109 | + |
| 110 | +- ZHIR default (0.5) is **higher** than THOL default (0.1) |
| 111 | +- ZHIR phase transformations are already controlled |
| 112 | +- Higher threshold = more conservative detection |
| 113 | +- Reduces false positives in typical mutation scenarios |
| 114 | + |
| 115 | +## Telemetry |
| 116 | + |
| 117 | +### Node-Level Flags: |
| 118 | + |
| 119 | +- `_zhir_bifurcation_potential`: Boolean - True if bifurcation detected |
| 120 | +- `_zhir_d2epi`: Float - Computed acceleration value |
| 121 | +- `_zhir_tau`: Float - Threshold used for detection |
| 122 | + |
| 123 | +### Graph-Level Events: |
| 124 | + |
| 125 | +```python |
| 126 | +G.graph["zhir_bifurcation_events"] = [ |
| 127 | + { |
| 128 | + "node": "node_id", |
| 129 | + "d2_epi": 0.123, |
| 130 | + "tau": 0.05, |
| 131 | + "timestamp": 5 |
| 132 | + }, |
| 133 | + ... |
| 134 | +] |
| 135 | +``` |
| 136 | + |
| 137 | +## Testing |
| 138 | + |
| 139 | +### Test Coverage (`tests/unit/operators/test_zhir_bifurcation_detection.py`): |
| 140 | + |
| 141 | +#### 1. Detection Tests (9 tests): |
| 142 | +- High acceleration → detection |
| 143 | +- Low acceleration → no detection |
| 144 | +- Telemetry flags correctness |
| 145 | +- Event recording |
| 146 | +- Configuration parameters |
| 147 | + |
| 148 | +#### 2. Integration Tests (3 tests): |
| 149 | +- OZ → ZHIR sequence with detection |
| 150 | +- Full sequence without structural changes |
| 151 | +- Preservation of existing ZHIR functionality |
| 152 | + |
| 153 | +#### 3. Edge Cases (4 tests): |
| 154 | +- Insufficient history |
| 155 | +- Exactly at threshold |
| 156 | +- Negative acceleration (magnitude) |
| 157 | +- Multiple ZHIR calls |
| 158 | + |
| 159 | +#### 4. Backward Compatibility (3 tests): |
| 160 | +- Works without epi_history |
| 161 | +- No breaking config changes |
| 162 | +- API unchanged |
| 163 | + |
| 164 | +#### 5. Grammar U4a Support (2 tests): |
| 165 | +- Detection enables U4a validation |
| 166 | +- No detection = no U4a requirement |
| 167 | + |
| 168 | +### Test Results: |
| 169 | + |
| 170 | +``` |
| 171 | +✅ 21/21 bifurcation detection tests PASS |
| 172 | +✅ 13/13 existing ZHIR phase tests PASS |
| 173 | +✅ 24/24 integration tests PASS |
| 174 | +✅ 0 CodeQL security alerts |
| 175 | +``` |
| 176 | + |
| 177 | +## Example Usage |
| 178 | + |
| 179 | +### Example 1: High Acceleration → Detection |
| 180 | + |
| 181 | +```python |
| 182 | +from tnfr.structural import create_nfr |
| 183 | +from tnfr.operators.definitions import Mutation |
| 184 | + |
| 185 | +G, node = create_nfr("system", epi=0.5, vf=1.0) |
| 186 | + |
| 187 | +# Build history with high acceleration |
| 188 | +G.nodes[node]["epi_history"] = [0.30, 0.40, 0.60] # d²EPI = 0.10 |
| 189 | +G.graph["BIFURCATION_THRESHOLD_TAU"] = 0.05 |
| 190 | + |
| 191 | +Mutation()(G, node) |
| 192 | + |
| 193 | +# Check detection |
| 194 | +assert G.nodes[node]["_zhir_bifurcation_potential"] == True |
| 195 | +print(f"Detected: ∂²EPI/∂t² = {G.nodes[node]['_zhir_d2epi']:.3f}") |
| 196 | +# Output: Detected: ∂²EPI/∂t² = 0.100 |
| 197 | +``` |
| 198 | + |
| 199 | +### Example 2: Low Acceleration → No Detection |
| 200 | + |
| 201 | +```python |
| 202 | +# Nearly linear progression |
| 203 | +G.nodes[node]["epi_history"] = [0.48, 0.49, 0.50] # d²EPI ≈ 0.00 |
| 204 | + |
| 205 | +Mutation()(G, node) |
| 206 | + |
| 207 | +# No detection |
| 208 | +assert G.nodes[node].get("_zhir_bifurcation_potential") != True |
| 209 | +``` |
| 210 | + |
| 211 | +### Example 3: Grammar U4a Validation |
| 212 | + |
| 213 | +```python |
| 214 | +# With stabilizer (valid) |
| 215 | +run_sequence(G, node, [Dissonance(), Mutation(), Coherence()]) |
| 216 | +# Grammar U4a satisfied: ZHIR followed by IL |
| 217 | + |
| 218 | +# Without stabilizer (should be flagged) |
| 219 | +run_sequence(G, node, [Dissonance(), Mutation()]) |
| 220 | +# Grammar validator can check: if _zhir_bifurcation_potential and no IL/THOL |
| 221 | +# then flag as U4a violation |
| 222 | +``` |
| 223 | + |
| 224 | +## Grammar U4a Integration |
| 225 | + |
| 226 | +### How It Enables Validation: |
| 227 | + |
| 228 | +1. **ZHIR detects bifurcation**: Sets `_zhir_bifurcation_potential = True` |
| 229 | +2. **Grammar validator checks**: If flag is True, verify THOL or IL present |
| 230 | +3. **If missing**: Flag as U4a violation (uncontrolled bifurcation risk) |
| 231 | + |
| 232 | +### Grammar Rule: |
| 233 | + |
| 234 | +``` |
| 235 | +IF: |
| 236 | + - ZHIR applied |
| 237 | + - _zhir_bifurcation_potential == True |
| 238 | +THEN: |
| 239 | + - Sequence must contain THOL or IL within window |
| 240 | +ELSE: |
| 241 | + - Risk of uncontrolled bifurcation |
| 242 | +``` |
| 243 | + |
| 244 | +## Physics Alignment |
| 245 | + |
| 246 | +### Canonical TNFR Compliance: |
| 247 | + |
| 248 | +✅ **Invariant #5 (Phase Verification)**: No coupling created without phase check |
| 249 | +✅ **Invariant #9 (Structural Metrics)**: All telemetry properly exposed |
| 250 | +✅ **Invariant #10 (Domain Neutrality)**: No field-specific assumptions |
| 251 | +✅ **U4a (Bifurcation Dynamics)**: Detection enables grammar validation |
| 252 | +✅ **Physics-First**: Derived from nodal equation ∂EPI/∂t = νf · ΔNFR(t) |
| 253 | +✅ **Reproducible**: Deterministic computation from EPI history |
| 254 | + |
| 255 | +### Nodal Equation Basis: |
| 256 | + |
| 257 | +From the integrated nodal equation: |
| 258 | + |
| 259 | +``` |
| 260 | +EPI(t_f) = EPI(t_0) + ∫[t_0 to t_f] νf(τ) · ΔNFR(τ) dτ |
| 261 | +``` |
| 262 | + |
| 263 | +Second derivative with respect to time: |
| 264 | + |
| 265 | +``` |
| 266 | +∂²EPI/∂t² = ∂/∂t[νf · ΔNFR] |
| 267 | +``` |
| 268 | + |
| 269 | +High ∂²EPI/∂t² indicates rapid changes in reorganization dynamics → bifurcation potential. |
| 270 | + |
| 271 | +## Future Enhancements (Option A) |
| 272 | + |
| 273 | +### Potential Extensions: |
| 274 | + |
| 275 | +If Option A (bifurcation with variant creation) is needed: |
| 276 | + |
| 277 | +1. **Add `_spawn_mutation_variant()` method**: |
| 278 | + - Create variant node with orthogonal phase |
| 279 | + - Link to parent with "mutation_variant" relationship |
| 280 | + - Preserve parent EPI while creating alternative configuration |
| 281 | + |
| 282 | +2. **Feature flag**: `G.graph["ZHIR_BIFURCATION_MODE"] = "variant_creation"` |
| 283 | + |
| 284 | +3. **Tests for variant creation**: |
| 285 | + - Verify variant node created |
| 286 | + - Check orthogonal phase relationship |
| 287 | + - Validate edge creation |
| 288 | + - Confirm parent-child metadata |
| 289 | + |
| 290 | +### Why Option B First: |
| 291 | + |
| 292 | +- **Conservative**: No structural changes |
| 293 | +- **Safe**: Easy to validate and test |
| 294 | +- **Flexible**: Can extend to Option A later |
| 295 | +- **Focused**: Solves grammar U4a validation need |
| 296 | + |
| 297 | +## Files Modified |
| 298 | + |
| 299 | +### Core Implementation: |
| 300 | +- `src/tnfr/operators/definitions.py` (+127 lines) |
| 301 | + |
| 302 | +### Tests: |
| 303 | +- `tests/unit/operators/test_zhir_bifurcation_detection.py` (NEW, 461 lines) |
| 304 | + |
| 305 | +### Examples: |
| 306 | +- `examples/zhir_bifurcation_detection_example.py` (NEW, 170 lines) |
| 307 | + |
| 308 | +## Acceptance Criteria |
| 309 | + |
| 310 | +From issue specification: |
| 311 | + |
| 312 | +- [x] Function `_compute_epi_acceleration()` created in Mutation ✅ |
| 313 | +- [x] Verification of ∂²EPI/∂t² > τ implemented ✅ |
| 314 | +- [x] Option B (detection) implemented ✅ |
| 315 | +- [x] Option A (creation) available as future enhancement 🔄 |
| 316 | +- [x] Tests of bifurcation created and passing ✅ |
| 317 | +- [x] Metrics updated with `bifurcation_potential` and `d2_epi` ✅ |
| 318 | +- [x] Documentation updated with bifurcation example ✅ |
| 319 | + |
| 320 | +## Summary |
| 321 | + |
| 322 | +This implementation provides **robust bifurcation detection** for ZHIR while maintaining: |
| 323 | +- ✅ **Theoretical integrity**: Physics-based detection |
| 324 | +- ✅ **Backward compatibility**: No breaking changes |
| 325 | +- ✅ **Test coverage**: 21 comprehensive tests |
| 326 | +- ✅ **Domain neutrality**: Works across all TNFR applications |
| 327 | +- ✅ **Grammar support**: Enables U4a validation |
| 328 | +- ✅ **Extensibility**: Ready for Option A if needed |
| 329 | + |
| 330 | +**The ZHIR operator now properly detects and records bifurcation potential, enabling controlled bifurcation management in TNFR systems.** |
0 commit comments