Skip to content

[Bug]: UnboundLocalError: ctx_len in show_banner() crashes CLI on launch #4331

Closed

Description

@steve-goldberg

Bug Description

The CLI crashes immediately on launch with an UnboundLocalError when the agent is not fully initialized before show_banner() runs. This prevents any use of the CLI with custom providers (e.g. Z.AI).

Environment

  • Hermes: v0.6.0 (latest via hermes update, March 31 2026)
  • OS: Ubuntu 24.04 (N95 Mini PC)
  • Provider: Z.AI (custom base_url in config.yaml)
  • Model: glm-5
  • Python: 3.11.14

Root Cause

In show_banner() (cli.py ~line 2179), ctx_len is assigned inside an else block:

        else:
            # ...
            ctx_len = None
            if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'context_compressor'):
                ctx_len = self.agent.context_compressor.context_length

But it's referenced outside the if/else, after both branches (~line 2198):

        # Warn about very low context lengths (common with local servers)
        if ctx_len and ctx_len <= 8192:

When the if branch executes (compact banner, or agent not yet initialized), ctx_len is never set, and the post-branch check crashes with UnboundLocalError.

It also appears on line 2191 where context_length=ctx_len is passed to build_welcome_banner() — same scoping issue.

Steps to Reproduce

  1. Configure a custom provider with base_url in ~/.hermes/config.yaml:
    model:
    default: glm-5
    provider: zai
    base_url: https://api.z.ai/api/coding/paas/v4

  2. Launch the CLI:
    hermes

  3. CLI crashes before reaching the input prompt.

Expected Behavior

CLI launches normally. If context length cannot be detected for a
custom provider, ctx_len should default to None and the low-context
warning should be silently skipped.

Actual Behavior

Banner partially renders (model name, tools, provider line visible),
then crashes with UnboundLocalError: cannot access local variable
'ctx_len' before the input prompt appears. CLI is completely unusable.

Affected Component

CLI (interactive chat)

Messaging Platform (if gateway-related)

N/A (CLI only)

Operating System

Ubuntu 24.04

Python Version

3.11.14

Hermes Version

0.6.0

Relevant Logs / Traceback

Traceback (most recent call last):
  File "/home/steve/.local/bin/hermes", line 10, in <module>
    sys.exit(main())
  File "/home/steve/.hermes/hermes-agent/hermes_cli/main.py", line 5035, in main
    cmd_chat(args)
  File "/home/steve/.hermes/hermes-agent/hermes_cli/main.py", line 652, in cmd_chat
    cli_main(**kwargs)
  File "/home/steve/.hermes/hermes-agent/cli.py", line 7898, in main
    cli.run()
  File "/home/steve/.hermes/hermes-agent/cli.py", line 6343, in run
    self.show_banner()
  File "/home/steve/.hermes/hermes-agent/cli.py", line 2198, in show_banner
    if ctx_len and ctx_len <= 8192:
       ^^^^^^^^^
UnboundLocalError: cannot access local variable 'ctx_len' where it is not associated with a value

Root Cause Analysis (optional)

No response

Proposed Fix (optional)

Move ctx_len = None above the if/else so it's always in scope:

        ctx_len = None  # Initialize before branching

        if use_compact:
            self.console.print(_build_compact_banner())
            self._show_status()
        else:
            # ...
            if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'context_compressor'):
                ctx_len = self.agent.context_compressor.context_length

One-line fix. Happy to open a PR if preferred.

Related

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Activity

  1. steve-goldberg commented on Apr 9, 2026

    @steve-goldberg
    Author

    Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions