File size: 3,393 Bytes
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
101
102
103
104
105
"""Deep dive into HOLD system and real-time inference interception."""
import sys
sys.path.insert(0, 'F:/End-Game/glassboxgames/children')

import cascade
from cascade import Hold, HoldPoint, HoldState
import numpy as np
import threading
import time

print("=" * 70)
print("⏸️ CASCADE HOLD SYSTEM - Real-time Inference Interception")
print("=" * 70)

# Get the Hold singleton
hold = Hold.get()
print(f"\nHold singleton: {hold}")
print(f"Current hold: {hold.current_hold}")
print(f"Stats: {hold.stats}")

# Register a listener to see hold points
hold_points_received = []

def my_listener(hold_point: HoldPoint):
    print(f"\n🛑 HOLD POINT RECEIVED!")
    print(f"  Brain: {hold_point.brain_id}")
    print(f"  AI choice: {hold_point.ai_choice}")
    print(f"  Confidence: {hold_point.ai_confidence:.2%}")
    print(f"  Merkle: {hold_point.merkle_root}")
    print(f"  State: {hold_point.state}")
    hold_points_received.append(hold_point)
    
    # Auto-accept after 0.1s for demo
    time.sleep(0.1)
    hold.accept()

hold.register_listener(my_listener)
print("\n✅ Listener registered")

# Simulate a brain yielding a hold point
print("\n=== Simulating Hold Point (non-blocking) ===")

def simulate_brain():
    # Simulate what happens inside a brain's forward()
    action_probs = np.array([0.1, 0.15, 0.5, 0.25])  # 4 actions
    value = 0.73
    observation = {'game': 'test', 'frame': 42}
    
    print(f"Brain: yielding hold point...")
    resolution = hold.yield_point(
        action_probs=action_probs,
        value=value,
        observation=observation,
        brain_id="test_brain_001",
        action_labels=["UP", "DOWN", "FIRE", "STAY"],
        blocking=False  # Non-blocking for demo
    )
    print(f"Brain: got resolution -> action={resolution.action}, override={resolution.was_override}, source={resolution.override_source}")
    return resolution

result = simulate_brain()

print(f"\n=== Result ===")
print(f"Action taken: {result.action}")
print(f"Was override: {result.was_override}")
print(f"Override source: {result.override_source}")
print(f"Hold duration: {result.hold_duration:.4f}s")
print(f"Merkle root: {result.merkle_root}")

# Try override scenario
print("\n=== Testing Override Scenario ===")

override_received = None

def override_listener(hp: HoldPoint):
    global override_received
    override_received = hp
    print(f"\n🛑 HOLD for override test")
    print(f"  AI wants action {hp.ai_choice} ({hp.ai_confidence:.0%} confident)")
    # Simulate human override
    time.sleep(0.05)
    hold.override(action=0, source="human_player")

hold.unregister_listener(my_listener)
hold.register_listener(override_listener)

action_probs = np.array([0.1, 0.8, 0.05, 0.05])  # AI strongly prefers action 1
resolution = hold.yield_point(
    action_probs=action_probs,
    value=0.9,
    observation={'test': 'override'},
    brain_id="test_brain_002",
    action_labels=["CONSERVATIVE", "AGGRESSIVE", "RETREAT", "WAIT"],
    blocking=False
)

print(f"\n  Final action: {resolution.action}")
print(f"  Was override: {resolution.was_override}")
print(f"  Override source: {resolution.override_source}")
print(f"  AI wanted: 1 (AGGRESSIVE)")
print(f"  Human chose: 0 (CONSERVATIVE)")

print("\n=== Hold Stats After Test ===")
print(hold.stats)