crca 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl
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.
- CRCA.py +172 -7
- MODEL_CARD.md +53 -0
- PKG-INFO +8 -2
- RELEASE_NOTES.md +17 -0
- STABILITY.md +19 -0
- architecture/hybrid/consistency_engine.py +362 -0
- architecture/hybrid/conversation_manager.py +421 -0
- architecture/hybrid/explanation_generator.py +452 -0
- architecture/hybrid/few_shot_learner.py +533 -0
- architecture/hybrid/graph_compressor.py +286 -0
- architecture/hybrid/hybrid_agent.py +4398 -0
- architecture/hybrid/language_compiler.py +623 -0
- architecture/hybrid/main,py +0 -0
- architecture/hybrid/reasoning_tracker.py +322 -0
- architecture/hybrid/self_verifier.py +524 -0
- architecture/hybrid/task_decomposer.py +567 -0
- architecture/hybrid/text_corrector.py +341 -0
- benchmark_results/crca_core_benchmarks.json +178 -0
- branches/crca_sd/crca_sd_realtime.py +6 -2
- branches/general_agent/__init__.py +102 -0
- branches/general_agent/general_agent.py +1400 -0
- branches/general_agent/personality.py +169 -0
- branches/general_agent/utils/__init__.py +19 -0
- branches/general_agent/utils/prompt_builder.py +170 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
- crca_core/__init__.py +35 -0
- crca_core/benchmarks/__init__.py +14 -0
- crca_core/benchmarks/synthetic_scm.py +103 -0
- crca_core/core/__init__.py +23 -0
- crca_core/core/api.py +120 -0
- crca_core/core/estimate.py +208 -0
- crca_core/core/godclass.py +72 -0
- crca_core/core/intervention_design.py +174 -0
- crca_core/core/lifecycle.py +48 -0
- crca_core/discovery/__init__.py +9 -0
- crca_core/discovery/tabular.py +193 -0
- crca_core/identify/__init__.py +171 -0
- crca_core/identify/backdoor.py +39 -0
- crca_core/identify/frontdoor.py +48 -0
- crca_core/identify/graph.py +106 -0
- crca_core/identify/id_algorithm.py +43 -0
- crca_core/identify/iv.py +48 -0
- crca_core/models/__init__.py +67 -0
- crca_core/models/provenance.py +56 -0
- crca_core/models/refusal.py +39 -0
- crca_core/models/result.py +83 -0
- crca_core/models/spec.py +151 -0
- crca_core/models/validation.py +68 -0
- crca_core/scm/__init__.py +9 -0
- crca_core/scm/linear_gaussian.py +198 -0
- crca_core/timeseries/__init__.py +6 -0
- crca_core/timeseries/pcmci.py +181 -0
- crca_llm/__init__.py +12 -0
- crca_llm/client.py +85 -0
- crca_llm/coauthor.py +118 -0
- crca_llm/orchestrator.py +289 -0
- crca_llm/types.py +21 -0
- crca_reasoning/__init__.py +16 -0
- crca_reasoning/critique.py +54 -0
- crca_reasoning/godclass.py +206 -0
- crca_reasoning/memory.py +24 -0
- crca_reasoning/rationale.py +10 -0
- crca_reasoning/react_controller.py +81 -0
- crca_reasoning/tool_router.py +97 -0
- crca_reasoning/types.py +40 -0
- crca_sd/__init__.py +15 -0
- crca_sd/crca_sd_core.py +2 -0
- crca_sd/crca_sd_governance.py +2 -0
- crca_sd/crca_sd_mpc.py +2 -0
- crca_sd/crca_sd_realtime.py +2 -0
- crca_sd/crca_sd_tui.py +2 -0
- cuda-keyring_1.1-1_all.deb +0 -0
- cuda-keyring_1.1-1_all.deb.1 +0 -0
- docs/IMAGE_ANNOTATION_USAGE.md +539 -0
- docs/INSTALL_DEEPSPEED.md +125 -0
- docs/api/branches/crca-cg.md +19 -0
- docs/api/branches/crca-q.md +27 -0
- docs/api/branches/crca-sd.md +37 -0
- docs/api/branches/general-agent.md +24 -0
- docs/api/branches/overview.md +19 -0
- docs/api/crca/agent-methods.md +62 -0
- docs/api/crca/operations.md +79 -0
- docs/api/crca/overview.md +32 -0
- docs/api/image-annotation/engine.md +52 -0
- docs/api/image-annotation/overview.md +17 -0
- docs/api/schemas/annotation.md +34 -0
- docs/api/schemas/core-schemas.md +82 -0
- docs/api/schemas/overview.md +32 -0
- docs/api/schemas/policy.md +30 -0
- docs/api/utils/conversation.md +22 -0
- docs/api/utils/graph-reasoner.md +32 -0
- docs/api/utils/overview.md +21 -0
- docs/api/utils/router.md +19 -0
- docs/api/utils/utilities.md +97 -0
- docs/architecture/causal-graphs.md +41 -0
- docs/architecture/data-flow.md +29 -0
- docs/architecture/design-principles.md +33 -0
- docs/architecture/hybrid-agent/components.md +38 -0
- docs/architecture/hybrid-agent/consistency.md +26 -0
- docs/architecture/hybrid-agent/overview.md +44 -0
- docs/architecture/hybrid-agent/reasoning.md +22 -0
- docs/architecture/llm-integration.md +26 -0
- docs/architecture/modular-structure.md +37 -0
- docs/architecture/overview.md +69 -0
- docs/architecture/policy-engine-arch.md +29 -0
- docs/branches/crca-cg/corposwarm.md +39 -0
- docs/branches/crca-cg/esg-scoring.md +30 -0
- docs/branches/crca-cg/multi-agent.md +35 -0
- docs/branches/crca-cg/overview.md +40 -0
- docs/branches/crca-q/alternative-data.md +55 -0
- docs/branches/crca-q/architecture.md +71 -0
- docs/branches/crca-q/backtesting.md +45 -0
- docs/branches/crca-q/causal-engine.md +33 -0
- docs/branches/crca-q/execution.md +39 -0
- docs/branches/crca-q/market-data.md +60 -0
- docs/branches/crca-q/overview.md +58 -0
- docs/branches/crca-q/philosophy.md +60 -0
- docs/branches/crca-q/portfolio-optimization.md +66 -0
- docs/branches/crca-q/risk-management.md +102 -0
- docs/branches/crca-q/setup.md +65 -0
- docs/branches/crca-q/signal-generation.md +61 -0
- docs/branches/crca-q/signal-validation.md +43 -0
- docs/branches/crca-sd/core.md +84 -0
- docs/branches/crca-sd/governance.md +53 -0
- docs/branches/crca-sd/mpc-solver.md +65 -0
- docs/branches/crca-sd/overview.md +59 -0
- docs/branches/crca-sd/realtime.md +28 -0
- docs/branches/crca-sd/tui.md +20 -0
- docs/branches/general-agent/overview.md +37 -0
- docs/branches/general-agent/personality.md +36 -0
- docs/branches/general-agent/prompt-builder.md +30 -0
- docs/changelog/index.md +79 -0
- docs/contributing/code-style.md +69 -0
- docs/contributing/documentation.md +43 -0
- docs/contributing/overview.md +29 -0
- docs/contributing/testing.md +29 -0
- docs/core/crcagent/async-operations.md +65 -0
- docs/core/crcagent/automatic-extraction.md +107 -0
- docs/core/crcagent/batch-prediction.md +80 -0
- docs/core/crcagent/bayesian-inference.md +60 -0
- docs/core/crcagent/causal-graph.md +92 -0
- docs/core/crcagent/counterfactuals.md +96 -0
- docs/core/crcagent/deterministic-simulation.md +78 -0
- docs/core/crcagent/dual-mode-operation.md +82 -0
- docs/core/crcagent/initialization.md +88 -0
- docs/core/crcagent/optimization.md +65 -0
- docs/core/crcagent/overview.md +63 -0
- docs/core/crcagent/time-series.md +57 -0
- docs/core/schemas/annotation.md +30 -0
- docs/core/schemas/core-schemas.md +82 -0
- docs/core/schemas/overview.md +30 -0
- docs/core/schemas/policy.md +41 -0
- docs/core/templates/base-agent.md +31 -0
- docs/core/templates/feature-mixins.md +31 -0
- docs/core/templates/overview.md +29 -0
- docs/core/templates/templates-guide.md +75 -0
- docs/core/tools/mcp-client.md +34 -0
- docs/core/tools/overview.md +24 -0
- docs/core/utils/conversation.md +27 -0
- docs/core/utils/graph-reasoner.md +29 -0
- docs/core/utils/overview.md +27 -0
- docs/core/utils/router.md +27 -0
- docs/core/utils/utilities.md +97 -0
- docs/css/custom.css +84 -0
- docs/examples/basic-usage.md +57 -0
- docs/examples/general-agent/general-agent-examples.md +50 -0
- docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
- docs/examples/image-annotation/image-annotation-examples.md +54 -0
- docs/examples/integration/integration-examples.md +58 -0
- docs/examples/overview.md +37 -0
- docs/examples/trading/trading-examples.md +46 -0
- docs/features/causal-reasoning/advanced-topics.md +101 -0
- docs/features/causal-reasoning/counterfactuals.md +43 -0
- docs/features/causal-reasoning/do-calculus.md +50 -0
- docs/features/causal-reasoning/overview.md +47 -0
- docs/features/causal-reasoning/structural-models.md +52 -0
- docs/features/hybrid-agent/advanced-components.md +55 -0
- docs/features/hybrid-agent/core-components.md +64 -0
- docs/features/hybrid-agent/overview.md +34 -0
- docs/features/image-annotation/engine.md +82 -0
- docs/features/image-annotation/features.md +113 -0
- docs/features/image-annotation/integration.md +75 -0
- docs/features/image-annotation/overview.md +53 -0
- docs/features/image-annotation/quickstart.md +73 -0
- docs/features/policy-engine/doctrine-ledger.md +105 -0
- docs/features/policy-engine/monitoring.md +44 -0
- docs/features/policy-engine/mpc-control.md +89 -0
- docs/features/policy-engine/overview.md +46 -0
- docs/getting-started/configuration.md +225 -0
- docs/getting-started/first-agent.md +164 -0
- docs/getting-started/installation.md +144 -0
- docs/getting-started/quickstart.md +137 -0
- docs/index.md +118 -0
- docs/js/mathjax.js +13 -0
- docs/lrm/discovery_proof_notes.md +25 -0
- docs/lrm/finetune_full.md +83 -0
- docs/lrm/math_appendix.md +120 -0
- docs/lrm/overview.md +32 -0
- docs/mkdocs.yml +238 -0
- docs/stylesheets/extra.css +21 -0
- docs_generated/crca_core/CounterfactualResult.md +12 -0
- docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
- docs_generated/crca_core/DraftSpec.md +13 -0
- docs_generated/crca_core/EstimateResult.md +13 -0
- docs_generated/crca_core/IdentificationResult.md +17 -0
- docs_generated/crca_core/InterventionDesignResult.md +12 -0
- docs_generated/crca_core/LockedSpec.md +15 -0
- docs_generated/crca_core/RefusalResult.md +12 -0
- docs_generated/crca_core/ValidationReport.md +9 -0
- docs_generated/crca_core/index.md +13 -0
- examples/general_agent_example.py +277 -0
- examples/general_agent_quickstart.py +202 -0
- examples/general_agent_simple.py +92 -0
- examples/hybrid_agent_auto_extraction.py +84 -0
- examples/hybrid_agent_dictionary_demo.py +104 -0
- examples/hybrid_agent_enhanced.py +179 -0
- examples/hybrid_agent_general_knowledge.py +107 -0
- examples/image_annotation_quickstart.py +328 -0
- examples/test_hybrid_fixes.py +77 -0
- image_annotation/__init__.py +27 -0
- image_annotation/annotation_engine.py +2593 -0
- install_cuda_wsl2.sh +59 -0
- install_deepspeed.sh +56 -0
- install_deepspeed_simple.sh +87 -0
- mkdocs.yml +252 -0
- ollama/Modelfile +8 -0
- prompts/__init__.py +2 -1
- prompts/default_crca.py +9 -1
- prompts/general_agent.py +227 -0
- prompts/image_annotation.py +56 -0
- pyproject.toml +17 -2
- requirements-docs.txt +10 -0
- requirements.txt +21 -2
- schemas/__init__.py +26 -1
- schemas/annotation.py +222 -0
- schemas/conversation.py +193 -0
- schemas/hybrid.py +211 -0
- schemas/reasoning.py +276 -0
- schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
- schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
- schemas_export/crca_core/DraftSpec.schema.json +635 -0
- schemas_export/crca_core/EstimateResult.schema.json +113 -0
- schemas_export/crca_core/IdentificationResult.schema.json +145 -0
- schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
- schemas_export/crca_core/LockedSpec.schema.json +646 -0
- schemas_export/crca_core/RefusalResult.schema.json +90 -0
- schemas_export/crca_core/ValidationReport.schema.json +62 -0
- scripts/build_lrm_dataset.py +80 -0
- scripts/export_crca_core_schemas.py +54 -0
- scripts/export_hf_lrm.py +37 -0
- scripts/export_ollama_gguf.py +45 -0
- scripts/generate_changelog.py +157 -0
- scripts/generate_crca_core_docs_from_schemas.py +86 -0
- scripts/run_crca_core_benchmarks.py +163 -0
- scripts/run_full_finetune.py +198 -0
- scripts/run_lrm_eval.py +31 -0
- templates/graph_management.py +29 -0
- tests/conftest.py +9 -0
- tests/test_core.py +2 -3
- tests/test_crca_core_discovery_tabular.py +15 -0
- tests/test_crca_core_estimate_dowhy.py +36 -0
- tests/test_crca_core_identify.py +18 -0
- tests/test_crca_core_intervention_design.py +36 -0
- tests/test_crca_core_linear_gaussian_scm.py +69 -0
- tests/test_crca_core_spec.py +25 -0
- tests/test_crca_core_timeseries_pcmci.py +15 -0
- tests/test_crca_llm_coauthor.py +12 -0
- tests/test_crca_llm_orchestrator.py +80 -0
- tests/test_hybrid_agent_llm_enhanced.py +556 -0
- tests/test_image_annotation_demo.py +376 -0
- tests/test_image_annotation_operational.py +408 -0
- tests/test_image_annotation_unit.py +551 -0
- tests/test_training_moe.py +13 -0
- training/__init__.py +42 -0
- training/datasets.py +140 -0
- training/deepspeed_zero2_0_5b.json +22 -0
- training/deepspeed_zero2_1_5b.json +22 -0
- training/deepspeed_zero3_0_5b.json +28 -0
- training/deepspeed_zero3_14b.json +28 -0
- training/deepspeed_zero3_h100_3gpu.json +20 -0
- training/deepspeed_zero3_offload.json +28 -0
- training/eval.py +92 -0
- training/finetune.py +516 -0
- training/public_datasets.py +89 -0
- training_data/react_train.jsonl +7473 -0
- utils/agent_discovery.py +311 -0
- utils/batch_processor.py +317 -0
- utils/conversation.py +78 -0
- utils/edit_distance.py +118 -0
- utils/formatter.py +33 -0
- utils/graph_reasoner.py +530 -0
- utils/rate_limiter.py +283 -0
- utils/router.py +2 -2
- utils/tool_discovery.py +307 -0
- webui/__init__.py +10 -0
- webui/app.py +229 -0
- webui/config.py +104 -0
- webui/static/css/style.css +332 -0
- webui/static/js/main.js +284 -0
- webui/templates/index.html +42 -0
- tests/test_crca_excel.py +0 -166
- tests/test_data_broker.py +0 -424
- tests/test_palantir.py +0 -349
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Consistency Guarantee Engine with Deterministic Semantics and State Persistence.
|
|
3
|
+
|
|
4
|
+
Implements deterministic computation with functional purity, seeded RNG,
|
|
5
|
+
immutable state snapshots, and exact replay capability.
|
|
6
|
+
|
|
7
|
+
Theoretical Basis:
|
|
8
|
+
- Deterministic Algorithms
|
|
9
|
+
- Functional Programming
|
|
10
|
+
- Immutable Data Structures (Okasaki 1998)
|
|
11
|
+
- State Monads
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from typing import Dict, List, Optional, Tuple, Any, Set
|
|
15
|
+
from collections import defaultdict
|
|
16
|
+
import logging
|
|
17
|
+
import copy
|
|
18
|
+
import time
|
|
19
|
+
import hashlib
|
|
20
|
+
|
|
21
|
+
logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
# Try to import numpy
|
|
24
|
+
try:
|
|
25
|
+
import numpy as np
|
|
26
|
+
NUMPY_AVAILABLE = True
|
|
27
|
+
except ImportError:
|
|
28
|
+
NUMPY_AVAILABLE = False
|
|
29
|
+
np = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class DeterministicProcessor:
|
|
33
|
+
"""
|
|
34
|
+
Implements deterministic computation with functional purity.
|
|
35
|
+
|
|
36
|
+
Features:
|
|
37
|
+
- Functional purity: f(x) = y always (no side effects)
|
|
38
|
+
- Deterministic algorithms with O-notation guarantees
|
|
39
|
+
- Seeded RNG for reproducibility
|
|
40
|
+
- Mathematical guarantee: same input + same seed → same output
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, seed: int = 42):
|
|
44
|
+
"""
|
|
45
|
+
Initialize deterministic processor.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
seed: Random seed for reproducibility
|
|
49
|
+
"""
|
|
50
|
+
self.seed = seed
|
|
51
|
+
self._rng = self._create_rng(seed)
|
|
52
|
+
self.operation_history: List[Dict[str, Any]] = []
|
|
53
|
+
|
|
54
|
+
def _create_rng(self, seed: int):
|
|
55
|
+
"""Create seeded random number generator."""
|
|
56
|
+
if NUMPY_AVAILABLE:
|
|
57
|
+
return np.random.default_rng(seed)
|
|
58
|
+
else:
|
|
59
|
+
# Fallback to Python's random with seed
|
|
60
|
+
import random
|
|
61
|
+
random.seed(seed)
|
|
62
|
+
return random
|
|
63
|
+
|
|
64
|
+
def process_deterministic(
|
|
65
|
+
self,
|
|
66
|
+
input_data: Any,
|
|
67
|
+
operations: List[callable],
|
|
68
|
+
seed: Optional[int] = None
|
|
69
|
+
) -> Any:
|
|
70
|
+
"""
|
|
71
|
+
Process input deterministically.
|
|
72
|
+
|
|
73
|
+
Algorithm:
|
|
74
|
+
function process_deterministic(input, seed):
|
|
75
|
+
rng = seeded_rng(seed)
|
|
76
|
+
state = initial_state()
|
|
77
|
+
for operation in operations:
|
|
78
|
+
state = apply_operation(state, operation, rng)
|
|
79
|
+
return state
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
input_data: Input data
|
|
83
|
+
operations: List of operations to apply
|
|
84
|
+
seed: Optional seed (uses instance seed if None)
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Processed result
|
|
88
|
+
"""
|
|
89
|
+
if seed is not None:
|
|
90
|
+
rng = self._create_rng(seed)
|
|
91
|
+
else:
|
|
92
|
+
rng = self._rng
|
|
93
|
+
|
|
94
|
+
state = input_data
|
|
95
|
+
|
|
96
|
+
for i, operation in enumerate(operations):
|
|
97
|
+
try:
|
|
98
|
+
# Apply operation (assumed to be pure function)
|
|
99
|
+
if callable(operation):
|
|
100
|
+
state = operation(state, rng)
|
|
101
|
+
else:
|
|
102
|
+
logger.warning(f"Operation {i} is not callable")
|
|
103
|
+
|
|
104
|
+
# Record operation
|
|
105
|
+
self.operation_history.append({
|
|
106
|
+
'operation': str(operation),
|
|
107
|
+
'input': str(state)[:100], # Truncate for logging
|
|
108
|
+
'timestamp': time.time()
|
|
109
|
+
})
|
|
110
|
+
except Exception as e:
|
|
111
|
+
logger.error(f"Operation {i} failed: {e}")
|
|
112
|
+
raise
|
|
113
|
+
|
|
114
|
+
return state
|
|
115
|
+
|
|
116
|
+
def get_random(self, min_val: float = 0.0, max_val: float = 1.0) -> float:
|
|
117
|
+
"""
|
|
118
|
+
Get deterministic random number.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
min_val: Minimum value
|
|
122
|
+
max_val: Maximum value
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
Random number in [min_val, max_val]
|
|
126
|
+
"""
|
|
127
|
+
if NUMPY_AVAILABLE:
|
|
128
|
+
return float(self._rng.uniform(min_val, max_val))
|
|
129
|
+
else:
|
|
130
|
+
return min_val + (max_val - min_val) * self._rng.random()
|
|
131
|
+
|
|
132
|
+
def reset_seed(self, seed: int) -> None:
|
|
133
|
+
"""
|
|
134
|
+
Reset random seed.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
seed: New seed
|
|
138
|
+
"""
|
|
139
|
+
self.seed = seed
|
|
140
|
+
self._rng = self._create_rng(seed)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class StateSnapshot:
|
|
144
|
+
"""
|
|
145
|
+
Implements immutable state management with copy-on-write.
|
|
146
|
+
|
|
147
|
+
Features:
|
|
148
|
+
- Graph state persistence as immutable snapshots
|
|
149
|
+
- Exact replay capability
|
|
150
|
+
- O(1) snapshot creation using copy-on-write
|
|
151
|
+
- Debugging support with step-by-step replay
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
def __init__(self):
|
|
155
|
+
"""Initialize state snapshot manager."""
|
|
156
|
+
self.snapshots: Dict[str, Dict[str, Any]] = {}
|
|
157
|
+
self.snapshot_history: List[Tuple[str, float]] = [] # (snapshot_id, timestamp)
|
|
158
|
+
|
|
159
|
+
def snapshot(
|
|
160
|
+
self,
|
|
161
|
+
state: Dict[str, Any],
|
|
162
|
+
snapshot_id: Optional[str] = None
|
|
163
|
+
) -> str:
|
|
164
|
+
"""
|
|
165
|
+
Create immutable snapshot of state.
|
|
166
|
+
|
|
167
|
+
Uses copy-on-write for O(1) snapshot creation.
|
|
168
|
+
|
|
169
|
+
Algorithm:
|
|
170
|
+
function snapshot(state):
|
|
171
|
+
return immutable_copy(state) // O(1) with copy-on-write
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
state: State dictionary to snapshot
|
|
175
|
+
snapshot_id: Optional snapshot ID (generated if None)
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
Snapshot ID
|
|
179
|
+
"""
|
|
180
|
+
if snapshot_id is None:
|
|
181
|
+
snapshot_id = f"snapshot_{int(time.time() * 1000)}_{hash(str(state)) % 10000}"
|
|
182
|
+
|
|
183
|
+
# Create deep copy (immutable snapshot)
|
|
184
|
+
# In full implementation, would use persistent data structures
|
|
185
|
+
snapshot = self._deep_copy(state)
|
|
186
|
+
|
|
187
|
+
self.snapshots[snapshot_id] = snapshot
|
|
188
|
+
self.snapshot_history.append((snapshot_id, time.time()))
|
|
189
|
+
|
|
190
|
+
return snapshot_id
|
|
191
|
+
|
|
192
|
+
def _deep_copy(self, obj: Any) -> Any:
|
|
193
|
+
"""
|
|
194
|
+
Create deep copy of object.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
obj: Object to copy
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
Deep copy
|
|
201
|
+
"""
|
|
202
|
+
# Use copy.deepcopy for full deep copy
|
|
203
|
+
return copy.deepcopy(obj)
|
|
204
|
+
|
|
205
|
+
def get_snapshot(self, snapshot_id: str) -> Optional[Dict[str, Any]]:
|
|
206
|
+
"""
|
|
207
|
+
Get snapshot by ID.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
snapshot_id: Snapshot ID
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
Snapshot state or None if not found
|
|
214
|
+
"""
|
|
215
|
+
return self.snapshots.get(snapshot_id)
|
|
216
|
+
|
|
217
|
+
def replay(
|
|
218
|
+
self,
|
|
219
|
+
snapshot_id: str,
|
|
220
|
+
operations: List[callable],
|
|
221
|
+
seed: int,
|
|
222
|
+
processor: DeterministicProcessor
|
|
223
|
+
) -> Any:
|
|
224
|
+
"""
|
|
225
|
+
Replay computation from snapshot.
|
|
226
|
+
|
|
227
|
+
Algorithm:
|
|
228
|
+
function replay(snapshot, operations, seed):
|
|
229
|
+
return process_deterministic(snapshot, seed)
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
snapshot_id: Snapshot ID to replay from
|
|
233
|
+
operations: List of operations to replay
|
|
234
|
+
seed: Random seed
|
|
235
|
+
processor: DeterministicProcessor instance
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
Replay result
|
|
239
|
+
"""
|
|
240
|
+
snapshot = self.get_snapshot(snapshot_id)
|
|
241
|
+
if snapshot is None:
|
|
242
|
+
raise ValueError(f"Snapshot {snapshot_id} not found")
|
|
243
|
+
|
|
244
|
+
# Create new processor with same seed for exact replay
|
|
245
|
+
replay_processor = DeterministicProcessor(seed)
|
|
246
|
+
|
|
247
|
+
return replay_processor.process_deterministic(snapshot, operations, seed)
|
|
248
|
+
|
|
249
|
+
def list_snapshots(self) -> List[Tuple[str, float]]:
|
|
250
|
+
"""
|
|
251
|
+
List all snapshots with timestamps.
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
List of (snapshot_id, timestamp) tuples
|
|
255
|
+
"""
|
|
256
|
+
return self.snapshot_history.copy()
|
|
257
|
+
|
|
258
|
+
def get_snapshot_hash(self, snapshot_id: str) -> Optional[str]:
|
|
259
|
+
"""
|
|
260
|
+
Get hash of snapshot for verification.
|
|
261
|
+
|
|
262
|
+
Args:
|
|
263
|
+
snapshot_id: Snapshot ID
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
Hash string or None if not found
|
|
267
|
+
"""
|
|
268
|
+
snapshot = self.get_snapshot(snapshot_id)
|
|
269
|
+
if snapshot is None:
|
|
270
|
+
return None
|
|
271
|
+
|
|
272
|
+
# Compute hash of snapshot
|
|
273
|
+
snapshot_str = str(snapshot)
|
|
274
|
+
return hashlib.md5(snapshot_str.encode()).hexdigest()
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
class ConsistencyEngine:
|
|
278
|
+
"""
|
|
279
|
+
Main consistency engine combining deterministic processing and state snapshots.
|
|
280
|
+
|
|
281
|
+
Provides:
|
|
282
|
+
- Deterministic computation guarantees
|
|
283
|
+
- State persistence and replay
|
|
284
|
+
- Reproducibility verification
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
def __init__(self, seed: int = 42):
|
|
288
|
+
"""
|
|
289
|
+
Initialize consistency engine.
|
|
290
|
+
|
|
291
|
+
Args:
|
|
292
|
+
seed: Random seed
|
|
293
|
+
"""
|
|
294
|
+
self.processor = DeterministicProcessor(seed)
|
|
295
|
+
self.snapshot_manager = StateSnapshot()
|
|
296
|
+
self.seed = seed
|
|
297
|
+
|
|
298
|
+
def process_with_snapshots(
|
|
299
|
+
self,
|
|
300
|
+
initial_state: Dict[str, Any],
|
|
301
|
+
operations: List[callable],
|
|
302
|
+
create_snapshots: bool = True
|
|
303
|
+
) -> Tuple[Any, List[str]]:
|
|
304
|
+
"""
|
|
305
|
+
Process state with automatic snapshots.
|
|
306
|
+
|
|
307
|
+
Args:
|
|
308
|
+
initial_state: Initial state
|
|
309
|
+
operations: List of operations
|
|
310
|
+
create_snapshots: Whether to create snapshots
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
Tuple of (final_result, snapshot_ids)
|
|
314
|
+
"""
|
|
315
|
+
snapshot_ids = []
|
|
316
|
+
|
|
317
|
+
# Create initial snapshot
|
|
318
|
+
if create_snapshots:
|
|
319
|
+
snapshot_id = self.snapshot_manager.snapshot(initial_state, "initial")
|
|
320
|
+
snapshot_ids.append(snapshot_id)
|
|
321
|
+
|
|
322
|
+
# Process with snapshots at each step
|
|
323
|
+
state = initial_state
|
|
324
|
+
for i, operation in enumerate(operations):
|
|
325
|
+
state = self.processor.process_deterministic(state, [operation])
|
|
326
|
+
|
|
327
|
+
if create_snapshots:
|
|
328
|
+
snapshot_id = self.snapshot_manager.snapshot(state, f"step_{i}")
|
|
329
|
+
snapshot_ids.append(snapshot_id)
|
|
330
|
+
|
|
331
|
+
return state, snapshot_ids
|
|
332
|
+
|
|
333
|
+
def verify_reproducibility(
|
|
334
|
+
self,
|
|
335
|
+
snapshot_id: str,
|
|
336
|
+
operations: List[callable],
|
|
337
|
+
num_runs: int = 3
|
|
338
|
+
) -> bool:
|
|
339
|
+
"""
|
|
340
|
+
Verify reproducibility by running multiple times.
|
|
341
|
+
|
|
342
|
+
Args:
|
|
343
|
+
snapshot_id: Snapshot ID to start from
|
|
344
|
+
operations: Operations to run
|
|
345
|
+
num_runs: Number of runs to verify
|
|
346
|
+
|
|
347
|
+
Returns:
|
|
348
|
+
True if all runs produce same result
|
|
349
|
+
"""
|
|
350
|
+
results = []
|
|
351
|
+
|
|
352
|
+
for i in range(num_runs):
|
|
353
|
+
result = self.snapshot_manager.replay(
|
|
354
|
+
snapshot_id,
|
|
355
|
+
operations,
|
|
356
|
+
self.seed,
|
|
357
|
+
self.processor
|
|
358
|
+
)
|
|
359
|
+
results.append(result)
|
|
360
|
+
|
|
361
|
+
# Check if all results are identical
|
|
362
|
+
return all(str(r) == str(results[0]) for r in results)
|