feat: implement L1-L7 context tree for multi-layered memory management
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>
This commit is contained in:
agentson
2026-02-04 14:12:29 +09:00
parent 3c676c2b8d
commit 917b68eb81
8 changed files with 1255 additions and 4 deletions

View File

@@ -220,7 +220,7 @@ docker compose --profile test up test # Run tests in container
## Architecture
Self-evolving AI trading agent for Korean stock markets (KIS API). The main loop in `src/main.py` orchestrates four components in a 60-second cycle per stock:
Self-evolving AI trading agent for Korean stock markets (KIS API). The main loop in `src/main.py` orchestrates five components in a 60-second cycle per stock:
1. **Broker** (`src/broker/kis_api.py`) — Async KIS API client with automatic OAuth token refresh, leaky-bucket rate limiter (10 RPS), and POST body hash-key signing. Uses a custom SSL context with disabled hostname verification for the VTS (virtual trading) endpoint due to a known certificate mismatch.
@@ -228,9 +228,11 @@ Self-evolving AI trading agent for Korean stock markets (KIS API). The main loop
3. **Risk Manager** (`src/core/risk_manager.py`) — **READ-ONLY by policy** (see `docs/agents.md`). Circuit breaker halts all trading via `SystemExit` when daily P&L drops below -3.0%. Fat-finger check rejects orders exceeding 30% of available cash.
4. **Evolution** (`src/evolution/optimizer.py`) — Analyzes high-confidence losing trades from SQLite, asks Gemini to generate new `BaseStrategy` subclasses, validates them by running the full pytest suite, and simulates PR creation.
4. **Context Tree** (`src/context/`) — **NEW: Pillar 2 implementation.** 7-tier hierarchical memory (L1-L7) from real-time quotes to generational wisdom. Auto-aggregates daily → weekly → monthly → quarterly → annual → legacy. See [`docs/context-tree.md`](docs/context-tree.md) for details.
**Data flow per cycle:** Fetch orderbook + balance → calculate P&L → get Gemini decision → validate with risk manager → execute order → log to SQLite (`src/db.py`).
5. **Evolution** (`src/evolution/optimizer.py`) — Analyzes high-confidence losing trades from SQLite, asks Gemini to generate new `BaseStrategy` subclasses, validates them by running the full pytest suite, and simulates PR creation.
**Data flow per cycle:** Fetch orderbook + balance → calculate P&L → query context tree → get Gemini decision → validate with risk manager → execute order → log to SQLite + context layers (`src/db.py`).
## Key Constraints (from `docs/agents.md`)
@@ -247,9 +249,10 @@ Pydantic Settings loaded from `.env` (see `.env.example`). Required vars: `KIS_A
## Test Structure
54 tests across four files. `asyncio_mode = "auto"` in pyproject.toml — async tests need no special decorator. The `settings` fixture in `conftest.py` provides safe defaults with test credentials and in-memory DB.
72 tests across five files. `asyncio_mode = "auto"` in pyproject.toml — async tests need no special decorator. The `settings` fixture in `conftest.py` provides safe defaults with test credentials and in-memory DB.
- `test_risk.py` (11) — Circuit breaker boundaries, fat-finger edge cases
- `test_broker.py` (6) — Token lifecycle, rate limiting, hash keys, network errors
- `test_brain.py` (18) — JSON parsing, confidence threshold, malformed responses, prompt construction
- `test_market_schedule.py` (19) — Market open/close logic, timezone handling, DST, lunch breaks
- `test_context.py` (18) — **NEW:** Context tree CRUD, aggregation logic, retention policies, layer metadata