File size: 41,590 Bytes
c5828bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 | """
ReasoningGraph - Recursive Cognitive Architecture
This module implements the core reasoning graph architecture that powers
the recursive cognition capabilities of AGI-HEDGE-FUND agents.
Key components:
- LangGraph-based reasoning networks
- Recursive thought paths with configurable depth
- Attribution tracing throughout reasoning chains
- Symbolic state propagation across reasoning steps
- Failsafe mechanisms for reasoning collapse
Internal Note: This implementation is inspired by circuit interpretability research,
simulating recursive attention pathways via LangGraph. The core patterns
encode .p/ command equivalents like reflect.trace and collapse.detect.
"""
import datetime
import uuid
import logging
from typing import Dict, List, Any, Optional, Callable, Tuple, Union, Set
import numpy as np
# LangGraph for reasoning graphs
from langgraph.graph import StateGraph, END
from langgraph.prebuilt import ToolNode, ToolExecutor
# For type hints
from pydantic import BaseModel, Field
# Internal imports
from ..llm.router import ModelRouter
from ..utils.diagnostics import TracingTools
class ReasoningState(BaseModel):
"""Reasoning state carried through graph execution."""
# Input data and context
input: Dict[str, Any] = Field(default_factory=dict)
context: Dict[str, Any] = Field(default_factory=dict)
# Reasoning chain and attribution
steps: List[Dict[str, Any]] = Field(default_factory=list)
attribution: Dict[str, Dict[str, float]] = Field(default_factory=dict)
# Execution metadata
depth: int = Field(default=0)
max_depth: int = Field(default=3)
start_time: datetime.datetime = Field(default_factory=datetime.datetime.now)
elapsed_ms: int = Field(default=0)
# Results and conclusions
output: Dict[str, Any] = Field(default_factory=dict)
confidence: float = Field(default=0.5)
# Error handling and diagnostics
errors: List[Dict[str, Any]] = Field(default_factory=list)
warnings: List[Dict[str, Any]] = Field(default_factory=list)
# Collapse detection
collapse_risk: float = Field(default=0.0)
collapse_detected: bool = Field(default=False)
collapse_reason: Optional[str] = Field(default=None)
# Tracing
trace_enabled: bool = Field(default=False)
trace_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
class ReasoningGraph:
"""
Implements a recursive reasoning architecture for agent cognition.
The ReasoningGraph enables:
- Multi-step reasoning via connected nodes
- Recursive thought patterns for deep analysis
- Attribution tracing across reasoning chains
- Dynamic graph reconfiguration based on context
- Safeguards against reasoning collapse (infinite loops, contradictions)
"""
def __init__(
self,
agent_name: str,
agent_philosophy: str,
model_router: ModelRouter,
collapse_threshold: float = 0.8,
trace_enabled: bool = False,
):
"""
Initialize reasoning graph.
Args:
agent_name: Name of the agent using this reasoning graph
agent_philosophy: Philosophy description of the agent
model_router: ModelRouter instance for LLM access
collapse_threshold: Threshold for reasoning collapse detection
trace_enabled: Whether to trace reasoning steps
"""
self.agent_name = agent_name
self.agent_philosophy = agent_philosophy
self.model_router = model_router
self.collapse_threshold = collapse_threshold
self.trace_enabled = trace_enabled
# Initialize graph components
self.nodes: Dict[str, Callable] = {}
self.edges: Dict[str, List[str]] = {}
self.entry_point: Optional[str] = None
# Diagnostic tooling
self.tracer = TracingTools(agent_id=str(uuid.uuid4()), agent_name=agent_name)
# Add base nodes
self._add_base_nodes()
# Build initial graph
self._build_graph()
def _add_base_nodes(self) -> None:
"""Add base reasoning nodes that are part of every graph."""
# Entry point for validation
self.add_node("validate_input", self._validate_input)
# Default nodes
self.add_node("initial_analysis", self._initial_analysis)
self.add_node("extract_themes", self._extract_themes)
self.add_node("generate_conclusions", self._generate_conclusions)
# Exit handlers
self.add_node("check_collapse", self._check_collapse)
self.add_node("finalize_output", self._finalize_output)
def _build_graph(self) -> None:
"""Build the reasoning graph based on defined nodes and edges."""
# Set default entry point if not specified
if self.entry_point is None:
if "validate_input" in self.nodes:
self.entry_point = "validate_input"
elif len(self.nodes) > 0:
self.entry_point = list(self.nodes.keys())[0]
else:
raise ValueError("Cannot build graph: no nodes defined")
# Build default edges if none defined
if not self.edges:
# Get sorted node names (for deterministic graphs)
node_names = sorted(self.nodes.keys())
# Find entry point index
try:
entry_index = node_names.index(self.entry_point)
except ValueError:
entry_index = 0
# Move entry point to front
if entry_index > 0:
node_names.insert(0, node_names.pop(entry_index))
# Create sequential edges between all nodes
for i in range(len(node_names) - 1):
self.add_edge(node_names[i], node_names[i + 1])
# Add exit handler edges
if "check_collapse" in self.nodes and "check_collapse" not in node_names:
self.add_edge(node_names[-1], "check_collapse")
if "finalize_output" in self.nodes:
self.add_edge("check_collapse", "finalize_output")
elif "finalize_output" in self.nodes and "finalize_output" not in node_names:
self.add_edge(node_names[-1], "finalize_output")
def add_node(self, name: str, fn: Callable) -> None:
"""
Add a reasoning node to the graph.
Args:
name: Node name
fn: Function to execute for this node
"""
self.nodes[name] = fn
def add_edge(self, source: str, target: str) -> None:
"""
Add an edge between reasoning nodes.
Args:
source: Source node name
target: Target node name
"""
if source not in self.nodes:
raise ValueError(f"Source node '{source}' does not exist")
if target not in self.nodes:
raise ValueError(f"Target node '{target}' does not exist")
if source not in self.edges:
self.edges[source] = []
if target not in self.edges[source]:
self.edges[source].append(target)
def set_entry_point(self, node_name: str) -> None:
"""
Set the entry point for the reasoning graph.
Args:
node_name: Name of entry node
"""
if node_name not in self.nodes:
raise ValueError(f"Entry node '{node_name}' does not exist")
self.entry_point = node_name
def run(self, input: Dict[str, Any], trace_depth: int = 3) -> Dict[str, Any]:
"""
Run the reasoning graph on input data.
Args:
input: Input data
trace_depth: Depth of reasoning trace (higher = deeper reasoning)
Returns:
Reasoning results
"""
# Prepare initial state
state = ReasoningState(
input=input,
max_depth=trace_depth,
trace_enabled=self.trace_enabled
)
# Define next node function
def get_next_node(state_dict: Dict[str, Any], current_node: str) -> Union[str, List[str]]:
# Convert state dict back to ReasoningState
state = ReasoningState.parse_obj(state_dict)
# Check for collapse detection
if state.collapse_detected:
if "finalize_output" in self.nodes:
return "finalize_output"
return END
# Check for max depth reached
if state.depth >= state.max_depth and current_node != "check_collapse" and "check_collapse" in self.nodes:
return "check_collapse"
# Check if we've reached a terminal node
if current_node == "finalize_output" or current_node not in self.edges:
return END
# Return next nodes
return self.edges[current_node]
# Create StateGraph
workflow = StateGraph(ReasoningState)
# Add nodes
for node_name, node_fn in self.nodes.items():
workflow.add_node(node_name, self._create_node_wrapper(node_fn))
# Set edge logic
workflow.set_conditional_edges(
condition_name="get_next_node",
condition_fn=get_next_node
)
# Compile graph
compiled_graph = workflow.compile()
# Run graph
start_time = datetime.datetime.now()
state_dict = compiled_graph.invoke(
{"input": input, "max_depth": trace_depth, "trace_enabled": self.trace_enabled}
)
end_time = datetime.datetime.now()
# Convert state dict back to ReasoningState
final_state = ReasoningState.parse_obj(state_dict)
# Update execution time
execution_time_ms = int((end_time - start_time).total_seconds() * 1000)
final_state.elapsed_ms = execution_time_ms
# Prepare result
result = {
"output": final_state.output,
"confidence": final_state.confidence,
"elapsed_ms": final_state.elapsed_ms,
"depth": final_state.depth,
"collapse_detected": final_state.collapse_detected,
}
# Include trace if enabled
if self.trace_enabled:
result["trace"] = {
"steps": final_state.steps,
"attribution": final_state.attribution,
"errors": final_state.errors,
"warnings": final_state.warnings,
"trace_id": final_state.trace_id,
}
return result
def _create_node_wrapper(self, node_fn: Callable) -> Callable:
"""
Create a wrapper for node functions to handle tracing and errors.
Args:
node_fn: Original node function
Returns:
Wrapped node function
"""
def wrapped_node(state: Dict[str, Any]) -> Dict[str, Any]:
# Convert state dict to ReasoningState
state_obj = ReasoningState.parse_obj(state)
# Increment depth counter
state_obj.depth += 1
# Run node function with try-except
try:
# Get node name from function
node_name = node_fn.__name__.replace("_", " ").title()
# Record step start
step_start = {
"name": node_name,
"depth": state_obj.depth,
"timestamp": datetime.datetime.now().isoformat(),
}
# Add to steps
state_obj.steps.append(step_start)
# Run node function
result = node_fn(state_obj)
# Update state with result
if isinstance(result, dict):
# Extract and update fields if returned
for key, value in result.items():
if hasattr(state_obj, key):
setattr(state_obj, key, value)
# Record step completion
step_end = {
"name": node_name,
"depth": state_obj.depth,
"completed": True,
"timestamp": datetime.datetime.now().isoformat(),
}
# Update last step
if state_obj.steps:
state_obj.steps[-1].update(step_end)
except Exception as e:
# Record error
error = {
"message": str(e),
"type": type(e).__name__,
"timestamp": datetime.datetime.now().isoformat(),
"node": node_fn.__name__,
"depth": state_obj.depth,
}
state_obj.errors.append(error)
# Update last step if exists
if state_obj.steps:
state_obj.steps[-1].update({
"completed": False,
"error": error,
})
# Set collapse if critical error
state_obj.collapse_detected = True
state_obj.collapse_reason = "critical_error"
# Convert back to dict
return state_obj.dict()
return wrapped_node
# Base node implementations
def _validate_input(self, state: ReasoningState) -> Dict[str, Any]:
"""
Validate input data.
Args:
state: Reasoning state
Returns:
Updated state fields
"""
result = {
"warnings": state.warnings.copy(),
}
# Check if input is empty
if not state.input:
result["warnings"].append({
"message": "Empty input provided",
"severity": "high",
"timestamp": datetime.datetime.now().isoformat(),
})
# Set collapse for empty input
result["collapse_detected"] = True
result["collapse_reason"] = "empty_input"
return result
def _initial_analysis(self, state: ReasoningState) -> Dict[str, Any]:
"""
Perform initial analysis of input data.
Args:
state: Reasoning state
Returns:
Updated state fields
"""
# Extract key information from input
analysis = {
"timestamp": datetime.datetime.now().isoformat(),
"key_entities": [],
"key_metrics": {},
"identified_patterns": [],
}
# Update state
result = {
"context": {**state.context, "initial_analysis": analysis},
}
return result
def _extract_themes(self, state: ReasoningState) -> Dict[str, Any]:
"""
Extract key themes from the data.
Args:
state: Reasoning state
Returns:
Updated state fields with extracted themes
"""
# Extract themes based on agent philosophy
themes = self.extract_themes(
text=str(state.input),
max_themes=5
)
# Calculate alignment with agent philosophy
alignment = self.compute_alignment(
themes=themes,
philosophy=self.agent_philosophy
)
# Update result
result = {
"context": {
**state.context,
"themes": themes,
"philosophy_alignment": alignment,
},
}
return result
def _generate_conclusions(self, state: ReasoningState) -> Dict[str, Any]:
"""
Generate conclusions based on analysis.
Args:
state: Reasoning state
Returns:
Updated state fields with conclusions
"""
# Simple placeholder conclusions
conclusions = {
"summary": "Analysis completed",
"confidence": 0.7,
"recommendations": [],
}
# Update result
result = {
"output": conclusions,
"confidence": conclusions["confidence"],
}
return result
def _check_collapse(self, state: ReasoningState) -> Dict[str, Any]:
"""
Check for reasoning collapse conditions.
Args:
state: Reasoning state
Returns:
Updated state fields with collapse detection
"""
# Default collapse risk is low
collapse_risk = 0.1
# Check for collapse conditions
collapse_conditions = {
"circular_reasoning": self._detect_circular_reasoning(state),
"confidence_collapse": state.confidence < 0.2,
"contradiction": self._detect_contradictions(state),
"depth_exhaustion": state.depth >= state.max_depth,
}
# Calculate overall collapse risk
active_conditions = [k for k, v in collapse_conditions.items() if v]
collapse_risk = len(active_conditions) / len(collapse_conditions) if collapse_conditions else 0
# Determine if collapse detected
collapse_detected = collapse_risk >= self.collapse_threshold
collapse_reason = active_conditions[0] if active_conditions else None
# Update result
result = {
"collapse_risk": collapse_risk,
"collapse_detected": collapse_detected,
"collapse_reason": collapse_reason,
}
# Add warning if collapse detected
if collapse_detected:
result["warnings"] = state.warnings + [{
"message": f"Reasoning collapse detected: {collapse_reason}",
"severity": "high",
"timestamp": datetime.datetime.now().isoformat(),
}]
return result
def _finalize_output(self, state: ReasoningState) -> Dict[str, Any]:
"""
Finalize output and prepare result.
Args:
state: Reasoning state
Returns:
Updated state fields with finalized output
"""
# If no output yet, create default
if not state.output:
output = {
"summary": "Analysis completed with limited results",
"confidence": max(0.1, state.confidence / 2), # Reduced confidence
"recommendations": [],
"timestamp": datetime.datetime.now().isoformat(),
}
# Update result
result = {
"output": output,
"confidence": output["confidence"],
}
else:
# Just return current state unchanged
result = {}
return result
# Utility methods for reasoning loops
def extract_themes(self, text: str, max_themes: int = 5) -> List[str]:
"""
Extract key themes from text using LLM.
Args:
text: Text to analyze
max_themes: Maximum number of themes to extract
Returns:
List of extracted themes
"""
# Use LLM to extract themes
prompt = f"""
Extract the {max_themes} most important themes or topics from the following text.
Respond with a Python list of strings, each representing one theme.
Text:
{text}
Themes:
"""
try:
response = self.model_router.generate(prompt)
# Parse response as Python list (safety handling)
try:
themes = eval(response.strip())
if isinstance(themes, list) and all(isinstance(t, str) for t in themes):
return themes[:max_themes]
except:
pass
# Fallback parsing
themes = [line.strip().strip('-*•').strip()
for line in response.split('\n')
if line.strip() and line.strip()[0] in '-*•']
return themes[:max_themes]
except Exception as e:
logging.warning(f"Error extracting themes: {e}")
return []
def compute_alignment(self, themes: List[str], philosophy: str) -> float:
"""
Compute alignment between themes and philosophy.
Args:
themes: List of themes
philosophy: Philosophy to align with
Returns:
Alignment score (0-1)
"""
# Without using LLM, use a simple heuristic based on word overlap
if not themes:
return 0.5 # Neutral score for no themes
# Convert to lowercase for comparison
philosophy_words = set(philosophy.lower().split())
# Count theme words that overlap with philosophy
alignment_scores = []
for theme in themes:
theme_words = set(theme.lower().split())
overlap = len(theme_words.intersection(philosophy_words))
total_words = len(theme_words)
# Calculate theme alignment
if total_words > 0:
theme_alignment = min(1.0, overlap / (total_words * 0.5)) # Scale for partial matches
alignment_scores.append(theme_alignment)
# Calculate average alignment across themes
return sum(alignment_scores) / len(alignment_scores) if alignment_scores else 0.5
def run_reflection(self, agent_state: Dict[str, Any], depth: int = 3,
trace_enabled: bool = False) -> Dict[str, Any]:
"""
Run reflection on agent's current state.
Args:
agent_state: Agent's current state
depth: Depth of reflection
trace_enabled: Whether to enable tracing
Returns:
Reflection results
"""
# Prepare reflection input
reflection_input = {
"agent_state": agent_state,
"agent_name": self.agent_name,
"agent_philosophy": self.agent_philosophy,
"reflection_depth": depth,
"timestamp": datetime.datetime.now().isoformat(),
}
# Create reflection prompt for LLM
prompt = f"""
You are the {self.agent_name} agent with the following philosophy:
"{self.agent_philosophy}"
Perform a depth {depth} reflection on your current state and decision making process.
Focus on:
1. Consistency between your investment philosophy and current beliefs
2. Quality of your recent decisions and their alignment with your values
3. Potential biases or blind spots in your analysis
4. Areas where your reasoning could be improved
Your current state:
{agent_state}
Respond with a JSON object containing:
- assessment: Overall assessment of your cognitive state
- consistency_score: How consistent your decisions are with your philosophy (0-1)
- identified_biases: List of potential biases detected
- improvement_areas: Areas where reasoning could be improved
- confidence: Your confidence in this self-assessment (0-1)
"""
try:
# Generate reflection using LLM
response = self.model_router.generate(prompt)
# Parse JSON response (with fallback)
# Parse JSON response (with fallback)
try:
import json
reflection = json.loads(response)
except json.JSONDecodeError:
# Fallback parsing
reflection = {
"assessment": "Unable to parse full reflection",
"consistency_score": 0.5,
"identified_biases": [],
"improvement_areas": [],
"confidence": 0.3,
}
# Extract fields from text response
if "consistency_score" in response:
try:
consistency_score = float(response.split("consistency_score")[1].split("\n")[0].replace(":", "").strip())
reflection["consistency_score"] = consistency_score
except:
pass
if "confidence" in response:
try:
confidence = float(response.split("confidence")[1].split("\n")[0].replace(":", "").strip())
reflection["confidence"] = confidence
except:
pass
return reflection
except Exception as e:
logging.warning(f"Error in reflection: {e}")
return {
"assessment": "Reflection failed due to error",
"consistency_score": 0.5,
"identified_biases": ["reflection_failure"],
"improvement_areas": ["error_handling"],
"confidence": 0.2,
"error": str(e),
}
def generate_from_experiences(self, experiences: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Generate signals based on past experiences.
Args:
experiences: List of past experiences
Returns:
List of generated signals
"""
if not experiences:
return []
# Format experiences for LLM prompt
formatted_experiences = "\n".join([
f"Experience {i+1}: {exp.get('description', 'No description')}" +
f"\nOutcome: {exp.get('outcome', 'Unknown')}" +
f"\nTimestamp: {exp.get('timestamp', 'Unknown')}"
for i, exp in enumerate(experiences)
])
# Create generation prompt for LLM
prompt = f"""
You are the {self.agent_name} agent with the following philosophy:
"{self.agent_philosophy}"
Based on the following past experiences, generate potential investment signals:
{formatted_experiences}
Generate 2-3 potential investment signals based on these experiences.
Each signal should be a JSON object containing:
- ticker: Stock ticker symbol
- action: "buy", "sell", or "hold"
- confidence: Confidence level (0.0-1.0)
- reasoning: Explicit reasoning chain
- intent: High-level investment intent
- value_basis: Core value driving this decision
Respond with a JSON array containing these signals.
"""
try:
# Generate signals using LLM
response = self.model_router.generate(prompt)
# Parse JSON response (with fallback)
try:
import json
signals = json.loads(response)
if not isinstance(signals, list):
signals = [signals]
except json.JSONDecodeError:
# Simple fallback extraction
signals = []
lines = response.split("\n")
current_signal = {}
for line in lines:
line = line.strip()
if line.startswith("ticker:") or line.startswith('"ticker":'):
# New signal starts
if current_signal and "ticker" in current_signal:
signals.append(current_signal)
current_signal = {}
current_signal["ticker"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("action:") or line.startswith('"action":'):
current_signal["action"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("confidence:") or line.startswith('"confidence":'):
try:
current_signal["confidence"] = float(line.split(":", 1)[1].strip().strip('"').strip("'").strip(','))
except:
current_signal["confidence"] = 0.5
elif line.startswith("reasoning:") or line.startswith('"reasoning":'):
current_signal["reasoning"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("intent:") or line.startswith('"intent":'):
current_signal["intent"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("value_basis:") or line.startswith('"value_basis":'):
current_signal["value_basis"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
# Add last signal if any
if current_signal and "ticker" in current_signal:
signals.append(current_signal)
# Ensure all required fields are present
for signal in signals:
signal.setdefault("ticker", "UNKNOWN")
signal.setdefault("action", "hold")
signal.setdefault("confidence", 0.5)
signal.setdefault("reasoning", "Generated from past experiences")
signal.setdefault("intent", "Learn from past experiences")
signal.setdefault("value_basis", "Experiential learning")
return signals
except Exception as e:
logging.warning(f"Error generating from experiences: {e}")
return []
def generate_from_beliefs(self, beliefs: Dict[str, float]) -> List[Dict[str, Any]]:
"""
Generate signals based on current beliefs.
Args:
beliefs: Current belief state
Returns:
List of generated signals
"""
if not beliefs:
return []
# Format beliefs for LLM prompt
formatted_beliefs = "\n".join([
f"Belief: {belief}, Strength: {strength:.2f}"
for belief, strength in beliefs.items()
])
# Create generation prompt for LLM
prompt = f"""
You are the {self.agent_name} agent with the following philosophy:
"{self.agent_philosophy}"
Based on the following current beliefs, generate potential investment signals:
{formatted_beliefs}
Generate 2-3 potential investment signals based on these beliefs.
Each signal should be a JSON object containing:
- ticker: Stock ticker symbol (extract from beliefs if present)
- action: "buy", "sell", or "hold"
- confidence: Confidence level (0.0-1.0)
- reasoning: Explicit reasoning chain
- intent: High-level investment intent
- value_basis: Core value driving this decision
Respond with a JSON array containing these signals.
"""
try:
# Generate signals using LLM
response = self.model_router.generate(prompt)
# Parse JSON response (with fallback)
try:
import json
signals = json.loads(response)
if not isinstance(signals, list):
signals = [signals]
except json.JSONDecodeError:
# Simple fallback extraction (same as in generate_from_experiences)
signals = []
lines = response.split("\n")
current_signal = {}
for line in lines:
line = line.strip()
if line.startswith("ticker:") or line.startswith('"ticker":'):
# New signal starts
if current_signal and "ticker" in current_signal:
signals.append(current_signal)
current_signal = {}
current_signal["ticker"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("action:") or line.startswith('"action":'):
current_signal["action"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("confidence:") or line.startswith('"confidence":'):
try:
current_signal["confidence"] = float(line.split(":", 1)[1].strip().strip('"').strip("'").strip(','))
except:
current_signal["confidence"] = 0.5
elif line.startswith("reasoning:") or line.startswith('"reasoning":'):
current_signal["reasoning"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("intent:") or line.startswith('"intent":'):
current_signal["intent"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
elif line.startswith("value_basis:") or line.startswith('"value_basis":'):
current_signal["value_basis"] = line.split(":", 1)[1].strip().strip('"').strip("'").strip(',')
# Add last signal if any
if current_signal and "ticker" in current_signal:
signals.append(current_signal)
# Ensure all required fields are present
for signal in signals:
signal.setdefault("ticker", "UNKNOWN")
signal.setdefault("action", "hold")
signal.setdefault("confidence", 0.5)
signal.setdefault("reasoning", "Generated from belief state")
signal.setdefault("intent", "Act on current beliefs")
signal.setdefault("value_basis", "Belief consistency")
return signals
except Exception as e:
logging.warning(f"Error generating from beliefs: {e}")
return []
# Collapse detection utilities
def _detect_circular_reasoning(self, state: ReasoningState) -> bool:
"""
Detect circular reasoning patterns in reasoning steps.
Args:
state: Current reasoning state
Returns:
True if circular reasoning detected, False otherwise
"""
# Need at least 3 steps to detect circularity
if len(state.steps) < 3:
return False
# Extract step names
step_names = [step.get("name", "") for step in state.steps]
# Look for repeating patterns (minimum 2 steps)
for pattern_len in range(2, len(step_names) // 2 + 1):
for i in range(len(step_names) - pattern_len * 2 + 1):
pattern = step_names[i:i+pattern_len]
next_seq = step_names[i+pattern_len:i+pattern_len*2]
if pattern == next_seq:
return True
return False
def _detect_contradictions(self, state: ReasoningState) -> bool:
"""
Detect contradictory statements in reasoning trace.
Args:
state: Current reasoning state
Returns:
True if contradictions detected, False otherwise
"""
# Simple implementation: check if confidence oscillates dramatically
if len(state.steps) < 3:
return False
# Extract confidence values
confidences = []
for step in state.steps:
if "output" in step and "confidence" in step["output"]:
confidences.append(step["output"]["confidence"])
# Check for oscillations
if len(confidences) >= 3:
for i in range(len(confidences) - 2):
# Check for significant up-down or down-up pattern
if (confidences[i] - confidences[i+1] > 0.3 and confidences[i+1] - confidences[i+2] < -0.3) or \
(confidences[i] - confidences[i+1] < -0.3 and confidences[i+1] - confidences[i+2] > 0.3):
return True
return False
# Reflection utilities
def fork_reflection(self, base_state: Dict[str, Any], depth: int = 2) -> List[Dict[str, Any]]:
"""
Create multiple reflection paths from a base state.
Args:
base_state: Base state to fork from
depth: Depth of reflection
Returns:
List of reflection results
"""
reflection_paths = []
# Create reflective dimensions
dimensions = [
"consistency", # Consistency with philosophy
"evidence", # Evidence evaluation
"alternatives", # Alternative hypotheses
"biases", # Cognitive biases
"gaps", # Knowledge gaps
]
# Generate reflection for each dimension
for dimension in dimensions:
reflection = self._reflect_on_dimension(base_state, dimension, depth)
reflection_paths.append({
"dimension": dimension,
"reflection": reflection,
"confidence": reflection.get("confidence", 0.5),
})
# Sort by confidence (highest first)
reflection_paths.sort(key=lambda x: x["confidence"], reverse=True)
return reflection_paths
def _reflect_on_dimension(self, state: Dict[str, Any], dimension: str, depth: int) -> Dict[str, Any]:
"""
Reflect on a specific dimension of reasoning.
Args:
state: Current state
dimension: Dimension to reflect on
depth: Depth of reflection
Returns:
Reflection results
"""
# Craft dimension-specific prompt
if dimension == "consistency":
prompt_focus = "the consistency between my decisions and my core philosophy"
elif dimension == "evidence":
prompt_focus = "the quality and completeness of evidence I'm considering"
elif dimension == "alternatives":
prompt_focus = "alternative hypotheses or viewpoints I might be overlooking"
elif dimension == "biases":
prompt_focus = "cognitive biases that might be affecting my judgment"
elif dimension == "gaps":
prompt_focus = "knowledge gaps that could be affecting my analysis"
else:
prompt_focus = f"the dimension of {dimension} in my reasoning"
# Create reflection prompt
prompt = f"""
You are the {self.agent_name} agent with the following philosophy:
"{self.agent_philosophy}"
Perform a depth {depth} reflection focusing specifically on {prompt_focus}.
Current state:
{state}
Respond with a JSON object containing:
- dimension: "{dimension}"
- assessment: Your assessment of this dimension
- issues_identified: List of specific issues identified
- recommendations: Recommendations to address these issues
- confidence: Your confidence in this assessment (0-1)
"""
try:
# Generate reflection using LLM
response = self.model_router.generate(prompt)
# Parse JSON response (with fallback)
try:
import json
reflection = json.loads(response)
except json.JSONDecodeError:
# Fallback parsing
reflection = {
"dimension": dimension,
"assessment": "Unable to parse full reflection",
"issues_identified": [],
"recommendations": [],
"confidence": 0.3,
}
return reflection
except Exception as e:
logging.warning(f"Error in dimension reflection: {e}")
return {
"dimension": dimension,
"assessment": "Reflection failed due to error",
"issues_identified": ["reflection_failure"],
"recommendations": ["error_handling"],
"confidence": 0.2,
"error": str(e),
}
|