diff --git a/cli.py b/cli.py index 151ae4615..0469d09b4 100644 --- a/cli.py +++ b/cli.py @@ -2163,6 +2163,12 @@ class HermesCLI: def show_banner(self): """Display the welcome banner in Claude Code style.""" self.console.clear() + + # Get context length for display before branching so it remains + # available to the low-context warning logic in compact mode too. + ctx_len = None + if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'context_compressor'): + ctx_len = self.agent.context_compressor.context_length # Auto-compact for narrow terminals — the full banner with caduceus # + tool list needs ~80 columns minimum to render without wrapping. @@ -2179,11 +2185,6 @@ class HermesCLI: # Get terminal working directory (where commands will execute) cwd = os.getenv("TERMINAL_CWD", os.getcwd()) - # Get context length for display - ctx_len = None - if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'context_compressor'): - ctx_len = self.agent.context_compressor.context_length - # Build and display the banner build_welcome_banner( console=self.console, diff --git a/tests/test_cli_context_warning.py b/tests/test_cli_context_warning.py index fa0305a27..abf9c1349 100644 --- a/tests/test_cli_context_warning.py +++ b/tests/test_cli_context_warning.py @@ -145,3 +145,15 @@ class TestLowContextWarning: calls = [str(c) for c in cli_obj.console.print.call_args_list] warning_calls = [c for c in calls if "too low" in c] assert len(warning_calls) == 0 + + def test_compact_banner_does_not_crash_on_narrow_terminal(self, cli_obj): + """Compact mode should still have ctx_len defined for warning logic.""" + cli_obj.agent.context_compressor.context_length = 4096 + + with patch("shutil.get_terminal_size", return_value=os.terminal_size((70, 40))), \ + patch("cli._build_compact_banner", return_value="compact banner"): + cli_obj.show_banner() + + calls = [str(c) for c in cli_obj.console.print.call_args_list] + warning_calls = [c for c in calls if "too low" in c] + assert len(warning_calls) == 1