Some checks failed
CI / test (pull_request) Has been cancelled
Implements Pillar 2 (Multi-layered Context Management) with a 7-tier hierarchical memory system from real-time market data to generational trading wisdom. ## New Modules - `src/context/layer.py`: ContextLayer enum and metadata config - `src/context/store.py`: ContextStore for CRUD operations - `src/context/aggregator.py`: Bottom-up aggregation (L7→L6→...→L1) ## Database Changes - Added `contexts` table for hierarchical data storage - Added `context_metadata` table for layer configuration - Indexed by layer, timeframe, and updated_at for fast queries ## Context Layers - L1 (Legacy): Cumulative wisdom (kept forever) - L2 (Annual): Yearly metrics (10 years retention) - L3 (Quarterly): Strategy pivots (3 years) - L4 (Monthly): Portfolio rebalancing (2 years) - L5 (Weekly): Stock selection (1 year) - L6 (Daily): Trade logs (90 days) - L7 (Real-time): Live market data (7 days) ## Tests - 18 new tests in `tests/test_context.py` - 100% coverage on context modules - All 72 tests passing (54 existing + 18 new) ## Documentation - Added `docs/context-tree.md` with comprehensive guide - Updated `CLAUDE.md` architecture section - Includes usage examples and best practices Closes #15 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
"""Context layer definitions for multi-tier memory management."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
|
|
|
|
class ContextLayer(str, Enum):
|
|
"""7-tier context hierarchy from real-time to generational."""
|
|
|
|
L1_LEGACY = "L1_LEGACY" # Cumulative/generational wisdom
|
|
L2_ANNUAL = "L2_ANNUAL" # Yearly performance
|
|
L3_QUARTERLY = "L3_QUARTERLY" # Quarterly strategy adjustments
|
|
L4_MONTHLY = "L4_MONTHLY" # Monthly rebalancing
|
|
L5_WEEKLY = "L5_WEEKLY" # Weekly stock selection
|
|
L6_DAILY = "L6_DAILY" # Daily trade logs
|
|
L7_REALTIME = "L7_REALTIME" # Real-time market data
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LayerMetadata:
|
|
"""Metadata for each context layer."""
|
|
|
|
layer: ContextLayer
|
|
description: str
|
|
retention_days: int | None # None = keep forever
|
|
aggregation_source: ContextLayer | None # Parent layer for aggregation
|
|
|
|
|
|
# Layer configuration
|
|
LAYER_CONFIG: dict[ContextLayer, LayerMetadata] = {
|
|
ContextLayer.L1_LEGACY: LayerMetadata(
|
|
layer=ContextLayer.L1_LEGACY,
|
|
description="Cumulative trading history and core lessons learned across generations",
|
|
retention_days=None, # Keep forever
|
|
aggregation_source=ContextLayer.L2_ANNUAL,
|
|
),
|
|
ContextLayer.L2_ANNUAL: LayerMetadata(
|
|
layer=ContextLayer.L2_ANNUAL,
|
|
description="Yearly returns, Sharpe ratio, max drawdown, win rate",
|
|
retention_days=365 * 10, # 10 years
|
|
aggregation_source=ContextLayer.L3_QUARTERLY,
|
|
),
|
|
ContextLayer.L3_QUARTERLY: LayerMetadata(
|
|
layer=ContextLayer.L3_QUARTERLY,
|
|
description="Quarterly strategy adjustments, market phase detection, sector rotation",
|
|
retention_days=365 * 3, # 3 years
|
|
aggregation_source=ContextLayer.L4_MONTHLY,
|
|
),
|
|
ContextLayer.L4_MONTHLY: LayerMetadata(
|
|
layer=ContextLayer.L4_MONTHLY,
|
|
description="Monthly portfolio rebalancing, risk exposure, drawdown recovery",
|
|
retention_days=365 * 2, # 2 years
|
|
aggregation_source=ContextLayer.L5_WEEKLY,
|
|
),
|
|
ContextLayer.L5_WEEKLY: LayerMetadata(
|
|
layer=ContextLayer.L5_WEEKLY,
|
|
description="Weekly stock selection, sector focus, volatility regime",
|
|
retention_days=365, # 1 year
|
|
aggregation_source=ContextLayer.L6_DAILY,
|
|
),
|
|
ContextLayer.L6_DAILY: LayerMetadata(
|
|
layer=ContextLayer.L6_DAILY,
|
|
description="Daily trade logs, P&L, market summaries, decision accuracy",
|
|
retention_days=90, # 90 days
|
|
aggregation_source=ContextLayer.L7_REALTIME,
|
|
),
|
|
ContextLayer.L7_REALTIME: LayerMetadata(
|
|
layer=ContextLayer.L7_REALTIME,
|
|
description="Real-time positions, quotes, orderbook, volatility, live P&L",
|
|
retention_days=7, # 7 days (real-time data is ephemeral)
|
|
aggregation_source=None, # No aggregation source (leaf layer)
|
|
),
|
|
}
|