Skip to content

refactor: 5-phase system architecture refactoring#487

Merged
cct08311github merged 9 commits intomainfrom
claude/system-refactoring-plan-BjwZr
Mar 28, 2026
Merged

refactor: 5-phase system architecture refactoring#487
cct08311github merged 9 commits intomainfrom
claude/system-refactoring-plan-BjwZr

Conversation

@cct08311github
Copy link
Copy Markdown
Owner

Summary

系統性重構,解決核心引擎的 7 個架構問題,分 5 個 Phase 漸進式實施(Strangler Fig Pattern)。

  • Phase 1 — ConfigManager:集中設定管理,取代 8 個模組各自載入 JSON 的反模式
  • Phase 2 — Repository Pattern:5 個 Repository 類別封裝 DB 存取,取代散落各處的直接 SQL
  • Phase 3 — MarketDataService + WatcherApp:從 1,675 行 ticker_watcher.py 抽離行情服務與生命週期管理,thread-safe shutdown
  • Phase 4 — Guard Chain Pattern:可插拔的 Chain of Responsibility 決策管線,解耦 8 個硬耦合 guard
  • Phase 5 — Structured Logging:StructuredAdapter 結構化日誌工具
  • Bonus — 修復 7 個遺留測試失敗:backup_db、execution_quality_report、quarantine_clear

新增檔案 (20)

src/openclaw/config_manager.py
src/openclaw/repositories/ (5 files)
src/openclaw/market_data_service.py
src/openclaw/watcher_lifecycle.py
src/openclaw/guards/ (6 files)
src/openclaw/log_utils.py
tests/test_config_manager.py (21 tests)
tests/test_repositories/ (16 tests)
tests/test_guard_chain.py (7 tests)
tests/test_log_utils.py (3 tests)

QA 結果

套件 結果
核心引擎 2482 passed, 0 failed
FastAPI 後端 938 passed, 0 failed
前端 129 passed, 0 failed
Total 3549 passed, 0 failed

Test plan

  • python -m pytest — 2482 passed, 0 failed
  • cd frontend/backend && python -m pytest tests/ — 938 passed, 0 failed
  • cd frontend/web && npx vitest --run — 129 passed, 0 failed
  • 所有原有函數簽名保持向後相容
  • PM2 ecosystem.config.js 不需修改
  • 新增 47 個測試覆蓋重構程式碼

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41

claude added 9 commits March 28, 2026 04:45
Comprehensive analysis of current architecture issues and a phased
refactoring roadmap: ConfigManager, Repository Pattern, ticker_watcher
decomposition, Guard Chain Pattern, and logging standardization.

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
Replace scattered JSON config loading across 8 modules with a unified
ConfigManager singleton. Thread-safe, cached, fail-safe with defaults.

Modules migrated:
- risk_engine.py: _is_symbol_locked, _get_daily_pm_approval
- sentinel.py: _locked_symbols
- ticker_watcher.py: _load_manual_watchlist, _load_sim_nav
- daily_pm_review.py: get_daily_pm_approval
- agent_orchestrator.py: _pm_review_just_completed
- eod_ingest.py: capital loading
- ops_health.py: load_alert_thresholds
- tg_kill_switch.py: _get_system_state

All 2450 core tests + 581 backend tests pass (0 new failures).

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
Add 5 repository classes encapsulating SQL operations:
- OrderRepository: orders, fills, order_events CRUD
- PositionRepository: positions read/update
- DecisionRepository: decisions, risk_checks INSERT
- SignalRepository: lm_signal_cache, eod_prices access
- TraceRepository: llm_traces, incidents access

Migrate ticker_watcher.py _persist_* functions to delegate
to repositories (preserving original function signatures).

16 new repository unit tests, all 2466 tests pass.

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
…ker_watcher

Extract two focused modules from the 1,675-line ticker_watcher.py:

- market_data_service.py: MarketDataService class encapsulating
  Shioaji snapshot fetching with mock fallback. Replaces global
  _BASE_PRICE dict and standalone _get_snapshot/_mock_snapshot functions.

- watcher_lifecycle.py: WatcherApp class with thread-safe shutdown
  via threading.Event (replacing global _shutdown_requested bool),
  signal handler installation, and interruptible sleep.

ticker_watcher.py retains backward-compatible function signatures
that delegate to the new classes.

All 2466 tests pass (0 new failures).

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
Introduce pluggable Guard Chain of Responsibility pattern:

- guards/base.py: Guard ABC, GuardContext, GuardResult, GuardChain
- guards/system_switch_guard.py: Master switch adapter
- guards/budget_guard.py: Token budget evaluation adapter
- guards/drawdown_guard.py: Drawdown + deep suspend adapters
- guards/sentinel_guard.py: Pre-trade, PM veto, post-risk adapters

Guards can be composed, reordered, and individually tested.
Context updates propagate through the chain.

decision_pipeline_v4.py retains its original function signature
for backward compatibility.

7 new guard chain tests, all 2473 tests pass.

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
Add log_utils.py with:
- StructuredAdapter: appends key-value JSON to log messages
- get_structured_logger(): factory for structured loggers
- Convention documentation for future modules

All 2476 tests pass (0 new failures).

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
Record actual commit hashes and test results for all 5 phases.
Final: 2476 passed, 6 pre-existing failures, 0 regressions.

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
- test_backup_db: add skipif when sqlite3 CLI unavailable
- test_execution_quality_report: use dynamic today date instead of
  hardcoded 2026-03-18 (was outside 7-day query window)
- test_system_api quarantine_clear: add missing entry_trading_day,
  high_water_mark, state columns to positions fixture schema;
  use named column INSERTs for forward compatibility

All 3549 tests now pass (2482 core + 938 backend + 129 frontend).

https://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41
@cct08311github cct08311github merged commit 293d469 into main Mar 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants