Revert "fix(cli): CLI/TUI on local backend always uses launch directory, ignores terminal.cwd (#19242)" (#19329)

This reverts commit 9eaddfafa3.
This commit is contained in:
Siddharth Balyan
2026-05-04 00:43:58 +05:30
committed by GitHub
parent 9eaddfafa3
commit 167b5648ea
6 changed files with 72 additions and 122 deletions

48
cli.py
View File

@@ -459,30 +459,32 @@ def load_cli_config() -> Dict[str, Any]:
if "backend" in terminal_config:
terminal_config["env_type"] = terminal_config["backend"]
# CWD resolution: CLI/TUI on local backend always uses os.getcwd();
# gateway/cron uses terminal.cwd from config. Detection: gateway's config
# bridge (gateway/run.py) sets TERMINAL_CWD before this runs.
# See #19214, #4672, #10225, #10817.
# Handle special cwd values: "." or "auto" means use current working directory.
# Only resolve to the host's CWD for the local backend where the host
# filesystem is directly accessible. For ALL remote/container backends
# (ssh, docker, modal, singularity), the host path doesn't exist on the
# target -- remove the key so terminal_tool.py uses its per-backend default.
#
# GUARD: If TERMINAL_CWD is already set to a real absolute path (by the
# gateway's config bridge earlier in the process), don't clobber it.
# This prevents a lazy import of cli.py during gateway runtime from
# rewriting TERMINAL_CWD to the service's working directory.
# See issue #10817.
_CWD_PLACEHOLDERS = (".", "auto", "cwd")
_existing_cwd = os.environ.get("TERMINAL_CWD", "")
_is_gateway_import = (
_existing_cwd
and _existing_cwd not in _CWD_PLACEHOLDERS
and os.path.isabs(_existing_cwd)
)
effective_backend = terminal_config.get("env_type", "local")
if _is_gateway_import:
terminal_config["cwd"] = _existing_cwd
defaults["terminal"]["cwd"] = _existing_cwd
elif effective_backend == "local":
# CLI/TUI: user's `cd` is the config — ignore terminal.cwd.
terminal_config["cwd"] = os.getcwd()
defaults["terminal"]["cwd"] = terminal_config["cwd"]
elif terminal_config.get("cwd") in _CWD_PLACEHOLDERS:
# Non-local backend with placeholder — let terminal_tool use its default.
terminal_config.pop("cwd", None)
# else: non-local backend with explicit path — keep as-is
if terminal_config.get("cwd") in _CWD_PLACEHOLDERS:
_existing_cwd = os.environ.get("TERMINAL_CWD", "")
if _existing_cwd and _existing_cwd not in _CWD_PLACEHOLDERS and os.path.isabs(_existing_cwd):
# Gateway (or earlier startup) already resolved a real path — keep it
terminal_config["cwd"] = _existing_cwd
defaults["terminal"]["cwd"] = _existing_cwd
else:
effective_backend = terminal_config.get("env_type", "local")
if effective_backend == "local":
terminal_config["cwd"] = os.getcwd()
defaults["terminal"]["cwd"] = terminal_config["cwd"]
else:
# Remove so TERMINAL_CWD stays unset → tool picks backend default
terminal_config.pop("cwd", None)
env_mappings = {
"env_type": "TERMINAL_ENV",