fix(cli): report an unusable terminal instead of a prompt_toolkit traceback

- Windows terminals without a console buffer raised NoConsoleScreenBufferError
  before the first prompt, surfacing a raw traceback with no guidance
- gate the Windows-only import on sys.platform so a broken prompt_toolkit still
  surfaces there, and the handler stays inert on other platforms #1138
This commit is contained in:
Yijia-Xiao
2026-07-18 06:28:38 +00:00
parent d78c698d0e
commit 3f6c082695
2 changed files with 83 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import datetime
import os
import sys
import time
from collections import deque
from functools import wraps
@@ -52,6 +53,18 @@ from tradingagents.reporting import write_report_tree
console = Console()
# prompt_toolkit's win32 output module is importable only on Windows (it asserts
# the platform at import time), so gate on the platform rather than catching the
# failure — that way a genuinely broken prompt_toolkit on Windows still surfaces
# instead of silently disabling the handler below. Off Windows this stays an
# empty tuple, which `except` accepts and never matches (#1138).
if sys.platform == "win32": # pragma: no cover - platform dependent
from prompt_toolkit.output.win32 import NoConsoleScreenBufferError
_NO_CONSOLE_ERRORS: tuple[type[BaseException], ...] = (NoConsoleScreenBufferError,)
else:
_NO_CONSOLE_ERRORS = ()
app = typer.Typer(
name="TradingAgents",
help="TradingAgents CLI: Multi-Agents LLM Financial Trading Framework",
@@ -1285,7 +1298,19 @@ def analyze(
from tradingagents.graph.checkpointer import clear_all_checkpoints
n = clear_all_checkpoints(DEFAULT_CONFIG["data_cache_dir"])
console.print(f"[yellow]Cleared {n} checkpoint(s).[/yellow]")
run_analysis(checkpoint=checkpoint)
try:
run_analysis(checkpoint=checkpoint)
except _NO_CONSOLE_ERRORS:
# A terminal with no console buffer cannot host the interactive prompts.
# Emit one actionable line on stderr instead of a prompt_toolkit
# traceback; plain text, since rich may not render here either (#1138).
typer.echo(
"Error: no Windows console available. The interactive CLI needs a real "
"console buffer — run it from Windows Terminal, PowerShell, or cmd.exe "
"rather than a piped or embedded terminal.",
err=True,
)
raise typer.Exit(code=1) from None
if __name__ == "__main__":