File size: 2,862 Bytes
a80c9f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bfd01e
a80c9f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""

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}")