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,322 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Chain-of-Thought Reasoning Engine with Natural Deduction and Proof Trees.
|
|
3
|
+
|
|
4
|
+
Implements explicit inference chains with backtracking, memoization,
|
|
5
|
+
and type-safe operations for complete reasoning traceability.
|
|
6
|
+
|
|
7
|
+
Theoretical Basis:
|
|
8
|
+
- Natural Deduction (Gentzen 1934)
|
|
9
|
+
- Proof Theory
|
|
10
|
+
- Inference Graphs (Richardson & Domingos 2006)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from typing import Dict, List, Optional, Tuple, Any, Set
|
|
14
|
+
from collections import defaultdict
|
|
15
|
+
import logging
|
|
16
|
+
import time
|
|
17
|
+
import uuid
|
|
18
|
+
|
|
19
|
+
from schemas.reasoning import (
|
|
20
|
+
ReasoningStep, ReasoningChain, InferenceRule, StepType, Evidence
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ReasoningTracker:
|
|
27
|
+
"""
|
|
28
|
+
Tracks reasoning chains with proof trees and backtracking support.
|
|
29
|
+
|
|
30
|
+
Implements natural deduction with:
|
|
31
|
+
- Explicit inference chains
|
|
32
|
+
- Backtracking with memoization (O(b^d) → O(n²))
|
|
33
|
+
- Decision point recording
|
|
34
|
+
- Type-safe operations
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self):
|
|
38
|
+
"""Initialize reasoning tracker."""
|
|
39
|
+
self.chains: Dict[str, ReasoningChain] = {}
|
|
40
|
+
self.current_chain: Optional[ReasoningChain] = None
|
|
41
|
+
self.memoization_cache: Dict[str, Any] = {} # For memoization
|
|
42
|
+
self.decision_history: List[Dict[str, Any]] = []
|
|
43
|
+
|
|
44
|
+
def create_chain(self, chain_id: Optional[str] = None) -> ReasoningChain:
|
|
45
|
+
"""
|
|
46
|
+
Create a new reasoning chain.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
chain_id: Optional chain ID (generated if not provided)
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
Created ReasoningChain
|
|
53
|
+
"""
|
|
54
|
+
if chain_id is None:
|
|
55
|
+
chain_id = f"chain_{int(time.time() * 1000)}_{uuid.uuid4().hex[:8]}"
|
|
56
|
+
|
|
57
|
+
chain = ReasoningChain(chain_id=chain_id)
|
|
58
|
+
self.chains[chain_id] = chain
|
|
59
|
+
self.current_chain = chain
|
|
60
|
+
return chain
|
|
61
|
+
|
|
62
|
+
def add_step(
|
|
63
|
+
self,
|
|
64
|
+
step_type: StepType,
|
|
65
|
+
operation: str,
|
|
66
|
+
input_state: Dict[str, Any],
|
|
67
|
+
output_state: Dict[str, Any],
|
|
68
|
+
inference_rule: Optional[InferenceRule] = None,
|
|
69
|
+
premises: Optional[List[str]] = None,
|
|
70
|
+
conclusion: Optional[Any] = None,
|
|
71
|
+
confidence: float = 1.0,
|
|
72
|
+
uncertainty: Optional[float] = None,
|
|
73
|
+
evidence: Optional[List[Evidence]] = None,
|
|
74
|
+
metadata: Optional[Dict[str, Any]] = None
|
|
75
|
+
) -> ReasoningStep:
|
|
76
|
+
"""
|
|
77
|
+
Add a reasoning step to current chain.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
step_type: Type of reasoning step
|
|
81
|
+
operation: Description of operation
|
|
82
|
+
input_state: Precondition graph state
|
|
83
|
+
output_state: Postcondition graph state
|
|
84
|
+
inference_rule: Inference rule used (if applicable)
|
|
85
|
+
premises: List of premise step IDs
|
|
86
|
+
conclusion: Conclusion reached
|
|
87
|
+
confidence: Confidence in conclusion
|
|
88
|
+
uncertainty: Uncertainty quantification
|
|
89
|
+
evidence: List of evidence
|
|
90
|
+
metadata: Additional metadata
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
Created ReasoningStep
|
|
94
|
+
"""
|
|
95
|
+
if self.current_chain is None:
|
|
96
|
+
self.create_chain()
|
|
97
|
+
|
|
98
|
+
step_id = f"step_{int(time.time() * 1000)}_{uuid.uuid4().hex[:8]}"
|
|
99
|
+
|
|
100
|
+
step = ReasoningStep(
|
|
101
|
+
step_id=step_id,
|
|
102
|
+
step_type=step_type,
|
|
103
|
+
inference_rule=inference_rule,
|
|
104
|
+
input_state=input_state,
|
|
105
|
+
operation=operation,
|
|
106
|
+
output_state=output_state,
|
|
107
|
+
confidence=confidence,
|
|
108
|
+
uncertainty=uncertainty,
|
|
109
|
+
evidence=evidence or [],
|
|
110
|
+
premises=premises or [],
|
|
111
|
+
conclusion=conclusion,
|
|
112
|
+
metadata=metadata or {}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
self.current_chain.add_step(step)
|
|
116
|
+
return step
|
|
117
|
+
|
|
118
|
+
def prove(
|
|
119
|
+
self,
|
|
120
|
+
goal: Any,
|
|
121
|
+
premises: List[Any],
|
|
122
|
+
graph: Dict[str, Any],
|
|
123
|
+
max_depth: int = 10
|
|
124
|
+
) -> Optional[ReasoningChain]:
|
|
125
|
+
"""
|
|
126
|
+
Attempt to prove a goal from premises using natural deduction.
|
|
127
|
+
|
|
128
|
+
Algorithm (Natural Deduction with Causal Rules):
|
|
129
|
+
function prove(goal, premises, graph):
|
|
130
|
+
if goal ∈ premises: return trivial_proof(goal)
|
|
131
|
+
for rule in inference_rules:
|
|
132
|
+
if applicable(rule, premises, graph):
|
|
133
|
+
new_premises = apply(rule, premises, graph)
|
|
134
|
+
subproof = prove(goal, new_premises, graph)
|
|
135
|
+
if subproof.success:
|
|
136
|
+
return compose(rule, subproof)
|
|
137
|
+
return failure
|
|
138
|
+
|
|
139
|
+
Uses memoization to reduce complexity from O(b^d) to O(n²).
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
goal: Goal to prove
|
|
143
|
+
premises: List of premises
|
|
144
|
+
graph: Graph state
|
|
145
|
+
max_depth: Maximum proof depth
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
ReasoningChain if proof succeeds, None otherwise
|
|
149
|
+
"""
|
|
150
|
+
# Check memoization cache
|
|
151
|
+
cache_key = f"prove_{hash(str(goal))}_{hash(str(premises))}"
|
|
152
|
+
if cache_key in self.memoization_cache:
|
|
153
|
+
return self.memoization_cache[cache_key]
|
|
154
|
+
|
|
155
|
+
# Trivial case: goal is in premises
|
|
156
|
+
if goal in premises:
|
|
157
|
+
chain = self.create_chain()
|
|
158
|
+
self.add_step(
|
|
159
|
+
step_type=StepType.INFERENCE,
|
|
160
|
+
operation="trivial_proof",
|
|
161
|
+
input_state={"premises": premises},
|
|
162
|
+
output_state={"conclusion": goal},
|
|
163
|
+
conclusion=goal,
|
|
164
|
+
confidence=1.0
|
|
165
|
+
)
|
|
166
|
+
chain.success = True
|
|
167
|
+
chain.final_conclusion = goal
|
|
168
|
+
self.memoization_cache[cache_key] = chain
|
|
169
|
+
return chain
|
|
170
|
+
|
|
171
|
+
# Try inference rules
|
|
172
|
+
if max_depth <= 0:
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
# Create chain for this proof attempt
|
|
176
|
+
chain = self.create_chain()
|
|
177
|
+
|
|
178
|
+
# Try causal inference rule
|
|
179
|
+
if self._try_causal_inference(goal, premises, graph, chain, max_depth - 1):
|
|
180
|
+
chain.success = True
|
|
181
|
+
chain.final_conclusion = goal
|
|
182
|
+
self.memoization_cache[cache_key] = chain
|
|
183
|
+
return chain
|
|
184
|
+
|
|
185
|
+
# Try other inference rules (simplified for now)
|
|
186
|
+
# Future: Implement full natural deduction
|
|
187
|
+
|
|
188
|
+
return None
|
|
189
|
+
|
|
190
|
+
def _try_causal_inference(
|
|
191
|
+
self,
|
|
192
|
+
goal: Any,
|
|
193
|
+
premises: List[Any],
|
|
194
|
+
graph: Dict[str, Any],
|
|
195
|
+
chain: ReasoningChain,
|
|
196
|
+
max_depth: int
|
|
197
|
+
) -> bool:
|
|
198
|
+
"""
|
|
199
|
+
Try causal inference rule.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
goal: Goal to prove
|
|
203
|
+
premises: Current premises
|
|
204
|
+
graph: Graph state
|
|
205
|
+
chain: Reasoning chain
|
|
206
|
+
max_depth: Remaining depth
|
|
207
|
+
|
|
208
|
+
Returns:
|
|
209
|
+
True if inference succeeds
|
|
210
|
+
"""
|
|
211
|
+
# Simplified causal inference: if goal is causally reachable from premises
|
|
212
|
+
# In full implementation, would use do-calculus
|
|
213
|
+
|
|
214
|
+
# Check if goal is a variable that can be causally inferred
|
|
215
|
+
if isinstance(goal, str) and 'graph' in graph:
|
|
216
|
+
graph_nodes = graph.get('nodes', [])
|
|
217
|
+
graph_edges = graph.get('edges', [])
|
|
218
|
+
|
|
219
|
+
# Check if goal is reachable from premises via causal paths
|
|
220
|
+
if goal in graph_nodes:
|
|
221
|
+
# Simple check: if any premise variable has a path to goal
|
|
222
|
+
for premise in premises:
|
|
223
|
+
if isinstance(premise, str) and premise in graph_nodes:
|
|
224
|
+
# Check for direct edge
|
|
225
|
+
if (premise, goal) in graph_edges:
|
|
226
|
+
self.add_step(
|
|
227
|
+
step_type=StepType.INFERENCE,
|
|
228
|
+
operation="causal_inference",
|
|
229
|
+
input_state={"premises": premises, "graph": graph},
|
|
230
|
+
output_state={"conclusion": goal},
|
|
231
|
+
inference_rule=InferenceRule.CAUSAL_INFERENCE,
|
|
232
|
+
conclusion=goal,
|
|
233
|
+
confidence=0.8
|
|
234
|
+
)
|
|
235
|
+
return True
|
|
236
|
+
|
|
237
|
+
return False
|
|
238
|
+
|
|
239
|
+
def get_reasoning_path(self, step_id: str) -> List[ReasoningStep]:
|
|
240
|
+
"""
|
|
241
|
+
Get reasoning path to a specific step.
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
step_id: Target step ID
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
List of steps forming path
|
|
248
|
+
"""
|
|
249
|
+
if self.current_chain is None:
|
|
250
|
+
return []
|
|
251
|
+
|
|
252
|
+
return self.current_chain.get_path_to_step(step_id)
|
|
253
|
+
|
|
254
|
+
def validate_chain(self, chain_id: Optional[str] = None) -> Tuple[bool, Optional[str]]:
|
|
255
|
+
"""
|
|
256
|
+
Validate reasoning chain.
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
chain_id: Chain ID (uses current chain if None)
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
Tuple of (is_valid, error_message)
|
|
263
|
+
"""
|
|
264
|
+
chain = self.chains.get(chain_id) if chain_id else self.current_chain
|
|
265
|
+
if chain is None:
|
|
266
|
+
return False, "No chain to validate"
|
|
267
|
+
|
|
268
|
+
return chain.validate_chain()
|
|
269
|
+
|
|
270
|
+
def record_decision_point(
|
|
271
|
+
self,
|
|
272
|
+
decision: str,
|
|
273
|
+
alternatives: List[Any],
|
|
274
|
+
chosen: Any,
|
|
275
|
+
reason: str
|
|
276
|
+
) -> None:
|
|
277
|
+
"""
|
|
278
|
+
Record a decision point with alternatives explored.
|
|
279
|
+
|
|
280
|
+
Args:
|
|
281
|
+
decision: Decision description
|
|
282
|
+
alternatives: List of alternatives considered
|
|
283
|
+
chosen: Chosen alternative
|
|
284
|
+
reason: Reason for choice
|
|
285
|
+
"""
|
|
286
|
+
decision_point = {
|
|
287
|
+
'decision': decision,
|
|
288
|
+
'alternatives': alternatives,
|
|
289
|
+
'chosen': chosen,
|
|
290
|
+
'reason': reason,
|
|
291
|
+
'timestamp': time.time()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
self.decision_history.append(decision_point)
|
|
295
|
+
|
|
296
|
+
if self.current_chain:
|
|
297
|
+
self.current_chain.decision_points.append(decision_point)
|
|
298
|
+
|
|
299
|
+
def get_chain_summary(self, chain_id: Optional[str] = None) -> Dict[str, Any]:
|
|
300
|
+
"""
|
|
301
|
+
Get summary of reasoning chain.
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
chain_id: Chain ID (uses current chain if None)
|
|
305
|
+
|
|
306
|
+
Returns:
|
|
307
|
+
Dictionary with chain summary
|
|
308
|
+
"""
|
|
309
|
+
chain = self.chains.get(chain_id) if chain_id else self.current_chain
|
|
310
|
+
if chain is None:
|
|
311
|
+
return {}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
'chain_id': chain.chain_id,
|
|
315
|
+
'num_steps': len(chain.steps),
|
|
316
|
+
'success': chain.success,
|
|
317
|
+
'final_conclusion': chain.final_conclusion,
|
|
318
|
+
'root_step': chain.root_step,
|
|
319
|
+
'leaf_steps': chain.leaf_steps,
|
|
320
|
+
'decision_points': len(chain.decision_points),
|
|
321
|
+
'alternative_branches': len(chain.alternative_branches)
|
|
322
|
+
}
|