Просмотр исходного кода

fix: reject failed codex executions

lxy 1 месяц назад
Родитель
Сommit
57a42c4aef
2 измененных файлов с 27 добавлено и 2 удалено
  1. 2 0
      okx_codex_trader/codex_analyzer.py
  2. 25 2
      tests/test_codex_analyzer.py

+ 2 - 0
okx_codex_trader/codex_analyzer.py

@@ -32,6 +32,8 @@ def analyze_with_codex(
         f"candles: {json.dumps([asdict(candle) for candle in candles], separators=(',', ':'))}"
     )
     completed = runner(["codex", "exec", prompt], capture_output=True, text=True, check=False)
+    if completed.returncode != 0:
+        raise ValueError("codex execution failed")
 
     try:
         payload = json.loads(completed.stdout)

+ 25 - 2
tests/test_codex_analyzer.py

@@ -14,12 +14,12 @@ def sample_candles() -> list[Candle]:
     ]
 
 
-def fake_runner(stdout: str):
+def fake_runner(stdout: str, returncode: int = 0):
     calls: list[tuple[object, bool, bool, bool]] = []
 
     def runner(command, capture_output: bool, text: bool, check: bool):
         calls.append((command, capture_output, text, check))
-        return subprocess.CompletedProcess(command, 0, stdout=stdout, stderr="")
+        return subprocess.CompletedProcess(command, returncode, stdout=stdout, stderr="")
 
     runner.calls = calls
     return runner
@@ -68,6 +68,29 @@ def test_analyzer_rejects_json_leverage_out_of_range():
     assert which.calls == ["codex"]
 
 
+def test_analyzer_rejects_non_zero_exit_with_non_json_stdout():
+    runner = fake_runner(stdout="not json", returncode=1)
+    which = fake_which()
+
+    with pytest.raises(ValueError, match="codex execution failed"):
+        analyze_with_codex(candles=sample_candles(), symbol="BTC-USDT-SWAP", bar="1H", runner=runner, which=which)
+
+    assert which.calls == ["codex"]
+
+
+def test_analyzer_rejects_non_zero_exit_with_json_stdout():
+    runner = fake_runner(
+        stdout='{"action":"long","confidence":0.8,"leverage":2,"entry_price":null,"take_profit_price":null,"stop_loss_price":null,"reason":"x"}',
+        returncode=1,
+    )
+    which = fake_which()
+
+    with pytest.raises(ValueError, match="codex execution failed"):
+        analyze_with_codex(candles=sample_candles(), symbol="BTC-USDT-SWAP", bar="1H", runner=runner, which=which)
+
+    assert which.calls == ["codex"]
+
+
 def test_analyzer_returns_valid_trade_signal():
     runner = fake_runner(
         stdout='{"action":"short","confidence":0.6,"leverage":2,"entry_price":101.5,"take_profit_price":99.0,"stop_loss_price":103.0,"reason":"trend"}'