Strategy Constitutional Memory
by @tltby12341
A living knowledge base of hard-earned strategy lessons and banned code patterns — prevents repeating past mistakes across strategy iterations by scanning co...
clawhub install strategy-constitutional-memory📖 About This Skill
name: strategy-constitutional-memory description: A living knowledge base of hard-earned strategy lessons and banned code patterns — prevents repeating past mistakes across strategy iterations by scanning code for violations and generating decision context. version: 1.0.0 metadata: openclaw: requires: bins: - python3 emoji: "\U0001F4DC"
Strategy Constitutional Memory
Stop making the same mistakes twice. This skill maintains a "constitutional memory" of lessons learned from past strategy iterations and a list of banned code patterns. Before generating new strategy code, the AI reads the constitution. After writing code, it scans for violations.
When to use
Core Concepts
Lessons
Structured records of what went wrong (or right) in past iterations:{
"strategy": "v6",
"category": "death_spiral",
"description": "Periodic rebalance caused death spiral: sell anchor -> buy options -> expire worthless -> sell more",
"evidence": "v6(-82%), v7(-71%), v8(-78.5%), v9(-71.8%)",
"severity": "critical"
}
Severity levels: critical > high > medium > low
Categories: drawdown, selection, position_sizing, timing, survival_structure, ml_failure, success
Bans
Code patterns that are absolutely prohibited because they've been proven catastrophic:["rebalance_qqq", "SetHoldings", "hard_stop_loss", "XGBClassifier"]
The scanner is case-insensitive and skips comments and string literals.
API
Initialize
from memory_system import ConstitutionalMemorymemory = ConstitutionalMemory(memory_dir="./memory")
Add a lesson
memory.add_lesson(
strategy_name="v6",
category="death_spiral",
description="Periodic equity rebalance caused -82% drawdown",
evidence="DD: 82%, triggered at 20% progress",
severity="critical",
new_ban="rebalance_anchor" # optionally add a new banned pattern
)
Auto-extract lessons from diagnosis report
memory.add_lesson_from_diagnosis("v30", diagnosis_report_text)
Automatically detects: high drawdown, high zero rate, negative ROI
Scan code for violations
violations = memory.scan_code(strategy_code_string)
Returns: [{"pattern": "rebalance_qqq", "line": 42, "content": "def rebalance_qqq():"}]
The scanner:
#)Generate LLM context
context = memory.get_context(max_lessons=30)
Returns formatted text with lessons sorted by severity,
banned patterns list, verified blueprints, and core rules
Feed this directly into your LLM system prompt before strategy generation.
CLI Usage
# Get decision context (lessons + bans + blueprints)
python3 -m orchestrator briefingScan a strategy file for violations
python3 -m orchestrator scan --code path/to/strategy.pyRecord an iteration result (auto-adds lessons for failures)
python3 -m orchestrator record \
--name "my_strategy_v2" \
--blueprint "baseline" \
--dimension "position_sizing" \
--hypothesis "Reduce Kelly from 3% to 2%" \
--status "early_stop" \
--drawdown 0.55
Storage
memory/lessons.json — Growing list of lessons (auto-persisted)memory/bans.json — Banned code patterns (auto-persisted)Both files are JSON and human-readable. You can manually edit them.
Seeding
For new projects, call memory.seed_from_history() to populate with your initial lessons. The method is idempotent — it won't overwrite existing data.
Why This Matters
In iterative strategy development, the biggest risk isn't finding the right approach — it's re-trying approaches that already failed. With 20+ iterations, no human (or LLM) can remember every lesson. Constitutional memory makes failures permanent knowledge.
Rules
scan_code() on new strategy code before submission. The scanner exists to prevent known-fatal patterns from being re-tested.lessons.json unless you are certain the lesson was recorded in error. Deleting valid lessons re-opens the door to repeating past failures.get_context() before generating new strategy code. The constitutional context must be in the LLM's prompt to prevent re-exploring failed approaches.⚡ When to Use
🔒 Constraints
scan_code() on new strategy code before submission. The scanner exists to prevent known-fatal patterns from being re-tested.lessons.json unless you are certain the lesson was recorded in error. Deleting valid lessons re-opens the door to repeating past failures.get_context() before generating new strategy code. The constitutional context must be in the LLM's prompt to prevent re-exploring failed approaches.