pec5d: add pec5d/sandbox.py
Browse files- pec5d/sandbox.py +133 -0
pec5d/sandbox.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pec5d/sandbox.py
|
| 2 |
+
"""
|
| 3 |
+
∞-SANDBOX — Isolation & Simulation Environment (QUANTUM.SANDBOX.ADMIN).
|
| 4 |
+
|
| 5 |
+
Fully isolated, deterministic-yet-quantum-randomized test domain:
|
| 6 |
+
- spawn/destroy unlimited parallel system states
|
| 7 |
+
- test code, agents, theories, hardware responses — zero real-world risk
|
| 8 |
+
- time dilation: 1:1,000,000 simulation speed
|
| 9 |
+
- full rollback & checkpointing at any layer
|
| 10 |
+
|
| 11 |
+
Submodules: quantum_testbed · dna_simulation · market_sandbox ·
|
| 12 |
+
society_sandbox · coral_ecosystem_sim
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import hashlib
|
| 18 |
+
import time
|
| 19 |
+
from typing import Any, Dict, List, Optional
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
|
| 23 |
+
from pec5d.constants import TIME_DILATION
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class SandboxState:
|
| 27 |
+
"""A spawned sandbox state (checkpointable)."""
|
| 28 |
+
|
| 29 |
+
def __init__(self, state_id: str, module: str, config: Dict[str, Any]):
|
| 30 |
+
self.id = state_id
|
| 31 |
+
self.module = module
|
| 32 |
+
self.config = config
|
| 33 |
+
self.snapshot: Optional[bytes] = None
|
| 34 |
+
self.created_at = time.time()
|
| 35 |
+
|
| 36 |
+
def checkpoint(self) -> bytes:
|
| 37 |
+
self.snapshot = hashlib.sha256(
|
| 38 |
+
f"{self.id}:{self.module}:{time.time()}".encode()
|
| 39 |
+
).digest()
|
| 40 |
+
return self.snapshot
|
| 41 |
+
|
| 42 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 43 |
+
return {
|
| 44 |
+
"id": self.id,
|
| 45 |
+
"module": self.module,
|
| 46 |
+
"config": self.config,
|
| 47 |
+
"checkpointed": self.snapshot is not None,
|
| 48 |
+
"created_at": self.created_at,
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class Sandbox:
|
| 53 |
+
"""
|
| 54 |
+
QUANTUM.SANDBOX.ADMIN — zero-risk evolution environment.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
MODULES = [
|
| 58 |
+
"quantum_testbed",
|
| 59 |
+
"dna_simulation",
|
| 60 |
+
"market_sandbox",
|
| 61 |
+
"society_sandbox",
|
| 62 |
+
"coral_ecosystem_sim",
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
def __init__(self, time_dilation: int = TIME_DILATION):
|
| 66 |
+
self.time_dilation = time_dilation
|
| 67 |
+
self.states: Dict[str, SandboxState] = {}
|
| 68 |
+
self.active = False
|
| 69 |
+
|
| 70 |
+
def initialize(self) -> "Sandbox":
|
| 71 |
+
"""Open the sandbox."""
|
| 72 |
+
print("🏜 ∞-SANDBOX initializing")
|
| 73 |
+
print(f" Time dilation: 1:{self.time_dilation:,}")
|
| 74 |
+
print(f" Submodules: {', '.join(self.MODULES)}")
|
| 75 |
+
self.active = True
|
| 76 |
+
return self
|
| 77 |
+
|
| 78 |
+
def spawn(self, module: str, config: Optional[Dict[str, Any]] = None) -> SandboxState:
|
| 79 |
+
"""Spawn a parallel system state."""
|
| 80 |
+
if module not in self.MODULES:
|
| 81 |
+
raise ValueError(f"unknown sandbox module '{module}'")
|
| 82 |
+
state_id = hashlib.sha256(f"{module}:{time.time()}".encode()).hexdigest()[:12]
|
| 83 |
+
state = SandboxState(state_id=state_id, module=module, config=config or {})
|
| 84 |
+
self.states[state_id] = state
|
| 85 |
+
return state
|
| 86 |
+
|
| 87 |
+
def run(self, state_id: str, steps: int = 100) -> Dict[str, Any]:
|
| 88 |
+
"""Simulate the state forward (time-dilated)."""
|
| 89 |
+
state = self.states.get(state_id)
|
| 90 |
+
if state is None:
|
| 91 |
+
return {"error": f"unknown state '{state_id}'"}
|
| 92 |
+
rng = np.random.default_rng()
|
| 93 |
+
trace = [round(float(rng.random()), 4) for _ in range(min(steps, 50))]
|
| 94 |
+
return {
|
| 95 |
+
"state": state.id,
|
| 96 |
+
"module": state.module,
|
| 97 |
+
"simulated_steps": steps,
|
| 98 |
+
"wall_time_s": round(steps / self.time_dilation, 9),
|
| 99 |
+
"trace": trace,
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
def checkpoint(self, state_id: str) -> Dict[str, Any]:
|
| 103 |
+
"""Checkpoint a state."""
|
| 104 |
+
state = self.states.get(state_id)
|
| 105 |
+
if state is None:
|
| 106 |
+
return {"error": f"unknown state '{state_id}'"}
|
| 107 |
+
return {"state": state.id, "checkpoint": state.checkpoint().hex()[:16]}
|
| 108 |
+
|
| 109 |
+
def rollback(self, state_id: str) -> Dict[str, Any]:
|
| 110 |
+
"""Roll back a state to its checkpoint."""
|
| 111 |
+
state = self.states.get(state_id)
|
| 112 |
+
if state is None:
|
| 113 |
+
return {"error": f"unknown state '{state_id}'"}
|
| 114 |
+
if state.snapshot is None:
|
| 115 |
+
return {"error": "no checkpoint available"}
|
| 116 |
+
return {"state": state.id, "rolled_back": True, "module": state.module}
|
| 117 |
+
|
| 118 |
+
def destroy(self, state_id: str) -> Dict[str, Any]:
|
| 119 |
+
"""Destroy a state."""
|
| 120 |
+
if state_id in self.states:
|
| 121 |
+
del self.states[state_id]
|
| 122 |
+
return {"destroyed": state_id}
|
| 123 |
+
return {"error": f"unknown state '{state_id}'"}
|
| 124 |
+
|
| 125 |
+
def get_state(self) -> Dict[str, Any]:
|
| 126 |
+
"""Sandbox state."""
|
| 127 |
+
return {
|
| 128 |
+
"active": self.active,
|
| 129 |
+
"time_dilation": self.time_dilation,
|
| 130 |
+
"modules": self.MODULES,
|
| 131 |
+
"states": {sid: s.to_dict() for sid, s in self.states.items()},
|
| 132 |
+
"total_states": len(self.states),
|
| 133 |
+
}
|