mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-21 16:18:55 +00:00
fix(execute-code): honor session cwd overrides
This commit is contained in:
parent
298a94926f
commit
4d6686c18a
2 changed files with 61 additions and 14 deletions
|
|
@ -1339,7 +1339,7 @@ def execute_code(
|
|||
# Env scrubbing and tool whitelist apply identically in both modes.
|
||||
_mode = _get_execution_mode()
|
||||
_child_python = _resolve_child_python(_mode)
|
||||
_child_cwd = _resolve_child_cwd(_mode, tmpdir, task_id=task_id)
|
||||
_child_cwd = _resolve_child_cwd(_mode, tmpdir, task_id=task_id or "")
|
||||
_script_path = os.path.join(tmpdir, "script.py")
|
||||
|
||||
proc = subprocess.Popen(
|
||||
|
|
@ -1745,29 +1745,43 @@ def _resolve_child_python(mode: str) -> str:
|
|||
return sys.executable
|
||||
|
||||
|
||||
def _resolve_child_cwd(mode: str, staging_dir: str, task_id: Optional[str] = None) -> str:
|
||||
def _resolve_child_cwd(mode: str, staging_dir: str, task_id: str = "") -> str:
|
||||
"""Resolve the working directory for the execute_code subprocess.
|
||||
|
||||
- ``strict``: the staging tmpdir (today's behavior).
|
||||
- ``project``: the session's registered cwd override (from
|
||||
``session.cwd.set``), then ``TERMINAL_CWD``, then ``os.getcwd()``.
|
||||
Falls back to the staging tmpdir as a last resort so we never invoke
|
||||
Popen with a nonexistent cwd.
|
||||
- ``project``: the session's own cwd — its per-session cwd record
|
||||
(written after every completed terminal command), then the raw
|
||||
per-session cwd override registered via ``session.cwd.set`` /
|
||||
``register_task_env_overrides``, then the session's TERMINAL_CWD
|
||||
(same as the terminal tool), or ``os.getcwd()`` if none points at a
|
||||
real dir. Falls back to the staging tmpdir as a last resort so we
|
||||
never invoke Popen with a nonexistent cwd.
|
||||
|
||||
This mirrors the resolution ladder file tools and the terminal use
|
||||
(record → registered override → TERMINAL_CWD), so all file-writing
|
||||
paths within a session agree on the working directory. (#56047)
|
||||
"""
|
||||
if mode != "project":
|
||||
return staging_dir
|
||||
# Check per-session cwd override first (registered via session.cwd.set
|
||||
# → register_task_env_overrides). This is the same lookup used by
|
||||
# write_file/read_file/patch/terminal so all tool paths agree on the
|
||||
# working directory within a session. (#56047)
|
||||
if task_id:
|
||||
# 1. The session's cwd record — IS the session's `cd` state.
|
||||
try:
|
||||
from tools.terminal_tool import get_session_cwd
|
||||
|
||||
recorded = get_session_cwd(task_id)
|
||||
except Exception:
|
||||
recorded = None
|
||||
if recorded and os.path.isdir(recorded):
|
||||
return recorded
|
||||
# 2. Registered workspace override (session.cwd.set → gateway/TUI/ACP).
|
||||
try:
|
||||
from tools.file_tools import _registered_task_cwd_override
|
||||
override = _registered_task_cwd_override(task_id)
|
||||
if override and os.path.isdir(override):
|
||||
return override
|
||||
|
||||
session_cwd = _registered_task_cwd_override(task_id)
|
||||
except Exception:
|
||||
pass
|
||||
session_cwd = None
|
||||
if session_cwd and os.path.isdir(session_cwd):
|
||||
return session_cwd
|
||||
raw = os.environ.get("TERMINAL_CWD", "").strip()
|
||||
if raw:
|
||||
expanded = os.path.expanduser(raw)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue