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
- Set
MESSAGING_CWD=/some/path in .env.
- Set
terminal.cwd: "." in config.yaml.
- Start the gateway.
- Send a message that triggers
execute_code or delegate_task.
- 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.
Describe the bug
When running Hermes in messaging gateway mode,
gateway/run.pycorrectly initializesTERMINAL_CWDfrom theMESSAGING_CWDenvironment variable at startup. However, as soon as a tool likeexecute_codeordelegate_tasklazily importscli.CLI_CONFIG,load_cli_config()is triggered. Ifconfig.yamlcontainsterminal.cwd: "."(or"auto"), the function unconditionally resolves it toos.getcwd()and overwrites the already-correctTERMINAL_CWDenvironment variable. This causes all subsequent terminal commands to run in the wrong working directory (e.g., the systemdWorkingDirectoryinstead of the configuredMESSAGING_CWD).Root cause
cli.pymodule-level code callsload_cli_config(), which resolvesterminal.cwd: "."toos.getcwd()and setsos.environ["TERMINAL_CWD"] = os.getcwd(). This happens aftergateway/run.pyhas correctly setTERMINAL_CWDfromMESSAGING_CWD, becausecli.pyis imported lazily by tools likecode_execution_tool.pyordelegate_tool.py.To Reproduce
MESSAGING_CWD=/some/pathin.env.terminal.cwd: "."inconfig.yaml.execute_codeordelegate_task.pwd— it returns the systemd working directory instead of/some/path.Expected behavior
load_cli_config()should respect an already-setTERMINAL_CWDand not overwrite it withos.getcwd().Proposed fix
In
load_cli_config(), before resolving./autotoos.getcwd(), check ifTERMINAL_CWDis already set to a valid path. If so, reuse it.