cegraph 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cegraph-0.1.0/.github/workflows/ci.yml +28 -0
- cegraph-0.1.0/.github/workflows/publish.yml +24 -0
- cegraph-0.1.0/.gitignore +17 -0
- cegraph-0.1.0/.pre-commit-config.yaml +14 -0
- cegraph-0.1.0/CHANGELOG.md +10 -0
- cegraph-0.1.0/CONTRIBUTING.md +31 -0
- cegraph-0.1.0/LICENSE +21 -0
- cegraph-0.1.0/PKG-INFO +205 -0
- cegraph-0.1.0/README.md +152 -0
- cegraph-0.1.0/SECURITY.md +9 -0
- cegraph-0.1.0/docs/.gitkeep +0 -0
- cegraph-0.1.0/pyproject.toml +52 -0
- cegraph-0.1.0/src/cegraph/__init__.py +40 -0
- cegraph-0.1.0/src/cegraph/_version.py +1 -0
- cegraph-0.1.0/src/cegraph/causal/__init__.py +0 -0
- cegraph-0.1.0/src/cegraph/causal/counterfactual.py +142 -0
- cegraph-0.1.0/src/cegraph/causal/optimizer.py +104 -0
- cegraph-0.1.0/src/cegraph/core/__init__.py +0 -0
- cegraph-0.1.0/src/cegraph/core/context.py +48 -0
- cegraph-0.1.0/src/cegraph/core/graph.py +123 -0
- cegraph-0.1.0/src/cegraph/core/node.py +82 -0
- cegraph-0.1.0/src/cegraph/core/tracer.py +131 -0
- cegraph-0.1.0/src/cegraph/exceptions.py +24 -0
- cegraph-0.1.0/tests/__init__.py +0 -0
- cegraph-0.1.0/tests/conftest.py +25 -0
- cegraph-0.1.0/tests/test_counterfactual.py +126 -0
- cegraph-0.1.0/tests/test_graph.py +97 -0
- cegraph-0.1.0/tests/test_overhead.py +95 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, develop]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: pip install -e ".[dev]"
|
|
23
|
+
- name: Lint with ruff
|
|
24
|
+
run: ruff check src/ tests/
|
|
25
|
+
- name: Type check with mypy
|
|
26
|
+
run: mypy src/cegraph
|
|
27
|
+
- name: Run tests
|
|
28
|
+
run: pytest tests/ -v --tb=short
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment: pypi
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
- name: Build
|
|
20
|
+
run: |
|
|
21
|
+
pip install build
|
|
22
|
+
python -m build
|
|
23
|
+
- name: Publish to PyPI
|
|
24
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
cegraph-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.4.4
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff
|
|
6
|
+
args: [--fix]
|
|
7
|
+
- id: ruff-format
|
|
8
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
9
|
+
rev: v4.6.0
|
|
10
|
+
hooks:
|
|
11
|
+
- id: trailing-whitespace
|
|
12
|
+
- id: end-of-file-fixer
|
|
13
|
+
- id: check-yaml
|
|
14
|
+
- id: check-toml
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-05-18)
|
|
4
|
+
|
|
5
|
+
- Initial alpha release
|
|
6
|
+
- CausalGraph with cycle detection and type validation
|
|
7
|
+
- @causal_node decorator with constraint checking
|
|
8
|
+
- CausalTracer with ring buffer and adaptive sampling
|
|
9
|
+
- Counterfactual simulation engine
|
|
10
|
+
- Adaptive fallback optimizer
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Development Setup
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
python3 -m venv .venv
|
|
7
|
+
source .venv/bin/activate
|
|
8
|
+
pip install -e ".[dev]"
|
|
9
|
+
pre-commit install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Code Style
|
|
13
|
+
|
|
14
|
+
- Ruff for linting and formatting (line-length 88)
|
|
15
|
+
- MyPy strict mode for type checking
|
|
16
|
+
- All optional imports inside functions
|
|
17
|
+
- No external dependencies beyond numpy
|
|
18
|
+
|
|
19
|
+
## Testing
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pytest tests/ -v --tb=short
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Pull Requests
|
|
26
|
+
|
|
27
|
+
1. Fork the repository
|
|
28
|
+
2. Create a feature branch
|
|
29
|
+
3. Write tests for new functionality
|
|
30
|
+
4. Ensure all tests pass
|
|
31
|
+
5. Submit a PR with a clear description
|
cegraph-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 devcegraph
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cegraph-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cegraph
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Causal-aware execution runtime for production Python systems.
|
|
5
|
+
Project-URL: Homepage, https://github.com/keyreyla/cegraph
|
|
6
|
+
Project-URL: Repository, https://github.com/keyreyla/cegraph
|
|
7
|
+
Project-URL: Issues, https://github.com/keyreyla/cegraph/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/keyreyla/cegraph/blob/main/CHANGELOG.md
|
|
9
|
+
License: MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 devcegraph
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Keywords: causal-graph,causal-inference,counterfactual,debugging,execution-graph,mlops,observability,production-ml,runtime,tracing
|
|
32
|
+
Classifier: Development Status :: 3 - Alpha
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: Intended Audience :: Science/Research
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
41
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
42
|
+
Classifier: Topic :: System :: Monitoring
|
|
43
|
+
Requires-Python: >=3.10
|
|
44
|
+
Requires-Dist: numpy>=1.24
|
|
45
|
+
Provides-Extra: dev
|
|
46
|
+
Requires-Dist: build; extra == 'dev'
|
|
47
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
48
|
+
Requires-Dist: pytest-benchmark; extra == 'dev'
|
|
49
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
50
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
51
|
+
Requires-Dist: twine; extra == 'dev'
|
|
52
|
+
Description-Content-Type: text/markdown
|
|
53
|
+
|
|
54
|
+
# cegraph
|
|
55
|
+
|
|
56
|
+
[]()
|
|
57
|
+
[](https://pypi.org/project/cegraph/)
|
|
58
|
+
[](LICENSE)
|
|
59
|
+
[](CONTRIBUTING.md)
|
|
60
|
+
|
|
61
|
+
> Causal-aware execution runtime for production Python systems.
|
|
62
|
+
|
|
63
|
+
cegraph tracks causal dependencies between Python functions, records execution
|
|
64
|
+
traces, runs lightweight counterfactual simulations, and performs adaptive
|
|
65
|
+
fallback when constraints are violated. Built for MLOps, dynamic decisioning,
|
|
66
|
+
and root-cause analysis -- no heavy dependencies beyond numpy.
|
|
67
|
+
|
|
68
|
+
## Features
|
|
69
|
+
|
|
70
|
+
- **`@causal_node` decorator** -- Annotate functions with sensitivity flags and constraint checks, with under 7% overhead vs native calls
|
|
71
|
+
- **CausalGraph** -- Directed graph with DFS cycle detection and type-level edge validation. Pure Python, no networkx
|
|
72
|
+
- **CausalTracer** -- Lock-free ring buffer tracer with adaptive sampling. O(1) append, lossy on overflow
|
|
73
|
+
- **Counterfactual engine** -- Deterministic perturbation simulation (`seed=42`) with per-node impact scores and confidence
|
|
74
|
+
- **Fallback optimizer** -- Evaluate constraints (latency, confidence, critical) and dispatch cache/bypass/error fallback in order
|
|
75
|
+
|
|
76
|
+
## Quick Start
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from cegraph import causal_node, CausalGraph, Context, counterfactual, optimize
|
|
80
|
+
|
|
81
|
+
@causal_node(sensitivity=["price"])
|
|
82
|
+
def fetch_price(symbol: str) -> dict:
|
|
83
|
+
return {"price": 100.0, "symbol": symbol}
|
|
84
|
+
|
|
85
|
+
@causal_node(sensitivity=["price"])
|
|
86
|
+
def compute_markup(data: dict) -> float:
|
|
87
|
+
return data["price"] * 1.1
|
|
88
|
+
|
|
89
|
+
@causal_node(constraint=lambda x: x > 0)
|
|
90
|
+
def apply_strategy(base: float) -> float:
|
|
91
|
+
return base * 1.15
|
|
92
|
+
|
|
93
|
+
graph = CausalGraph()
|
|
94
|
+
graph.connect(fetch_price, compute_markup)
|
|
95
|
+
graph.connect(compute_markup, apply_strategy)
|
|
96
|
+
graph.validate()
|
|
97
|
+
|
|
98
|
+
with Context(graph=graph, buffer_size=1000) as ctx:
|
|
99
|
+
data = fetch_price("AAPL")
|
|
100
|
+
base = compute_markup(data)
|
|
101
|
+
result = apply_strategy(base)
|
|
102
|
+
|
|
103
|
+
cf = counterfactual(
|
|
104
|
+
base_trace=ctx.tracer.records,
|
|
105
|
+
interventions={"fetch_price": {"price": 150.0}},
|
|
106
|
+
)
|
|
107
|
+
print(f"Impact: {cf.overall_impact:.2f}, Confidence: {cf.confidence:.2f}")
|
|
108
|
+
|
|
109
|
+
result = optimize(ctx, constraints={"max_latency_ms": 50.0})
|
|
110
|
+
print(f"Status: {result.status}")
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Installation
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pip install cegraph
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Requires Python 3.10+ and numpy 1.24+.
|
|
120
|
+
|
|
121
|
+
## Use Cases
|
|
122
|
+
|
|
123
|
+
### MLOps & Model Drift
|
|
124
|
+
|
|
125
|
+
Production models degrade silently. Isolate the causal node responsible, run
|
|
126
|
+
counterfactual simulations, and route to fallback automatically.
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
@causal_node(sensitivity=["feature_distribution"])
|
|
130
|
+
def encode_features(raw: dict) -> dict:
|
|
131
|
+
return preprocessor.transform(raw)
|
|
132
|
+
|
|
133
|
+
@causal_node(sensitivity=["feature_distribution"], constraint=lambda x: x > 0.5)
|
|
134
|
+
def predict(features: dict) -> float:
|
|
135
|
+
return model.predict(features)
|
|
136
|
+
|
|
137
|
+
@causal_node()
|
|
138
|
+
def fallback_predict(features: dict) -> float:
|
|
139
|
+
return ensemble_fallback(features)
|
|
140
|
+
|
|
141
|
+
with Context(buffer_size=5000) as ctx:
|
|
142
|
+
score = predict(encode_features(input_data))
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
When `predict` violates its confidence constraint, `optimize()` returns
|
|
146
|
+
`fallback_cache` or `fallback_bypass` with recommendations.
|
|
147
|
+
|
|
148
|
+
### Dynamic Decisioning
|
|
149
|
+
|
|
150
|
+
Real-time what-if simulation. "If I raise price 5%, what's the causal impact?"
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
cf = counterfactual(
|
|
154
|
+
base_trace=ctx.tracer.records,
|
|
155
|
+
interventions={"pricing_model": {"price_multiplier": 1.05}},
|
|
156
|
+
n_perturbations=100,
|
|
157
|
+
)
|
|
158
|
+
print(f"Estimated impact: {cf.overall_impact:.3f}")
|
|
159
|
+
print(f"Sensitivity ranking: {cf.node_impacts}")
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Root-Cause Analysis
|
|
163
|
+
|
|
164
|
+
Replace correlation-timing debugging with explicit causal traces.
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
summary = ctx.tracer.summary()
|
|
168
|
+
for node, stats in summary.items():
|
|
169
|
+
print(f"{node}: count={stats['count']}, "
|
|
170
|
+
f"mean={stats['mean_latency']:.2f}ms, "
|
|
171
|
+
f"p95={stats['p95_latency']:.2f}ms")
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## API Reference
|
|
175
|
+
|
|
176
|
+
| API | Description |
|
|
177
|
+
|-----|-------------|
|
|
178
|
+
| `@causal_node(sensitivity, constraint, low_sensitivity)` | Decorate a function for causal tracking. `constraint` raises `CausalConstraintViolation` on failure. |
|
|
179
|
+
| `NodeMetadata` | Attached to decorated functions as `__cegraph_meta__`. |
|
|
180
|
+
| `CausalGraph.connect(src, dst)` | Register a causal edge. `validate()` runs DFS cycle detection + type checking. |
|
|
181
|
+
| `CausalGraph.ancestors(fn)` / `descendants(fn)` | Traverse upstream/downstream causal dependencies. |
|
|
182
|
+
| `Context(graph, buffer_size, sample_rate)` | Session scope. Binds tracer to all `@causal_node` calls via `threading.local`. |
|
|
183
|
+
| `CausalTracer` | Ring buffer (`deque(maxlen=N)`) with adaptive sampling. Overflow warning once. |
|
|
184
|
+
| `TraceRecord` | `__slots__` dataclass per node execution. Fields: `node_name`, `input_hash`, `output_summary`, `latency_ms`, `sensitivity_flags`, `timestamp`, `constraint_passed`. |
|
|
185
|
+
| `counterfactual(base_trace, interventions, ...)` | Deterministic perturbation engine. Returns `CounterfactualResult` with per-node `ImpactScore`. |
|
|
186
|
+
| `optimize(context, objective, constraints)` | Evaluate constraints in order: `max_latency_ms` -> `min_confidence` -> `critical`. Returns `OptimizationResult` with fallback status. |
|
|
187
|
+
|
|
188
|
+
### Exceptions
|
|
189
|
+
|
|
190
|
+
| Exception | Raised when |
|
|
191
|
+
|-----------|-------------|
|
|
192
|
+
| `CegraphError` | Base class for all cegraph errors. |
|
|
193
|
+
| `CausalCycleError` | A cycle is detected in the causal graph. |
|
|
194
|
+
| `CausalTypeError` | Output type of src node mismatches input type of dst node. |
|
|
195
|
+
| `CausalConstraintViolation` | A node's constraint function returns `False`. |
|
|
196
|
+
| `TracerOverflowWarning` | Ring buffer is full and overwriting old records (warning, not exception). |
|
|
197
|
+
|
|
198
|
+
## Contributing
|
|
199
|
+
|
|
200
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, code style, and
|
|
201
|
+
pull request process. All contributions are welcome.
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT -- see [LICENSE](LICENSE) for details.
|
cegraph-0.1.0/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# cegraph
|
|
2
|
+
|
|
3
|
+
[]()
|
|
4
|
+
[](https://pypi.org/project/cegraph/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](CONTRIBUTING.md)
|
|
7
|
+
|
|
8
|
+
> Causal-aware execution runtime for production Python systems.
|
|
9
|
+
|
|
10
|
+
cegraph tracks causal dependencies between Python functions, records execution
|
|
11
|
+
traces, runs lightweight counterfactual simulations, and performs adaptive
|
|
12
|
+
fallback when constraints are violated. Built for MLOps, dynamic decisioning,
|
|
13
|
+
and root-cause analysis -- no heavy dependencies beyond numpy.
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **`@causal_node` decorator** -- Annotate functions with sensitivity flags and constraint checks, with under 7% overhead vs native calls
|
|
18
|
+
- **CausalGraph** -- Directed graph with DFS cycle detection and type-level edge validation. Pure Python, no networkx
|
|
19
|
+
- **CausalTracer** -- Lock-free ring buffer tracer with adaptive sampling. O(1) append, lossy on overflow
|
|
20
|
+
- **Counterfactual engine** -- Deterministic perturbation simulation (`seed=42`) with per-node impact scores and confidence
|
|
21
|
+
- **Fallback optimizer** -- Evaluate constraints (latency, confidence, critical) and dispatch cache/bypass/error fallback in order
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from cegraph import causal_node, CausalGraph, Context, counterfactual, optimize
|
|
27
|
+
|
|
28
|
+
@causal_node(sensitivity=["price"])
|
|
29
|
+
def fetch_price(symbol: str) -> dict:
|
|
30
|
+
return {"price": 100.0, "symbol": symbol}
|
|
31
|
+
|
|
32
|
+
@causal_node(sensitivity=["price"])
|
|
33
|
+
def compute_markup(data: dict) -> float:
|
|
34
|
+
return data["price"] * 1.1
|
|
35
|
+
|
|
36
|
+
@causal_node(constraint=lambda x: x > 0)
|
|
37
|
+
def apply_strategy(base: float) -> float:
|
|
38
|
+
return base * 1.15
|
|
39
|
+
|
|
40
|
+
graph = CausalGraph()
|
|
41
|
+
graph.connect(fetch_price, compute_markup)
|
|
42
|
+
graph.connect(compute_markup, apply_strategy)
|
|
43
|
+
graph.validate()
|
|
44
|
+
|
|
45
|
+
with Context(graph=graph, buffer_size=1000) as ctx:
|
|
46
|
+
data = fetch_price("AAPL")
|
|
47
|
+
base = compute_markup(data)
|
|
48
|
+
result = apply_strategy(base)
|
|
49
|
+
|
|
50
|
+
cf = counterfactual(
|
|
51
|
+
base_trace=ctx.tracer.records,
|
|
52
|
+
interventions={"fetch_price": {"price": 150.0}},
|
|
53
|
+
)
|
|
54
|
+
print(f"Impact: {cf.overall_impact:.2f}, Confidence: {cf.confidence:.2f}")
|
|
55
|
+
|
|
56
|
+
result = optimize(ctx, constraints={"max_latency_ms": 50.0})
|
|
57
|
+
print(f"Status: {result.status}")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install cegraph
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Requires Python 3.10+ and numpy 1.24+.
|
|
67
|
+
|
|
68
|
+
## Use Cases
|
|
69
|
+
|
|
70
|
+
### MLOps & Model Drift
|
|
71
|
+
|
|
72
|
+
Production models degrade silently. Isolate the causal node responsible, run
|
|
73
|
+
counterfactual simulations, and route to fallback automatically.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
@causal_node(sensitivity=["feature_distribution"])
|
|
77
|
+
def encode_features(raw: dict) -> dict:
|
|
78
|
+
return preprocessor.transform(raw)
|
|
79
|
+
|
|
80
|
+
@causal_node(sensitivity=["feature_distribution"], constraint=lambda x: x > 0.5)
|
|
81
|
+
def predict(features: dict) -> float:
|
|
82
|
+
return model.predict(features)
|
|
83
|
+
|
|
84
|
+
@causal_node()
|
|
85
|
+
def fallback_predict(features: dict) -> float:
|
|
86
|
+
return ensemble_fallback(features)
|
|
87
|
+
|
|
88
|
+
with Context(buffer_size=5000) as ctx:
|
|
89
|
+
score = predict(encode_features(input_data))
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
When `predict` violates its confidence constraint, `optimize()` returns
|
|
93
|
+
`fallback_cache` or `fallback_bypass` with recommendations.
|
|
94
|
+
|
|
95
|
+
### Dynamic Decisioning
|
|
96
|
+
|
|
97
|
+
Real-time what-if simulation. "If I raise price 5%, what's the causal impact?"
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
cf = counterfactual(
|
|
101
|
+
base_trace=ctx.tracer.records,
|
|
102
|
+
interventions={"pricing_model": {"price_multiplier": 1.05}},
|
|
103
|
+
n_perturbations=100,
|
|
104
|
+
)
|
|
105
|
+
print(f"Estimated impact: {cf.overall_impact:.3f}")
|
|
106
|
+
print(f"Sensitivity ranking: {cf.node_impacts}")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Root-Cause Analysis
|
|
110
|
+
|
|
111
|
+
Replace correlation-timing debugging with explicit causal traces.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
summary = ctx.tracer.summary()
|
|
115
|
+
for node, stats in summary.items():
|
|
116
|
+
print(f"{node}: count={stats['count']}, "
|
|
117
|
+
f"mean={stats['mean_latency']:.2f}ms, "
|
|
118
|
+
f"p95={stats['p95_latency']:.2f}ms")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## API Reference
|
|
122
|
+
|
|
123
|
+
| API | Description |
|
|
124
|
+
|-----|-------------|
|
|
125
|
+
| `@causal_node(sensitivity, constraint, low_sensitivity)` | Decorate a function for causal tracking. `constraint` raises `CausalConstraintViolation` on failure. |
|
|
126
|
+
| `NodeMetadata` | Attached to decorated functions as `__cegraph_meta__`. |
|
|
127
|
+
| `CausalGraph.connect(src, dst)` | Register a causal edge. `validate()` runs DFS cycle detection + type checking. |
|
|
128
|
+
| `CausalGraph.ancestors(fn)` / `descendants(fn)` | Traverse upstream/downstream causal dependencies. |
|
|
129
|
+
| `Context(graph, buffer_size, sample_rate)` | Session scope. Binds tracer to all `@causal_node` calls via `threading.local`. |
|
|
130
|
+
| `CausalTracer` | Ring buffer (`deque(maxlen=N)`) with adaptive sampling. Overflow warning once. |
|
|
131
|
+
| `TraceRecord` | `__slots__` dataclass per node execution. Fields: `node_name`, `input_hash`, `output_summary`, `latency_ms`, `sensitivity_flags`, `timestamp`, `constraint_passed`. |
|
|
132
|
+
| `counterfactual(base_trace, interventions, ...)` | Deterministic perturbation engine. Returns `CounterfactualResult` with per-node `ImpactScore`. |
|
|
133
|
+
| `optimize(context, objective, constraints)` | Evaluate constraints in order: `max_latency_ms` -> `min_confidence` -> `critical`. Returns `OptimizationResult` with fallback status. |
|
|
134
|
+
|
|
135
|
+
### Exceptions
|
|
136
|
+
|
|
137
|
+
| Exception | Raised when |
|
|
138
|
+
|-----------|-------------|
|
|
139
|
+
| `CegraphError` | Base class for all cegraph errors. |
|
|
140
|
+
| `CausalCycleError` | A cycle is detected in the causal graph. |
|
|
141
|
+
| `CausalTypeError` | Output type of src node mismatches input type of dst node. |
|
|
142
|
+
| `CausalConstraintViolation` | A node's constraint function returns `False`. |
|
|
143
|
+
| `TracerOverflowWarning` | Ring buffer is full and overwriting old records (warning, not exception). |
|
|
144
|
+
|
|
145
|
+
## Contributing
|
|
146
|
+
|
|
147
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, code style, and
|
|
148
|
+
pull request process. All contributions are welcome.
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT -- see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Reporting a Vulnerability
|
|
4
|
+
|
|
5
|
+
Please report security issues to the GitHub Issues tracker at
|
|
6
|
+
https://github.com/keyreyla/cegraph/issues.
|
|
7
|
+
|
|
8
|
+
Do not open public issues for critical vulnerabilities. Instead, contact the
|
|
9
|
+
maintainers directly via the repository's security advisory feature.
|
|
File without changes
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cegraph"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Causal-aware execution runtime for production Python systems."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = [
|
|
13
|
+
"causal-inference", "runtime", "observability", "mlops",
|
|
14
|
+
"execution-graph", "counterfactual", "tracing", "debugging",
|
|
15
|
+
"causal-graph", "production-ml",
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Intended Audience :: Science/Research",
|
|
21
|
+
"License :: OSI Approved :: MIT License",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
28
|
+
"Topic :: System :: Monitoring",
|
|
29
|
+
]
|
|
30
|
+
dependencies = ["numpy>=1.24"]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = ["pytest>=7.0", "pytest-benchmark", "ruff", "mypy", "build", "twine"]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/keyreyla/cegraph"
|
|
37
|
+
Repository = "https://github.com/keyreyla/cegraph"
|
|
38
|
+
Issues = "https://github.com/keyreyla/cegraph/issues"
|
|
39
|
+
Changelog = "https://github.com/keyreyla/cegraph/blob/main/CHANGELOG.md"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.version]
|
|
42
|
+
path = "src/cegraph/_version.py"
|
|
43
|
+
|
|
44
|
+
[tool.ruff]
|
|
45
|
+
src = ["src"]
|
|
46
|
+
line-length = 88
|
|
47
|
+
target-version = "py310"
|
|
48
|
+
|
|
49
|
+
[tool.mypy]
|
|
50
|
+
python_version = "3.10"
|
|
51
|
+
strict = true
|
|
52
|
+
files = ["src/cegraph"]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""cegraph -- Causal-aware execution runtime for production Python systems."""
|
|
2
|
+
|
|
3
|
+
from cegraph._version import __version__
|
|
4
|
+
from cegraph.causal.counterfactual import (
|
|
5
|
+
CounterfactualResult,
|
|
6
|
+
ImpactScore,
|
|
7
|
+
counterfactual,
|
|
8
|
+
)
|
|
9
|
+
from cegraph.causal.optimizer import OptimizationResult, optimize
|
|
10
|
+
from cegraph.core.context import Context
|
|
11
|
+
from cegraph.core.graph import CausalGraph
|
|
12
|
+
from cegraph.core.node import NodeMetadata, causal_node
|
|
13
|
+
from cegraph.core.tracer import CausalTracer, TraceRecord
|
|
14
|
+
from cegraph.exceptions import (
|
|
15
|
+
CausalConstraintViolation,
|
|
16
|
+
CausalCycleError,
|
|
17
|
+
CausalTypeError,
|
|
18
|
+
CegraphError,
|
|
19
|
+
TracerOverflowWarning,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"__version__",
|
|
24
|
+
"causal_node",
|
|
25
|
+
"NodeMetadata",
|
|
26
|
+
"CausalGraph",
|
|
27
|
+
"Context",
|
|
28
|
+
"CausalTracer",
|
|
29
|
+
"TraceRecord",
|
|
30
|
+
"counterfactual",
|
|
31
|
+
"CounterfactualResult",
|
|
32
|
+
"ImpactScore",
|
|
33
|
+
"optimize",
|
|
34
|
+
"OptimizationResult",
|
|
35
|
+
"CegraphError",
|
|
36
|
+
"CausalCycleError",
|
|
37
|
+
"CausalTypeError",
|
|
38
|
+
"CausalConstraintViolation",
|
|
39
|
+
"TracerOverflowWarning",
|
|
40
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
File without changes
|