refactor: 5-phase system architecture refactoring#487
Merged
cct08311github merged 9 commits intomainfrom Mar 28, 2026
Merged
Conversation
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
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
系統性重構,解決核心引擎的 7 個架構問題,分 5 個 Phase 漸進式實施(Strangler Fig Pattern)。
新增檔案 (20)
QA 結果
Test plan
python -m pytest— 2482 passed, 0 failedcd frontend/backend && python -m pytest tests/— 938 passed, 0 failedcd frontend/web && npx vitest --run— 129 passed, 0 failedhttps://claude.ai/code/session_011LhoTJuk5FAaZMaHVzWC41