Skip to content

fix(cli): load_cli_config() overwrites gateway's TERMINAL_CWD from MESSAGING_CWD #10225

@LLQWQ

Description

@LLQWQ

Describe the bug
When running Hermes in messaging gateway mode, gateway/run.py correctly initializes TERMINAL_CWD from the MESSAGING_CWD environment variable at startup. However, as soon as a tool like execute_code or delegate_task lazily imports cli.CLI_CONFIG, load_cli_config() is triggered. If config.yaml contains terminal.cwd: "." (or "auto"), the function unconditionally resolves it to os.getcwd() and overwrites the already-correct TERMINAL_CWD environment variable. This causes all subsequent terminal commands to run in the wrong working directory (e.g., the systemd WorkingDirectory instead of the configured MESSAGING_CWD).

Root cause
cli.py module-level code calls load_cli_config(), which resolves terminal.cwd: "." to os.getcwd() and sets os.environ["TERMINAL_CWD"] = os.getcwd(). This happens after gateway/run.py has correctly set TERMINAL_CWD from MESSAGING_CWD, because cli.py is imported lazily by tools like code_execution_tool.py or delegate_tool.py.

To Reproduce

  1. Set MESSAGING_CWD=/some/path in .env.
  2. Set terminal.cwd: "." in config.yaml.
  3. Start the gateway.
  4. Send a message that triggers execute_code or delegate_task.
  5. Run pwd — it returns the systemd working directory instead of /some/path.

Expected behavior
load_cli_config() should respect an already-set TERMINAL_CWD and not overwrite it with os.getcwd().

Proposed fix
In load_cli_config(), before resolving . / auto to os.getcwd(), check if TERMINAL_CWD is already set to a valid path. If so, reuse it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilescomp/cliCLI entry point, hermes_cli/, setup wizardcomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions