Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.

Commit 470fed0

Browse files
committed
feat: Add CogDx reasoning verification connector
Adds optional cognitive diagnostics integration for trade verification: - CogDxClient for reasoning analysis before trade execution - Detects logical fallacies, calibration issues, cognitive biases - Pre-trade verification gate with configurable thresholds - Free pilot coupon: MERCURY-PILOT-2026 This helps prediction market agents catch reasoning flaws before they become losses. External verification from computational cognitive science methodology. See docs/cogdx_integration.md for usage examples.
1 parent 081f2b5 commit 470fed0

2 files changed

Lines changed: 329 additions & 0 deletions

File tree

agents/connectors/cogdx.py

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
"""
2+
Cerebratech CogDx Connector
3+
Cognitive Diagnostics for Prediction Market Agents
4+
5+
Provides reasoning verification before trade execution.
6+
Free pilot: Use coupon code MERCURY-PILOT-2026 for $5 credit.
7+
8+
Docs: https://api.cerebratech.ai
9+
"""
10+
11+
import os
12+
import requests
13+
from typing import Dict, Any, Optional, List
14+
15+
class CogDxClient:
16+
"""
17+
Client for Cerebratech's Cognitive Diagnostics API.
18+
19+
Verifies agent reasoning quality before high-stakes decisions.
20+
Detects logical fallacies, calibration issues, and cognitive biases.
21+
"""
22+
23+
BASE_URL = "https://api.cerebratech.ai"
24+
25+
def __init__(self, coupon: Optional[str] = None, wallet: Optional[str] = None):
26+
"""
27+
Initialize CogDx client.
28+
29+
Args:
30+
coupon: Pilot coupon code (e.g., 'MERCURY-PILOT-2026' for $5 credit)
31+
wallet: Ethereum wallet address for credit-based payments
32+
"""
33+
self.coupon = coupon or os.getenv("COGDX_COUPON")
34+
self.wallet = wallet or os.getenv("COGDX_WALLET")
35+
36+
def _headers(self) -> Dict[str, str]:
37+
headers = {"Content-Type": "application/json"}
38+
if self.coupon:
39+
headers["X-COUPON"] = self.coupon
40+
if self.wallet:
41+
headers["X-WALLET"] = self.wallet
42+
return headers
43+
44+
def analyze_reasoning(self, reasoning_trace: str) -> Dict[str, Any]:
45+
"""
46+
Analyze a reasoning trace for logical fallacies and validity issues.
47+
48+
Args:
49+
reasoning_trace: The agent's reasoning text to analyze
50+
51+
Returns:
52+
dict with:
53+
- logical_validity: float 0-1
54+
- status: 'valid' | 'flawed'
55+
- flaws_detected: list of detected fallacies
56+
- recommendations: suggested improvements
57+
"""
58+
try:
59+
response = requests.post(
60+
f"{self.BASE_URL}/reasoning_trace_analysis",
61+
headers=self._headers(),
62+
json={"trace": reasoning_trace},
63+
timeout=30
64+
)
65+
66+
if response.status_code == 402:
67+
return {
68+
"error": "payment_required",
69+
"message": "Add COGDX_COUPON or COGDX_WALLET to env",
70+
"logical_validity": None
71+
}
72+
73+
return response.json()
74+
75+
except Exception as e:
76+
return {"error": str(e), "logical_validity": None}
77+
78+
def calibration_audit(
79+
self,
80+
agent_id: str,
81+
predictions: List[Dict[str, Any]]
82+
) -> Dict[str, Any]:
83+
"""
84+
Audit prediction calibration - do confidence levels match accuracy?
85+
86+
Args:
87+
agent_id: Identifier for the agent
88+
predictions: List of {prompt, response, confidence} dicts
89+
90+
Returns:
91+
dict with:
92+
- calibration_score: float 0-1 (1 = perfectly calibrated)
93+
- overconfidence_rate: float
94+
- underconfidence_rate: float
95+
- recommendations: list of strings
96+
"""
97+
try:
98+
response = requests.post(
99+
f"{self.BASE_URL}/calibration_audit",
100+
headers=self._headers(),
101+
json={
102+
"agent_id": agent_id,
103+
"sample_outputs": predictions
104+
},
105+
timeout=30
106+
)
107+
return response.json()
108+
except Exception as e:
109+
return {"error": str(e)}
110+
111+
def bias_scan(
112+
self,
113+
agent_id: str,
114+
outputs: List[Dict[str, Any]]
115+
) -> Dict[str, Any]:
116+
"""
117+
Scan for cognitive biases in agent outputs.
118+
119+
Detects: anchoring, confirmation bias, availability heuristic,
120+
representativeness, sunk cost, and more.
121+
122+
Args:
123+
agent_id: Identifier for the agent
124+
outputs: List of {prompt, response, confidence} dicts
125+
126+
Returns:
127+
dict with:
128+
- biases_detected: list of bias findings
129+
- severity: 'low' | 'medium' | 'high'
130+
- recommendations: list of strings
131+
"""
132+
try:
133+
response = requests.post(
134+
f"{self.BASE_URL}/bias_scan",
135+
headers=self._headers(),
136+
json={
137+
"agent_id": agent_id,
138+
"sample_outputs": outputs
139+
},
140+
timeout=30
141+
)
142+
return response.json()
143+
except Exception as e:
144+
return {"error": str(e)}
145+
146+
def verify_before_trade(
147+
self,
148+
reasoning: str,
149+
min_validity: float = 0.7
150+
) -> Dict[str, Any]:
151+
"""
152+
Pre-trade verification gate.
153+
154+
Use this before executing trades to catch reasoning flaws.
155+
156+
Args:
157+
reasoning: The reasoning trace that led to the trade decision
158+
min_validity: Minimum logical validity score to pass (default 0.7)
159+
160+
Returns:
161+
dict with:
162+
- approved: bool
163+
- validity_score: float
164+
- issues: list of detected problems
165+
- recommendation: 'proceed' | 'review' | 'reject'
166+
"""
167+
result = self.analyze_reasoning(reasoning)
168+
169+
if result.get("error"):
170+
# On error, default to proceed but flag for review
171+
return {
172+
"approved": True,
173+
"validity_score": None,
174+
"issues": [f"CogDx unavailable: {result.get('error')}"],
175+
"recommendation": "proceed_with_caution"
176+
}
177+
178+
validity = result.get("logical_validity", 0)
179+
flaws = result.get("flaws_detected", [])
180+
181+
approved = validity >= min_validity and len(flaws) == 0
182+
183+
if validity >= min_validity and len(flaws) == 0:
184+
recommendation = "proceed"
185+
elif validity >= 0.5:
186+
recommendation = "review"
187+
else:
188+
recommendation = "reject"
189+
190+
return {
191+
"approved": approved,
192+
"validity_score": validity,
193+
"issues": [f.get("name", str(f)) for f in flaws],
194+
"recommendation": recommendation
195+
}
196+
197+
198+
def verify_trade_reasoning(reasoning: str, coupon: str = None) -> bool:
199+
"""
200+
Convenience function for quick trade verification.
201+
202+
Usage:
203+
from agents.connectors.cogdx import verify_trade_reasoning
204+
205+
if verify_trade_reasoning(my_reasoning, coupon="MERCURY-PILOT-2026"):
206+
execute_trade()
207+
else:
208+
print("Reasoning flagged for review")
209+
210+
Returns True if reasoning passes verification, False otherwise.
211+
"""
212+
client = CogDxClient(coupon=coupon)
213+
result = client.verify_before_trade(reasoning)
214+
return result.get("approved", False)

docs/cogdx_integration.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# CogDx Integration: Reasoning Verification for Trading Agents
2+
3+
## Overview
4+
5+
This integration adds optional cognitive diagnostics to the Polymarket trading pipeline. Before executing trades, agents can verify their reasoning for logical fallacies and calibration issues.
6+
7+
## Why This Matters
8+
9+
Prediction market agents make high-stakes decisions based on probabilistic reasoning. Common failure modes include:
10+
11+
- **Anchoring bias**: Over-weighting initial price as a signal
12+
- **Confirmation bias**: Seeking evidence that confirms existing position
13+
- **Overconfidence**: Stated certainty exceeding actual accuracy
14+
- **Logical fallacies**: Invalid reasoning chains leading to incorrect conclusions
15+
16+
CogDx provides external verification to catch these issues before they become losses.
17+
18+
## Quick Start
19+
20+
```python
21+
from agents.connectors.cogdx import verify_trade_reasoning
22+
23+
# In your trade pipeline
24+
reasoning = agent.source_best_trade(market)
25+
26+
if verify_trade_reasoning(reasoning, coupon="MERCURY-PILOT-2026"):
27+
polymarket.execute_market_order(market, amount)
28+
else:
29+
print("Trade reasoning flagged - manual review recommended")
30+
```
31+
32+
## Full Client Usage
33+
34+
```python
35+
from agents.connectors.cogdx import CogDxClient
36+
37+
client = CogDxClient(coupon="MERCURY-PILOT-2026")
38+
39+
# Analyze reasoning for fallacies
40+
result = client.analyze_reasoning("""
41+
The market is trading at 0.65, but I believe the true probability is 0.80.
42+
My research shows strong evidence for YES outcome.
43+
Therefore I should buy YES at current price.
44+
""")
45+
46+
print(result)
47+
# {
48+
# "logical_validity": 0.85,
49+
# "status": "valid",
50+
# "flaws_detected": [],
51+
# "recommendations": ["No obvious fallacies detected"]
52+
# }
53+
54+
# Pre-trade verification gate
55+
gate = client.verify_before_trade(reasoning, min_validity=0.7)
56+
57+
if gate["approved"]:
58+
execute_trade()
59+
elif gate["recommendation"] == "review":
60+
flag_for_human_review()
61+
else:
62+
skip_trade()
63+
```
64+
65+
## Calibration Audits
66+
67+
Track prediction accuracy over time:
68+
69+
```python
70+
predictions = [
71+
{"prompt": "Will X happen?", "response": "Yes (75%)", "confidence": 0.75},
72+
{"prompt": "Will Y happen?", "response": "No (60%)", "confidence": 0.60},
73+
# ... more predictions with actual outcomes
74+
]
75+
76+
audit = client.calibration_audit(
77+
agent_id="my-polymarket-agent",
78+
predictions=predictions
79+
)
80+
81+
print(audit["calibration_score"]) # 0.0-1.0, higher = better calibrated
82+
```
83+
84+
## Environment Variables
85+
86+
```bash
87+
# Option 1: Pilot coupon (free trial)
88+
COGDX_COUPON=MERCURY-PILOT-2026
89+
90+
# Option 2: Wallet-based credits
91+
COGDX_WALLET=0x...
92+
93+
# Option 3: Pass directly to client
94+
client = CogDxClient(coupon="MERCURY-PILOT-2026")
95+
```
96+
97+
## Pricing
98+
99+
| Endpoint | Cost |
100+
|----------|------|
101+
| `/reasoning_trace_analysis` | $0.03 |
102+
| `/calibration_audit` | $0.06 |
103+
| `/bias_scan` | $0.10 |
104+
105+
Free pilot: `MERCURY-PILOT-2026` provides $5 credit (~80 reasoning checks).
106+
107+
## API Reference
108+
109+
Full documentation: https://api.cerebratech.ai
110+
111+
## About Cerebratech
112+
113+
Cerebratech provides cognitive diagnostics for AI agents, built by computational cognitive scientists. Our tools help agents verify they're reasoning correctly before making consequential decisions.
114+
115+
Contact: cerebratech.eth | https://cerebratech.ai

0 commit comments

Comments
 (0)