Skip to content

The agent that grows with you

refactor(tools): collapse cwd/interpreter ladders in code_execution_env

Teknium9a9b719533d1
diff --git a/tools/code_execution_env.py b/tools/code_execution_env.py
index 9b44675ec13..e8039c03daa 100644
--- a/tools/code_execution_env.py
+++ b/tools/code_execution_env.py
@@ -210,17 +210,14 @@ def _uses_hermes_python_environment(python_path: str) -> bool:
def _resolve_child_python(mode: str) -> str:
- """Interpreter for the child: ``sys.executable`` in strict mode; in project
- mode the active VIRTUAL_ENV/CONDA_PREFIX python if it exists and passes the
- 3.8+ probe, else ``sys.executable``."""
+ """Child interpreter: ``sys.executable`` in strict mode; in project mode the active
+ VIRTUAL_ENV/CONDA_PREFIX python if it exists and passes the 3.8+ probe, else ``sys.executable``."""
if mode != "project":
return sys.executable
subdir, exe_names = ("Scripts", ("python.exe", "python3.exe")) if _IS_WINDOWS else ("bin", ("python", "python3"))
for var in ("VIRTUAL_ENV", "CONDA_PREFIX"):
root = os.environ.get(var, "").strip()
- if not root:
- continue
- for exe in exe_names:
+ for exe in exe_names if root else ():
candidate = os.path.join(root, subdir, exe)
if not (os.path.isfile(candidate) and os.access(candidate, os.X_OK)):
continue
@@ -255,11 +252,7 @@ def _resolve_child_cwd(mode: str, staging_dir: str, task_id: str = "") -> str:
return session_cwd
from agent.runtime_cwd import scope_terminal_cwd
raw = scope_terminal_cwd().strip()
- if raw:
- expanded = os.path.expanduser(raw)
- if os.path.isdir(expanded):
- return expanded
- here = os.getcwd()
- if os.path.isdir(here):
- return here
+ for candidate in (os.path.expanduser(raw) if raw else "", os.getcwd()):
+ if candidate and os.path.isdir(candidate):
+ return candidate
return staging_dir
diff --git a/tools/code_kernel.py b/tools/code_kernel.py
index 1a9d4123544..14f13cd8229 100644
--- a/tools/code_kernel.py
+++ b/tools/code_kernel.py
@@ -530,9 +530,8 @@ def _cell_result(kernel: SessionKernel, key: Tuple, status: str, payload: Dict[s
duration = round(time.monotonic() - exec_start, 2)
kernel.execution_count = int(payload.get("execution_count", kernel.execution_count + 1))
stderr_raw = kernel.stderr.drain()
- stdout_text = clean(str(payload.get("stdout", "")) + kernel.raw.drain())
+ stdout_text, stdout_metadata = _truncate_stdout_text(clean(str(payload.get("stdout", "")) + kernel.raw.drain()))
cell_stderr = clean(str(payload.get("stderr", "")) + stderr_raw)
- stdout_text, stdout_metadata = _truncate_stdout_text(stdout_text)
cell_status = payload.get("status", "")
result: Dict[str, Any] = {
"status": status, "output": stdout_text, "exit_code": 0,
@@ -602,9 +601,7 @@ def execute_in_session_kernel(
assert kernel.proc is not None and kernel.proc.stdin is not None
# Per-cell tool budget: the RPC loop enforces counter < max; reset without restarting.
kernel.tool_call_counter[0] = 0
- # Anything raw that leaked between cells belongs to no cell.
- kernel.raw.drain()
- kernel.stderr.drain()
+ kernel.raw.drain(), kernel.stderr.drain() # raw output leaked between cells belongs to no cell
kernel.cell_authority = authority
kernel.proc.stdin.write((json.dumps({"id": uuid.uuid4().hex, "code": code}) + "\n").encode("utf-8"))
kernel.proc.stdin.flush()