fix(tools): isolate per-session worktree cwd

This commit is contained in:
Brooklyn Nicholson
2026-06-25 16:40:27 -05:00
parent 4ffdedd369
commit cb3f8ec03d
4 changed files with 129 additions and 7 deletions
+15 -2
View File
@@ -2290,14 +2290,27 @@ def terminal_tool(
"EOF."
)
# Claim the (shared "default") terminal env for the session driving this
# command. File tools read env.cwd_owner to decide whether the env's live
# cwd is THIS session's `cd` or a different worktree session's — without
# it, two open worktree sessions sharing the env route each other's edits
# to the wrong checkout. get_current_session_key()'s contextvar doesn't
# cross tool-worker threads, so fall back to the raw task_id (which IS the
# session_key for the top-level agent) — a stable, thread-safe anchor.
from tools.approval import get_current_session_key
session_key = get_current_session_key(default="") or (task_id or "")
try:
env.cwd_owner = session_key
except Exception:
pass
if background:
# Spawn a tracked background process via the process registry.
# For local backends: uses subprocess.Popen with output buffering.
# For non-local backends: runs inside the sandbox via env.execute().
from tools.approval import get_current_session_key
from tools.process_registry import process_registry
session_key = get_current_session_key(default="")
effective_cwd = _resolve_command_cwd(
workdir=workdir,
env=env,