refactor(cli): split the interactive choices and the run loop out of cli/main.py

- selections.py asks what to run; run.py builds the graph, streams it to the live view and saves the report
- main.py keeps the Typer app and its two commands
This commit is contained in:
Yijia-Xiao
2026-09-24 05:00:36 +00:00
parent 56bd98f690
commit 4a30cb1c0a
11 changed files with 846 additions and 830 deletions
+15 -15
View File
@@ -10,6 +10,7 @@ from unittest import mock
import pytest
import cli.main as m
import cli.run as cli_run
# Minimal selections dict shaped like get_user_selections()'s return value.
SELECTIONS = {
@@ -28,7 +29,7 @@ SELECTIONS = {
def test_research_depth_sets_both_rounds_without_env(monkeypatch):
for var in ("TRADINGAGENTS_MAX_DEBATE_ROUNDS", "TRADINGAGENTS_MAX_RISK_ROUNDS"):
monkeypatch.delenv(var, raising=False)
cfg = m._build_run_config(SELECTIONS, checkpoint=None)
cfg = cli_run._build_run_config(SELECTIONS, checkpoint=None)
assert cfg["max_debate_rounds"] == 5
assert cfg["max_risk_discuss_rounds"] == 5
@@ -37,9 +38,9 @@ def test_env_round_counts_win_over_selection(monkeypatch):
monkeypatch.setenv("TRADINGAGENTS_MAX_DEBATE_ROUNDS", "2")
monkeypatch.setenv("TRADINGAGENTS_MAX_RISK_ROUNDS", "4")
# DEFAULT_CONFIG already reflects the env (applied at import); emulate that.
patched = dict(m.DEFAULT_CONFIG, max_debate_rounds=2, max_risk_discuss_rounds=4)
with mock.patch.object(m, "DEFAULT_CONFIG", patched):
cfg = m._build_run_config(SELECTIONS, checkpoint=None)
patched = dict(cli_run.DEFAULT_CONFIG, max_debate_rounds=2, max_risk_discuss_rounds=4)
with mock.patch.object(cli_run, "DEFAULT_CONFIG", patched):
cfg = cli_run._build_run_config(SELECTIONS, checkpoint=None)
assert cfg["max_debate_rounds"] == 2 # env value, not research_depth=5
assert cfg["max_risk_discuss_rounds"] == 4
@@ -47,25 +48,25 @@ def test_env_round_counts_win_over_selection(monkeypatch):
def test_partial_env_only_overrides_that_count(monkeypatch):
monkeypatch.setenv("TRADINGAGENTS_MAX_DEBATE_ROUNDS", "2")
monkeypatch.delenv("TRADINGAGENTS_MAX_RISK_ROUNDS", raising=False)
patched = dict(m.DEFAULT_CONFIG, max_debate_rounds=2)
with mock.patch.object(m, "DEFAULT_CONFIG", patched):
cfg = m._build_run_config(SELECTIONS, checkpoint=None)
patched = dict(cli_run.DEFAULT_CONFIG, max_debate_rounds=2)
with mock.patch.object(cli_run, "DEFAULT_CONFIG", patched):
cfg = cli_run._build_run_config(SELECTIONS, checkpoint=None)
assert cfg["max_debate_rounds"] == 2 # env wins
assert cfg["max_risk_discuss_rounds"] == 5 # falls through to research_depth
def test_checkpoint_none_preserves_env_default():
patched = dict(m.DEFAULT_CONFIG, checkpoint_enabled=True) # e.g. env-enabled
with mock.patch.object(m, "DEFAULT_CONFIG", patched):
cfg = m._build_run_config(SELECTIONS, checkpoint=None)
patched = dict(cli_run.DEFAULT_CONFIG, checkpoint_enabled=True) # e.g. env-enabled
with mock.patch.object(cli_run, "DEFAULT_CONFIG", patched):
cfg = cli_run._build_run_config(SELECTIONS, checkpoint=None)
assert cfg["checkpoint_enabled"] is True # not clobbered back to False
@pytest.mark.parametrize("flag", [True, False])
def test_checkpoint_flag_overrides_env(flag):
patched = dict(m.DEFAULT_CONFIG, checkpoint_enabled=not flag)
with mock.patch.object(m, "DEFAULT_CONFIG", patched):
cfg = m._build_run_config(SELECTIONS, checkpoint=flag)
patched = dict(cli_run.DEFAULT_CONFIG, checkpoint_enabled=not flag)
with mock.patch.object(cli_run, "DEFAULT_CONFIG", patched):
cfg = cli_run._build_run_config(SELECTIONS, checkpoint=flag)
assert cfg["checkpoint_enabled"] is flag
@@ -89,14 +90,13 @@ def test_glm_resolves_to_the_endpoint_its_key_belongs_to():
def test_a_half_set_round_count_says_which_value_won(capsys, monkeypatch):
"""With only one of the two round-count variables set, the depth prompt is
still shown but half the answer is discarded; the user was never told."""
import cli.main as m
monkeypatch.setenv("TRADINGAGENTS_MAX_DEBATE_ROUNDS", "1")
monkeypatch.delenv("TRADINGAGENTS_MAX_RISK_ROUNDS", raising=False)
printed = []
monkeypatch.setattr(m.console, "print", lambda *a, **k: printed.append(str(a[0]) if a else ""))
config = m._build_run_config({
config = cli_run._build_run_config({
"ticker": "NVDA", "analysis_date": "2026-09-01", "asset_type": "stock",
"analysts": [], "research_depth": 5, "llm_provider": "openai",
"quick_think_llm": "gpt-5.6-luna", "deep_think_llm": "gpt-5.6",