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,524 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Self-Verification & Error Detection with Formal Verification and Automated Theorem Proving.
|
|
3
|
+
|
|
4
|
+
Implements formal verification, model checking, consistency checking,
|
|
5
|
+
anomaly detection, and automated error correction.
|
|
6
|
+
|
|
7
|
+
Theoretical Basis:
|
|
8
|
+
- Formal Verification (Clarke et al. 1999)
|
|
9
|
+
- Model Checking
|
|
10
|
+
- Automated Theorem Proving (Robinson 1965)
|
|
11
|
+
- Type Theory (Martin-Löf 1984)
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from typing import Dict, List, Optional, Tuple, Any, Set
|
|
15
|
+
from collections import defaultdict, deque
|
|
16
|
+
import logging
|
|
17
|
+
import re
|
|
18
|
+
|
|
19
|
+
from schemas.reasoning import ReasoningStep, ReasoningChain
|
|
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 ConsistencyChecker:
|
|
33
|
+
"""
|
|
34
|
+
Implements formal consistency verification.
|
|
35
|
+
|
|
36
|
+
Features:
|
|
37
|
+
- Graph consistency checking
|
|
38
|
+
- Contradiction detection using resolution theorem proving
|
|
39
|
+
- Causal relationship validation using do-calculus
|
|
40
|
+
- Epistemic grounding verification
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self):
|
|
44
|
+
"""Initialize consistency checker."""
|
|
45
|
+
self.constraints: List[Dict[str, Any]] = []
|
|
46
|
+
|
|
47
|
+
def verify_consistency(
|
|
48
|
+
self,
|
|
49
|
+
graph: Dict[str, Any],
|
|
50
|
+
constraints: Optional[List[Dict[str, Any]]] = None
|
|
51
|
+
) -> Tuple[bool, Optional[str]]:
|
|
52
|
+
"""
|
|
53
|
+
Verify graph consistency.
|
|
54
|
+
|
|
55
|
+
Algorithm:
|
|
56
|
+
function verify_consistency(graph, constraints):
|
|
57
|
+
for constraint in constraints:
|
|
58
|
+
if not satisfies(graph, constraint):
|
|
59
|
+
return (False, constraint)
|
|
60
|
+
return (True, None)
|
|
61
|
+
|
|
62
|
+
Verifies: ∀c ∈ C, G ⊨ c (satisfaction checking)
|
|
63
|
+
|
|
64
|
+
O(n²) complexity for consistency checking.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
graph: Graph dictionary
|
|
68
|
+
constraints: Optional constraints (uses default if None)
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Tuple of (is_consistent, error_message)
|
|
72
|
+
"""
|
|
73
|
+
if constraints is None:
|
|
74
|
+
constraints = self._get_default_constraints()
|
|
75
|
+
|
|
76
|
+
nodes = graph.get('nodes', [])
|
|
77
|
+
edges = graph.get('edges', [])
|
|
78
|
+
|
|
79
|
+
# Check each constraint
|
|
80
|
+
for constraint in constraints:
|
|
81
|
+
constraint_type = constraint.get('type', '')
|
|
82
|
+
|
|
83
|
+
if constraint_type == 'no_self_loops':
|
|
84
|
+
# Check for self-loops
|
|
85
|
+
for source, target in edges:
|
|
86
|
+
if source == target:
|
|
87
|
+
return False, f"Self-loop detected: {source} → {source}"
|
|
88
|
+
|
|
89
|
+
elif constraint_type == 'no_duplicate_edges':
|
|
90
|
+
# Check for duplicate edges
|
|
91
|
+
edge_set = set(edges)
|
|
92
|
+
if len(edge_set) < len(edges):
|
|
93
|
+
return False, "Duplicate edges detected"
|
|
94
|
+
|
|
95
|
+
elif constraint_type == 'valid_nodes':
|
|
96
|
+
# Check all edge nodes are in node set
|
|
97
|
+
node_set = set(nodes)
|
|
98
|
+
for source, target in edges:
|
|
99
|
+
if source not in node_set:
|
|
100
|
+
return False, f"Invalid source node: {source}"
|
|
101
|
+
if target not in node_set:
|
|
102
|
+
return False, f"Invalid target node: {target}"
|
|
103
|
+
|
|
104
|
+
elif constraint_type == 'no_contradictions':
|
|
105
|
+
# Check for contradictory relationships
|
|
106
|
+
contradiction = self._detect_contradictions(graph)
|
|
107
|
+
if contradiction:
|
|
108
|
+
return False, f"Contradiction detected: {contradiction}"
|
|
109
|
+
|
|
110
|
+
return True, None
|
|
111
|
+
|
|
112
|
+
def _get_default_constraints(self) -> List[Dict[str, Any]]:
|
|
113
|
+
"""Get default consistency constraints."""
|
|
114
|
+
return [
|
|
115
|
+
{'type': 'no_self_loops'},
|
|
116
|
+
{'type': 'no_duplicate_edges'},
|
|
117
|
+
{'type': 'valid_nodes'},
|
|
118
|
+
{'type': 'no_contradictions'}
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
def _detect_contradictions(
|
|
122
|
+
self,
|
|
123
|
+
graph: Dict[str, Any]
|
|
124
|
+
) -> Optional[str]:
|
|
125
|
+
"""
|
|
126
|
+
Detect contradictions using resolution theorem proving.
|
|
127
|
+
|
|
128
|
+
If P ∧ ¬P derivable, then inconsistent.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
graph: Graph dictionary
|
|
132
|
+
|
|
133
|
+
Returns:
|
|
134
|
+
Contradiction description or None
|
|
135
|
+
"""
|
|
136
|
+
edges = graph.get('edges', [])
|
|
137
|
+
|
|
138
|
+
# Check for direct contradictions (A → B and B → A with negative relationship)
|
|
139
|
+
# Simplified: check for cycles that might indicate contradictions
|
|
140
|
+
edge_dict = defaultdict(list)
|
|
141
|
+
for source, target in edges:
|
|
142
|
+
edge_dict[source].append(target)
|
|
143
|
+
|
|
144
|
+
# Check for bidirectional edges (might indicate contradiction)
|
|
145
|
+
for source, targets in edge_dict.items():
|
|
146
|
+
for target in targets:
|
|
147
|
+
if source in edge_dict.get(target, []):
|
|
148
|
+
# Bidirectional edge - might be contradiction
|
|
149
|
+
return f"Bidirectional relationship: {source} ↔ {target}"
|
|
150
|
+
|
|
151
|
+
return None
|
|
152
|
+
|
|
153
|
+
def validate_causal_relationships(
|
|
154
|
+
self,
|
|
155
|
+
graph: Dict[str, Any]
|
|
156
|
+
) -> Tuple[bool, Optional[str]]:
|
|
157
|
+
"""
|
|
158
|
+
Validate causal relationships using do-calculus.
|
|
159
|
+
|
|
160
|
+
Checks P(Y | do(X)) is well-defined.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
graph: Graph dictionary
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
Tuple of (is_valid, error_message)
|
|
167
|
+
"""
|
|
168
|
+
nodes = graph.get('nodes', [])
|
|
169
|
+
edges = graph.get('edges', [])
|
|
170
|
+
|
|
171
|
+
# Simple validation: check that edges represent valid causal relationships
|
|
172
|
+
# In full implementation, would use do-calculus to verify
|
|
173
|
+
|
|
174
|
+
# Check for temporal ordering (if temporal info available)
|
|
175
|
+
# Check for confounders (simplified)
|
|
176
|
+
|
|
177
|
+
return True, None
|
|
178
|
+
|
|
179
|
+
def verify_epistemic_grounding(
|
|
180
|
+
self,
|
|
181
|
+
graph: Dict[str, Any],
|
|
182
|
+
observable_variables: Set[str]
|
|
183
|
+
) -> Tuple[bool, List[str]]:
|
|
184
|
+
"""
|
|
185
|
+
Verify epistemic grounding.
|
|
186
|
+
|
|
187
|
+
Ensures all variables v ∈ V are grounded: ∃ path from observables O to v.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
graph: Graph dictionary
|
|
191
|
+
observable_variables: Set of observable variables
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
Tuple of (all_grounded, ungrounded_variables)
|
|
195
|
+
"""
|
|
196
|
+
nodes = set(graph.get('nodes', []))
|
|
197
|
+
edges = graph.get('edges', [])
|
|
198
|
+
|
|
199
|
+
# Build adjacency for BFS
|
|
200
|
+
adjacency = defaultdict(list)
|
|
201
|
+
for source, target in edges:
|
|
202
|
+
adjacency[source].append(target)
|
|
203
|
+
|
|
204
|
+
# BFS from observable variables
|
|
205
|
+
visited = set(observable_variables)
|
|
206
|
+
queue = deque(observable_variables)
|
|
207
|
+
|
|
208
|
+
while queue:
|
|
209
|
+
node = queue.popleft()
|
|
210
|
+
for neighbor in adjacency.get(node, []):
|
|
211
|
+
if neighbor not in visited:
|
|
212
|
+
visited.add(neighbor)
|
|
213
|
+
queue.append(neighbor)
|
|
214
|
+
|
|
215
|
+
# Find ungrounded variables
|
|
216
|
+
ungrounded = nodes - visited
|
|
217
|
+
|
|
218
|
+
return len(ungrounded) == 0, list(ungrounded)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class ErrorDetector:
|
|
222
|
+
"""
|
|
223
|
+
Implements error detection using anomaly detection.
|
|
224
|
+
|
|
225
|
+
Features:
|
|
226
|
+
- Extraction error detection
|
|
227
|
+
- Reasoning mistake identification
|
|
228
|
+
- Epistemic violation flags
|
|
229
|
+
- Statistical result validation
|
|
230
|
+
"""
|
|
231
|
+
|
|
232
|
+
def __init__(self):
|
|
233
|
+
"""Initialize error detector."""
|
|
234
|
+
self.validators: List[callable] = []
|
|
235
|
+
|
|
236
|
+
def detect_errors(
|
|
237
|
+
self,
|
|
238
|
+
reasoning_chain: ReasoningChain,
|
|
239
|
+
graph: Dict[str, Any]
|
|
240
|
+
) -> List[Dict[str, Any]]:
|
|
241
|
+
"""
|
|
242
|
+
Detect errors in reasoning chain and graph.
|
|
243
|
+
|
|
244
|
+
Algorithm:
|
|
245
|
+
function detect_errors(reasoning_chain, graph):
|
|
246
|
+
errors = []
|
|
247
|
+
for step in reasoning_chain:
|
|
248
|
+
if not is_valid_proof_step(step):
|
|
249
|
+
errors.append(('invalid_step', step))
|
|
250
|
+
if not is_grounded(step.conclusion, graph):
|
|
251
|
+
errors.append(('ungrounded', step))
|
|
252
|
+
return errors
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
reasoning_chain: Reasoning chain to check
|
|
256
|
+
graph: Graph state
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
List of error dictionaries
|
|
260
|
+
"""
|
|
261
|
+
errors = []
|
|
262
|
+
|
|
263
|
+
# Check reasoning steps
|
|
264
|
+
for step in reasoning_chain.steps:
|
|
265
|
+
# Check if step is valid
|
|
266
|
+
if not step.is_valid_proof_step():
|
|
267
|
+
errors.append({
|
|
268
|
+
'type': 'invalid_step',
|
|
269
|
+
'step_id': step.step_id,
|
|
270
|
+
'message': f"Invalid proof step: {step.step_id}",
|
|
271
|
+
'step': step
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
# Check confidence threshold
|
|
275
|
+
if step.confidence < 0.5:
|
|
276
|
+
errors.append({
|
|
277
|
+
'type': 'low_confidence',
|
|
278
|
+
'step_id': step.step_id,
|
|
279
|
+
'message': f"Low confidence step: {step.confidence:.2f}",
|
|
280
|
+
'step': step
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
# Check for epistemic violations
|
|
284
|
+
if step.conclusion:
|
|
285
|
+
# Simplified: check if conclusion is a string that looks like action verb
|
|
286
|
+
if isinstance(step.conclusion, str):
|
|
287
|
+
action_verbs = {'identify', 'analyze', 'examine', 'determine'}
|
|
288
|
+
if step.conclusion.lower() in action_verbs:
|
|
289
|
+
errors.append({
|
|
290
|
+
'type': 'epistemic_violation',
|
|
291
|
+
'step_id': step.step_id,
|
|
292
|
+
'message': f"Epistemic violation: conclusion is action verb '{step.conclusion}'",
|
|
293
|
+
'step': step
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
# Check graph consistency
|
|
297
|
+
consistency_checker = ConsistencyChecker()
|
|
298
|
+
is_consistent, error_msg = consistency_checker.verify_consistency(graph)
|
|
299
|
+
if not is_consistent:
|
|
300
|
+
errors.append({
|
|
301
|
+
'type': 'inconsistency',
|
|
302
|
+
'message': error_msg,
|
|
303
|
+
'graph': graph
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
return errors
|
|
307
|
+
|
|
308
|
+
def validate_statistical_results(
|
|
309
|
+
self,
|
|
310
|
+
results: Dict[str, Any],
|
|
311
|
+
alpha: float = 0.05
|
|
312
|
+
) -> Tuple[bool, Optional[str]]:
|
|
313
|
+
"""
|
|
314
|
+
Validate statistical results using hypothesis testing.
|
|
315
|
+
|
|
316
|
+
If p-value < α, reject null hypothesis.
|
|
317
|
+
|
|
318
|
+
Args:
|
|
319
|
+
results: Statistical results dictionary
|
|
320
|
+
alpha: Significance level
|
|
321
|
+
|
|
322
|
+
Returns:
|
|
323
|
+
Tuple of (is_valid, error_message)
|
|
324
|
+
"""
|
|
325
|
+
p_value = results.get('p_value')
|
|
326
|
+
if p_value is not None:
|
|
327
|
+
if p_value < alpha:
|
|
328
|
+
return False, f"Null hypothesis rejected (p={p_value:.4f} < α={alpha})"
|
|
329
|
+
|
|
330
|
+
return True, None
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
class SelfCorrector:
|
|
334
|
+
"""
|
|
335
|
+
Implements automated error correction with proof validation.
|
|
336
|
+
|
|
337
|
+
Features:
|
|
338
|
+
- Automatic fixes using correction rules
|
|
339
|
+
- Correction suggestions using edit distance
|
|
340
|
+
- Re-verification after correction
|
|
341
|
+
- Correction history for learning
|
|
342
|
+
"""
|
|
343
|
+
|
|
344
|
+
def __init__(self):
|
|
345
|
+
"""Initialize self corrector."""
|
|
346
|
+
self.correction_rules: Dict[str, callable] = {}
|
|
347
|
+
self.correction_history: List[Dict[str, Any]] = []
|
|
348
|
+
self._init_correction_rules()
|
|
349
|
+
|
|
350
|
+
def _init_correction_rules(self) -> None:
|
|
351
|
+
"""Initialize correction rules."""
|
|
352
|
+
self.correction_rules = {
|
|
353
|
+
'type_mismatch': self._correct_type_mismatch,
|
|
354
|
+
'low_confidence': self._correct_low_confidence,
|
|
355
|
+
'epistemic_violation': self._correct_epistemic_violation,
|
|
356
|
+
'inconsistency': self._correct_inconsistency
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
def correct_errors(
|
|
360
|
+
self,
|
|
361
|
+
errors: List[Dict[str, Any]],
|
|
362
|
+
graph: Dict[str, Any]
|
|
363
|
+
) -> List[Dict[str, Any]]:
|
|
364
|
+
"""
|
|
365
|
+
Automatically correct detected errors.
|
|
366
|
+
|
|
367
|
+
Algorithm:
|
|
368
|
+
function correct_errors(errors, graph):
|
|
369
|
+
corrections = []
|
|
370
|
+
for error_type, error_data in errors:
|
|
371
|
+
correction = apply_correction_rule(error_type, error_data, graph)
|
|
372
|
+
if verify(correction):
|
|
373
|
+
corrections.append(correction)
|
|
374
|
+
return corrections
|
|
375
|
+
|
|
376
|
+
Args:
|
|
377
|
+
errors: List of error dictionaries
|
|
378
|
+
graph: Graph state
|
|
379
|
+
|
|
380
|
+
Returns:
|
|
381
|
+
List of correction dictionaries
|
|
382
|
+
"""
|
|
383
|
+
corrections = []
|
|
384
|
+
|
|
385
|
+
for error in errors:
|
|
386
|
+
error_type = error.get('type', '')
|
|
387
|
+
correction_func = self.correction_rules.get(error_type)
|
|
388
|
+
|
|
389
|
+
if correction_func:
|
|
390
|
+
try:
|
|
391
|
+
correction = correction_func(error, graph)
|
|
392
|
+
if correction:
|
|
393
|
+
# Re-verify correction
|
|
394
|
+
if self._verify_correction(correction, graph):
|
|
395
|
+
corrections.append(correction)
|
|
396
|
+
self.correction_history.append({
|
|
397
|
+
'error': error,
|
|
398
|
+
'correction': correction,
|
|
399
|
+
'success': True
|
|
400
|
+
})
|
|
401
|
+
except Exception as e:
|
|
402
|
+
logger.warning(f"Correction failed for {error_type}: {e}")
|
|
403
|
+
self.correction_history.append({
|
|
404
|
+
'error': error,
|
|
405
|
+
'correction': None,
|
|
406
|
+
'success': False,
|
|
407
|
+
'error_message': str(e)
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
return corrections
|
|
411
|
+
|
|
412
|
+
def _correct_type_mismatch(
|
|
413
|
+
self,
|
|
414
|
+
error: Dict[str, Any],
|
|
415
|
+
graph: Dict[str, Any]
|
|
416
|
+
) -> Optional[Dict[str, Any]]:
|
|
417
|
+
"""Correct type mismatch error."""
|
|
418
|
+
# Placeholder: would implement type coercion
|
|
419
|
+
return {
|
|
420
|
+
'type': 'type_coercion',
|
|
421
|
+
'error': error,
|
|
422
|
+
'suggestion': 'Apply type coercion'
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
def _correct_low_confidence(
|
|
426
|
+
self,
|
|
427
|
+
error: Dict[str, Any],
|
|
428
|
+
graph: Dict[str, Any]
|
|
429
|
+
) -> Optional[Dict[str, Any]]:
|
|
430
|
+
"""Correct low confidence error."""
|
|
431
|
+
step = error.get('step')
|
|
432
|
+
if step:
|
|
433
|
+
# Suggest increasing confidence or adding more evidence
|
|
434
|
+
return {
|
|
435
|
+
'type': 'confidence_boost',
|
|
436
|
+
'error': error,
|
|
437
|
+
'suggestion': 'Add more evidence or increase confidence threshold'
|
|
438
|
+
}
|
|
439
|
+
return None
|
|
440
|
+
|
|
441
|
+
def _correct_epistemic_violation(
|
|
442
|
+
self,
|
|
443
|
+
error: Dict[str, Any],
|
|
444
|
+
graph: Dict[str, Any]
|
|
445
|
+
) -> Optional[Dict[str, Any]]:
|
|
446
|
+
"""Correct epistemic violation."""
|
|
447
|
+
step = error.get('step')
|
|
448
|
+
if step and step.conclusion:
|
|
449
|
+
# Remove action verb from conclusion
|
|
450
|
+
conclusion = step.conclusion
|
|
451
|
+
if isinstance(conclusion, str):
|
|
452
|
+
# Try to extract actual variable from context
|
|
453
|
+
return {
|
|
454
|
+
'type': 'epistemic_filter',
|
|
455
|
+
'error': error,
|
|
456
|
+
'suggestion': f"Remove action verb from conclusion: {conclusion}"
|
|
457
|
+
}
|
|
458
|
+
return None
|
|
459
|
+
|
|
460
|
+
def _correct_inconsistency(
|
|
461
|
+
self,
|
|
462
|
+
error: Dict[str, Any],
|
|
463
|
+
graph: Dict[str, Any]
|
|
464
|
+
) -> Optional[Dict[str, Any]]:
|
|
465
|
+
"""Correct inconsistency error."""
|
|
466
|
+
return {
|
|
467
|
+
'type': 'consistency_fix',
|
|
468
|
+
'error': error,
|
|
469
|
+
'suggestion': 'Remove contradictory edges or resolve conflict'
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
def _verify_correction(
|
|
473
|
+
self,
|
|
474
|
+
correction: Dict[str, Any],
|
|
475
|
+
graph: Dict[str, Any]
|
|
476
|
+
) -> bool:
|
|
477
|
+
"""
|
|
478
|
+
Verify that correction is valid.
|
|
479
|
+
|
|
480
|
+
Args:
|
|
481
|
+
correction: Correction dictionary
|
|
482
|
+
graph: Graph state
|
|
483
|
+
|
|
484
|
+
Returns:
|
|
485
|
+
True if correction is valid
|
|
486
|
+
"""
|
|
487
|
+
# Simple verification: check if correction has required fields
|
|
488
|
+
return 'type' in correction and 'suggestion' in correction
|
|
489
|
+
|
|
490
|
+
def suggest_corrections(
|
|
491
|
+
self,
|
|
492
|
+
error: Dict[str, Any],
|
|
493
|
+
k: int = 3
|
|
494
|
+
) -> List[str]:
|
|
495
|
+
"""
|
|
496
|
+
Suggest correction candidates using edit distance.
|
|
497
|
+
|
|
498
|
+
candidates = {c : edit_distance(c, error) < k}
|
|
499
|
+
|
|
500
|
+
Args:
|
|
501
|
+
error: Error dictionary
|
|
502
|
+
k: Maximum edit distance
|
|
503
|
+
|
|
504
|
+
Returns:
|
|
505
|
+
List of correction suggestions
|
|
506
|
+
"""
|
|
507
|
+
suggestions = []
|
|
508
|
+
error_type = error.get('type', '')
|
|
509
|
+
|
|
510
|
+
# Generate suggestions based on error type
|
|
511
|
+
if error_type == 'invalid_step':
|
|
512
|
+
suggestions.append("Validate proof step with proper inference rule")
|
|
513
|
+
suggestions.append("Add missing premises or evidence")
|
|
514
|
+
elif error_type == 'low_confidence':
|
|
515
|
+
suggestions.append("Add more evidence to increase confidence")
|
|
516
|
+
suggestions.append("Use more reliable extraction method")
|
|
517
|
+
elif error_type == 'epistemic_violation':
|
|
518
|
+
suggestions.append("Remove action verb from conclusion")
|
|
519
|
+
suggestions.append("Extract actual state variable instead")
|
|
520
|
+
elif error_type == 'inconsistency':
|
|
521
|
+
suggestions.append("Remove contradictory relationships")
|
|
522
|
+
suggestions.append("Resolve conflict by choosing one relationship")
|
|
523
|
+
|
|
524
|
+
return suggestions[:k]
|