Coverage for risk/manual_override.py: 100%
48 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 12:55 +0000
1"""Manual trade-lock override for Aether Quant V2.
3`main.py`'s sticky total-drawdown lock (see `_refresh_risk_state()`) never
4auto-clears on its own once tripped - by design, a capital-preservation
5circuit breaker requiring a deliberate decision to resume trading. This
6module is that deliberate decision, expressed as a single config value:
7`phase_v2.risk.manual_trade_lock_override` (`true` = force-lock, `false` =
8force-clear, absent/`null` = leave today's automatic behavior untouched).
10Two callers, both read-modify-write the same key:
11- `aq_cli.py`'s `trade-lock` command - a human flips the switch directly.
12- `retraining/orchestrator.py::promote()` - a successful promotion clears it
13 automatically, tying "trading resumes" to "a genuinely new model shipped".
15This is the first place in the codebase where a Python process writes to
16`config.json` (every other reader treats it as human-edited, read-only
17input) - a deliberate, narrow exception, not a new general pattern. It is a
18*standing* switch, not one-shot: it stays in effect until explicitly changed
19again, so `main.py` itself never writes back to `config.json` - it only
20reads this one key once per session rollover (see `_refresh_risk_state()`).
21"""
23from __future__ import annotations
25import json
26from pathlib import Path
28from execution.config_cache import read_cached
30_PHASE_V2_KEY = "phase_v2"
31_RISK_KEY = "risk"
32_OVERRIDE_KEY = "manual_trade_lock_override"
35def read_manual_trade_lock_override(config_path: Path) -> bool | None:
36 """Returns True/False if a manual override is set, None if absent/unset
37 or the config file doesn't exist yet - never raises. Mtime-gated cache
38 (see execution/config_cache.py) - picks up an edit as soon as the file's
39 mtime changes, not on a fixed schedule."""
40 return read_cached(config_path, _read_manual_trade_lock_override_uncached)
43def _read_manual_trade_lock_override_uncached(config_path: Path) -> bool | None:
44 if not config_path.exists():
45 return None
46 with config_path.open("r", encoding="utf-8") as f:
47 config = json.load(f)
48 value = config.get(_PHASE_V2_KEY, {}).get(_RISK_KEY, {}).get(_OVERRIDE_KEY)
49 return value if isinstance(value, bool) else None
52def write_manual_trade_lock_override(value: bool | None, config_path: Path) -> None:
53 """Read-modify-write config.json, setting only
54 phase_v2.risk.manual_trade_lock_override - every other key is preserved
55 untouched. value=None removes the override key entirely (returns to
56 fully automatic behavior)."""
57 with config_path.open("r", encoding="utf-8") as f:
58 config = json.load(f)
60 phase_v2 = config.setdefault(_PHASE_V2_KEY, {})
61 risk = phase_v2.setdefault(_RISK_KEY, {})
62 if value is None:
63 risk.pop(_OVERRIDE_KEY, None)
64 else:
65 risk[_OVERRIDE_KEY] = value
67 with config_path.open("w", encoding="utf-8") as f:
68 json.dump(config, f, indent=4)
69 f.write("\n")
72# V5.1 Phase 6 (production safety) - the kill switch's own arm/disarm
73# override, deliberately the EXACT same read/write/cache shape as the
74# manual trade-lock override above (same GUARDRAIL the plan states for
75# `aq kill-switch`: reuse this convention so the two can never disagree -
76# an operator can force-disarm the kill switch's own evaluation without
77# touching phase_v2.risk.kill_switch.enabled in config.json at all, the
78# same way manual_trade_lock_override sits ON TOP of the automatic
79# drawdown-lock logic rather than replacing it).
80_KILL_SWITCH_OVERRIDE_KEY = "kill_switch_manual_override"
83def read_kill_switch_manual_override(config_path: Path) -> bool | None:
84 """True = force-armed, False = force-disarmed, None = defer entirely to
85 config.json's own phase_v2.risk.kill_switch.enabled. Never raises -
86 same missing-file/missing-key degrade-to-None contract as
87 read_manual_trade_lock_override()."""
88 return read_cached(config_path, _read_kill_switch_manual_override_uncached)
91def _read_kill_switch_manual_override_uncached(config_path: Path) -> bool | None:
92 if not config_path.exists():
93 return None
94 with config_path.open("r", encoding="utf-8") as f:
95 config = json.load(f)
96 value = config.get(_PHASE_V2_KEY, {}).get(_RISK_KEY, {}).get(_KILL_SWITCH_OVERRIDE_KEY)
97 return value if isinstance(value, bool) else None
100def write_kill_switch_manual_override(value: bool | None, config_path: Path) -> None:
101 """Read-modify-write config.json, setting only
102 phase_v2.risk.kill_switch_manual_override - every other key (including
103 phase_v2.risk.manual_trade_lock_override) is preserved untouched.
104 value=None removes the override key entirely (returns to fully
105 config-driven kill_switch.enabled behavior)."""
106 with config_path.open("r", encoding="utf-8") as f:
107 config = json.load(f)
109 phase_v2 = config.setdefault(_PHASE_V2_KEY, {})
110 risk = phase_v2.setdefault(_RISK_KEY, {})
111 if value is None:
112 risk.pop(_KILL_SWITCH_OVERRIDE_KEY, None)
113 else:
114 risk[_KILL_SWITCH_OVERRIDE_KEY] = value
116 with config_path.open("w", encoding="utf-8") as f:
117 json.dump(config, f, indent=4)
118 f.write("\n")