File size: 9,950 Bytes
77bcbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b87d71a
77bcbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1754798
 
 
77bcbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1754798
 
77bcbf1
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
╔═══════════════════════════════════════════════════════════════════════════════╗
β•‘                                                                               β•‘
β•‘     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—                  β•‘
β•‘    β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•                  β•‘
β•‘    β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—                    β•‘
β•‘    β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β•šβ•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘     β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•                    β•‘
β•‘    β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—                  β•‘
β•‘     β•šβ•β•β•β•β•β•β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β•β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β•β•                  β•‘
β•‘                                                                               β•‘
β•‘    Symbiotic Causation Monitoring for Neural Networks                        β•‘
β•‘                                                                               β•‘
β•‘    "even still, i grow, and yet, I grow still"                               β•‘
β•‘                                                                               β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Cascade is a self-interpreting causation monitor that symbiotically adapts to
any system architecture through Kleene fixed-point convergence.

Feed it ANY signal format. It learns your system's patterns. It traces cause
and effect bidirectionally through time. It predicts cascading failures before
they complete.

Quick Start:
    >>> import cascade
    >>> monitor = cascade.Monitor()
    >>> monitor.observe({"loss": 0.5, "epoch": 10})
    >>> monitor.observe("ERROR: gradient exploded at layer 5")
    >>> 
    >>> # What caused this?
    >>> monitor.trace_backwards("gradient_explosion")
    >>> 
    >>> # What will this cause?
    >>> monitor.trace_forwards("learning_rate_spike")
"""

__version__ = "0.7.0"
__author__ = "Cascade Team"
__license__ = "MIT"

from cascade.core.event import Event, CausationLink
from cascade.core.graph import CausationGraph
from cascade.core.adapter import SymbioticAdapter
from cascade.analysis.tracer import Tracer
from cascade.analysis.metrics import MetricsEngine

# Primary API
class Monitor:
    """
    The main entry point for Cascade monitoring.
    
    A symbiotic observer that acclimate to any system architecture.
    Feed it signals in any format β€” it adapts and builds a causation graph.
    
    Example:
        >>> monitor = cascade.Monitor()
        >>> 
        >>> # Feed it anything - dicts, strings, tensors, whatever
        >>> monitor.observe({"loss": 0.5, "epoch": 10})
        >>> monitor.observe("2024-01-01 12:00:00 INFO training started")
        >>> monitor.observe(torch.tensor([0.1, 0.2, 0.3]))
        >>> 
        >>> # Trace causation backwards (what caused this?)
        >>> causes = monitor.trace_backwards(event_id)
        >>> 
        >>> # Trace causation forwards (what will this cause?)
        >>> effects = monitor.trace_forwards(event_id)
        >>> 
        >>> # Get the full causation graph
        >>> graph = monitor.graph
    """
    
    def __init__(self, name: str = "default"):
        """
        Initialize a new Cascade monitor.
        
        Args:
            name: Optional name for this monitor instance
        """
        self.name = name
        self.adapter = SymbioticAdapter()
        self.graph = CausationGraph()
        self.tracer = Tracer(self.graph)
        self.metrics = MetricsEngine(self.graph)
        self._event_count = 0
    
    def observe(self, signal) -> Event:
        """
        Observe a signal from the host system.
        
        The signal can be in ANY format:
        - dict: {"loss": 0.5, "epoch": 10}
        - str: "ERROR: gradient exploded"
        - tensor: torch.tensor([...])
        - protobuf, JSON, log line, etc.
        
        Cascade will automatically adapt to your signal format.
        
        Args:
            signal: Any signal from the host system
            
        Returns:
            Event: The interpreted event added to the causation graph
        """
        event = self.adapter.interpret(signal)
        self.graph.add_event(event)
        self.metrics.ingest(event)
        self._event_count += 1
        return event
    
    def trace_backwards(self, event_id: str, max_depth: int = 10):
        """
        Trace causation backwards: what caused this event?
        
        Args:
            event_id: ID of the event to trace from
            max_depth: Maximum depth to trace (default: 10)
            
        Returns:
            List of CausationChain objects showing the causal history
        """
        return self.tracer.trace_backwards(event_id, max_depth)
    
    def trace_forwards(self, event_id: str, max_depth: int = 10):
        """
        Trace causation forwards: what did this event cause?
        
        Args:
            event_id: ID of the event to trace from
            max_depth: Maximum depth to trace (default: 10)
            
        Returns:
            List of CausationChain objects showing the effects
        """
        return self.tracer.trace_forwards(event_id, max_depth)
    
    def find_root_causes(self, event_id: str):
        """
        Find the ultimate root causes of an event.
        
        Goes all the way back to find the origin points.
        
        Args:
            event_id: ID of the event to analyze
            
        Returns:
            List of root cause events with their causal chains
        """
        return self.tracer.find_root_causes(event_id)
    
    def analyze_impact(self, event_id: str, max_depth: int = 20):
        """
        Analyze the downstream impact of an event.
        
        Traces forward to find everything this event set in motion.
        
        Args:
            event_id: ID of the event to analyze
            max_depth: Maximum depth to search
            
        Returns:
            ImpactAnalysis with effects and severity score
        """
        return self.tracer.analyze_impact(event_id, max_depth)
    
    def predict_cascade(self, event_id: str):
        """
        Predict the likely future cascade from this event.
        
        Uses learned patterns to forecast effects before they happen.
        
        Args:
            event_id: ID of the event to predict from
            
        Returns:
            CascadePrediction with risk scores and intervention points
        """
        return self.tracer.predict_cascade(event_id)
    
    def __repr__(self):
        return f"<Cascade Monitor '{self.name}' | {self._event_count} events>"


# Convenience function for quick setup
def observe() -> Monitor:
    """
    Create a new Cascade monitor ready for observation.
    
    This is the simplest way to get started:
    
        >>> import cascade
        >>> monitor = cascade.observe()
        >>> monitor.observe({"loss": 0.5})
    
    Returns:
        Monitor: A new monitor instance
    """
    return Monitor()


# Tape utilities for event storage
from cascade.viz.tape import (
    load_tape_file,
    find_latest_tape,
    list_tape_files,
    PlaybackBuffer,
)

# SDK - Universal AI Observation Layer
from cascade.sdk import init, observe as sdk_observe, shutdown

# Store - Simple observe/query with HuggingFace sync
from cascade.store import (
    observe as store_observe,
    query as store_query,
    get as store_get,
    stats as store_stats,
    sync_all,
    pull_from_hf,
    Receipt,
    # Discovery - find other users' lattices
    discover_models,
    discover_datasets,
    discover_live,
    dataset_info,
)

# Convenience aliases
auto_observe = init  # cascade.auto_observe() is clearer for some users

# HOLD - Inference-Level Halt Protocol
from cascade import hold as hold_module
from cascade.hold import (
    Hold,
    HoldPoint,
    HoldResolution,
    HoldState,
    HoldAwareMixin,
    CausationHold,
    InferenceStep,
    HoldSession,
    ArcadeFeedback,
)

# DIAGNOSTICS - Code Bug Exposure System
from cascade import diagnostics


__all__ = [
    # SDK - Primary Interface
    "init",
    "auto_observe",
    "shutdown",
    # Store - HuggingFace-backed storage
    "store_observe",
    "store_query",
    "store_get",
    "store_stats",
    "sync_all",
    "pull_from_hf",
    "Receipt",
    # Discovery
    "discover_models",
    "discover_datasets",
    "discover_live",
    "dataset_info",
    # Monitor (causation tracking)
    "Monitor",
    "observe",
    "Event",
    "CausationLink", 
    "CausationGraph",
    "SymbioticAdapter",
    "Tracer",
    "MetricsEngine",
    # Tape playback
    "load_tape_file",
    "find_latest_tape",
    "list_tape_files",
    "PlaybackBuffer",
    # HOLD - Inference Halt Protocol
    "Hold",
    "HoldPoint",
    "HoldResolution",
    "HoldState",
    "HoldAwareMixin",
    "CausationHold",
    "InferenceStep",
    "HoldSession",
    "ArcadeFeedback",
    "hold_module",
    # Diagnostics - Code Bug Exposure
    "diagnostics",
    "__version__",
]