key-data / models /tests /deep_dive_forward_hold.py
tostido's picture
Update tests to target gen42 and remove gen52
4bfd01e
"""
Deep Dive: forward_hold() - HOLD-aware inference
The CapsuleAgent.forward_hold() method is the production API
for HOLD-aware inference. It:
1. Runs the brain forward pass
2. Yields a HOLD point automatically
3. Returns the final action (possibly overridden)
This is what the arcade stream uses for Twitch chat control!
"""
import numpy as np
import threading
import time
# Load the champion
import champion_gen42 as ch
print("=" * 60)
print("🎮 forward_hold() - HOLD-aware Arcade Inference")
print("=" * 60)
# Create agent
agent = ch.CapsuleAgent()
print(f"\nAgent: {agent}")
print(f"Genesis: {ch.get_genesis_root()}")
# Get the HOLD singleton
hold = ch.get_hold()
print(f"Hold: {hold}")
print(f"Hold enabled: {hold.enabled}")
print(f"Available: {[x for x in dir(hold) if not x.startswith('_')]}")
# Add a listener via the listeners list
def on_hold(hp):
print(f"\n🔴 HOLD POINT from forward_hold()!")
print(f" Brain ID: {hp.brain_id}")
print(f" Action probs: {hp.action_probs}")
if hp.action_labels:
print(f" Labels: {hp.action_labels}")
print(f" Value: {hp.value:.3f}")
hold.listeners.append(on_hold)
# Simulate arcade observation
observation = {
"screen": np.random.rand(84, 84, 3).astype(np.float32),
"lives": 3,
"score": 1500
}
print("\n=== Test 1: Non-blocking forward_hold ===")
result = agent.forward_hold(
inputs={"observation": observation},
blocking=False # Don't block, just return AI choice
)
print(f"\nResult keys: {result.keys()}")
print(f"Action: {result.get('action')}")
print(f"Was override: {result.get('was_override', False)}")
print(f"Merkle: {result.get('merkle_root', 'N/A')}")
print("\n=== Test 2: Blocking with Override ===")
def override_thread():
"""Simulates chat override"""
time.sleep(0.3)
print("\n💬 Chat: !override 0")
hold.override(0) # Embedded hold state only takes action
# Start override thread
t = threading.Thread(target=override_thread)
t.start()
# This will block until override or timeout
result2 = agent.forward_hold(
inputs={"observation": observation},
blocking=True
)
t.join()
print(f"\nResult 2:")
print(f" Action: {result2.get('action')}")
print(f" Was override: {result2.get('was_override', False)}")
print(f" Override source: {result2.get('override_source', 'N/A')}")
# Check provenance
print("\n=== Provenance Check ===")
prov = agent.get_full_provenance()
print(f"Quine hash: {prov.get('quine_hash', 'N/A')[:16]}...")
print(f"Genesis: {prov.get('genesis_root', 'N/A')}")
print(f"Observation count: {prov.get('observation_count', 0)}")
print("\n=== Hold Stats ===")
print(f"Hold count: {hold.hold_count}")
print(f"Override count: {hold.override_count}")
print(f"Last merkle: {hold.last_merkle}")