mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-18 09:51:59 +00:00
fix(tools): preserve live session cwd in terminal_tool, and keep ACP update_cwd authoritative
terminal_tool re-sent the init-time/config cwd on every command, clobbering session-local `cd` state: the environment tracked the new directory in `env.cwd`, but foreground/background calls forced the old cwd back. A small `_resolve_command_cwd` resolver now applies the precedence `workdir > live env.cwd > config/override cwd` to: - foreground `env.execute(...)` - background `process_registry.spawn_local(...)` - background `process_registry.spawn_via_env(...)` Additionally, syncing the cwd onto the live cached env when a `cwd` override is (re-)registered. Preferring live `env.cwd` would otherwise demote the ACP `update_cwd` override (registered via `register_task_env_overrides` on `session/load` / `session/resume`) below an already-set `env.cwd`, silently ignoring an editor's mid-session project-root change once any command had run. `register_task_env_overrides` now pushes a new cwd onto the cached env so an explicit ACP cwd change wins, while ordinary in-session `cd` tracking is preserved. Regression coverage: - foreground/background commands follow live `env.cwd` - explicit `workdir` still overrides everything - registering a cwd override updates the live env cwd (ACP authority) - no-op when no live env exists; non-cwd overrides leave env.cwd untouched Based on #35510 by @Dusk1e. Co-authored-by: Dusk1e <yusufalweshdemir@gmail.com>
This commit is contained in:
parent
1044d9f25d
commit
7a315bd702
2 changed files with 197 additions and 2 deletions
|
|
@ -962,6 +962,23 @@ def register_task_env_overrides(task_id: str, overrides: Dict[str, Any]):
|
|||
"""
|
||||
_task_env_overrides[task_id] = overrides
|
||||
|
||||
# If a live environment already exists for this task, a freshly registered
|
||||
# ``cwd`` override (e.g. the ACP client switching the editor's project root
|
||||
# mid-session via ``session/load`` / ``session/resume``) must take effect on
|
||||
# the cached env too. ``terminal_tool`` resolves the per-command cwd as
|
||||
# ``workdir > env.cwd > config/override cwd`` so that ordinary in-session
|
||||
# ``cd`` state is preserved; without syncing here the override would sit
|
||||
# below the (already-set) ``env.cwd`` and be silently ignored once any
|
||||
# command has run. Pushing it onto the live env keeps ``cd`` tracking intact
|
||||
# while letting an explicit ACP cwd change win, as the client expects.
|
||||
new_cwd = overrides.get("cwd")
|
||||
if isinstance(new_cwd, str) and new_cwd.strip():
|
||||
container_id = _resolve_container_task_id(task_id)
|
||||
with _env_lock:
|
||||
env = _active_environments.get(container_id)
|
||||
if env is not None and getattr(env, "cwd", None) is not None:
|
||||
env.cwd = new_cwd
|
||||
|
||||
|
||||
def clear_task_env_overrides(task_id: str):
|
||||
"""
|
||||
|
|
@ -1718,6 +1735,30 @@ def _resolve_notification_flag_conflict(
|
|||
return watch_patterns, ""
|
||||
|
||||
|
||||
def _resolve_command_cwd(
|
||||
*,
|
||||
workdir: Optional[str],
|
||||
env: Any,
|
||||
default_cwd: str,
|
||||
) -> str:
|
||||
"""Return the cwd for a command, preferring the live session cwd.
|
||||
|
||||
``terminal_tool`` historically re-sent the init-time/config cwd on every
|
||||
call. That broke session-local ``cd`` state: the environment tracked the
|
||||
new directory in ``env.cwd``, but foreground/background calls kept forcing
|
||||
the old cwd back through ``env.execute(..., cwd=...)``. Explicit
|
||||
``workdir=`` must still override everything.
|
||||
"""
|
||||
if workdir:
|
||||
return workdir
|
||||
|
||||
live_cwd = getattr(env, "cwd", None)
|
||||
if isinstance(live_cwd, str) and live_cwd.strip():
|
||||
return live_cwd
|
||||
|
||||
return default_cwd
|
||||
|
||||
|
||||
def terminal_tool(
|
||||
command: str,
|
||||
background: bool = False,
|
||||
|
|
@ -1990,7 +2031,11 @@ def terminal_tool(
|
|||
from tools.process_registry import process_registry
|
||||
|
||||
session_key = get_current_session_key(default="")
|
||||
effective_cwd = workdir or cwd
|
||||
effective_cwd = _resolve_command_cwd(
|
||||
workdir=workdir,
|
||||
env=env,
|
||||
default_cwd=cwd,
|
||||
)
|
||||
try:
|
||||
if env_type == "local":
|
||||
proc_session = process_registry.spawn_local(
|
||||
|
|
@ -2207,7 +2252,11 @@ def terminal_tool(
|
|||
try:
|
||||
execute_kwargs = {
|
||||
"timeout": effective_timeout,
|
||||
"cwd": workdir or cwd,
|
||||
"cwd": _resolve_command_cwd(
|
||||
workdir=workdir,
|
||||
env=env,
|
||||
default_cwd=cwd,
|
||||
),
|
||||
}
|
||||
result = env.execute(command, **execute_kwargs)
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue