Skip to content

The agent that grows with you

fix(git): neutralize repository-named filters in the session snapshot and subagent worktrees

John Paul Soliva803cd0352159
diff --git a/agent/coding_context.py b/agent/coding_context.py
index 94a4f2be6c4..6a7417666e7 100644
--- a/agent/coding_context.py
+++ b/agent/coding_context.py
@@ -528,7 +528,13 @@ def build_coding_workspace_block(cwd: Optional[str | Path] = None) -> str:
f"- Root: {root}",
]
if git_root is not None:
- branch, counts = _parse_status(_git(root, "status", "--porcelain=2", "--branch"))
+ # ``status`` hashes re-timestamped files through the repo-named clean filter; skip it when
+ # the filter overrides cannot be discovered rather than run it half-hardened.
+ from hermes_cli._subprocess_compat import noninteractive_repo_git_env
+ status_env = noninteractive_repo_git_env(root)
+ status = "" if status_env is None else bounded_git_probe(
+ ["git", "-C", str(root), "status", "--porcelain=2", "--branch"], timeout=_GIT_TIMEOUT, env=status_env)
+ branch, counts = _parse_status(status)
head = branch.get("head", "")
if head == "(detached)":
lines.append("- Branch: (detached HEAD)")
diff --git a/hermes_cli/_subprocess_compat.py b/hermes_cli/_subprocess_compat.py
index 9ada8ab89d0..fdaadbe4e7b 100644
--- a/hermes_cli/_subprocess_compat.py
+++ b/hermes_cli/_subprocess_compat.py
@@ -775,7 +775,7 @@ def _close_job(job) -> None:
pass
-def bounded_git_probe(argv: Sequence[str], *, timeout: float) -> str:
+def bounded_git_probe(argv: Sequence[str], *, timeout: float, env: "Mapping[str, str] | None" = None) -> str:
"""Run a short ``git`` probe and return stripped stdout, or ``""`` on ANY failure.
On Windows ``run()``'s post-timeout cleanup calls an unbounded ``communicate()``; a suspended
@@ -801,7 +801,7 @@ def bounded_git_probe(argv: Sequence[str], *, timeout: float) -> str:
openai/codex#36793). ``process_group`` only changes which group the child belongs to; it does not detach
the terminal or alter the fast path.
"""
- result = bounded_probe_run(argv, timeout=timeout, env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV})
+ result = bounded_probe_run(argv, timeout=timeout, env={**(env or noninteractive_git_env()), **NO_LAZY_FETCH_ENV})
if result is None or result.returncode != 0:
return ""
return (result.stdout or "").strip()
diff --git a/tests/security/test_gitspawn_config_injection.py b/tests/security/test_gitspawn_config_injection.py
index 3ae191ac41c..875369472f3 100644
--- a/tests/security/test_gitspawn_config_injection.py
+++ b/tests/security/test_gitspawn_config_injection.py
@@ -234,6 +234,12 @@ def test_repo_named_filters_never_run_from_kanban_gc_or_hints(tmp_path):
os.utime(repo / "README", (time.time() + 60, time.time() + 60))
assert worktree_gc._git(["status", "--porcelain"], cwd=str(repo)).returncode == 0
assert git_state_hint(str(repo)) is not None
+ # The automatic session-start snapshot (status) and a delegated subagent's worktree (checkout).
+ from agent.coding_context import build_coding_workspace_block
+ from tools.subagent_worktree import create_subagent_worktree
+ assert "- Status:" in build_coding_workspace_block(repo)
+ sub = create_subagent_worktree(str(repo), "filters")
+ assert sub is not None and (Path(sub["path"]) / "README").read_text() == "hi\n"
assert sorted(p.name for p in tmp_path.glob("FILTER.*")) == []
diff --git a/tools/subagent_worktree.py b/tools/subagent_worktree.py
index b30dc5bbc37..f63191db9d8 100644
--- a/tools/subagent_worktree.py
+++ b/tools/subagent_worktree.py
@@ -22,7 +22,7 @@ logger = logging.getLogger(__name__)
_GIT_TIMEOUT = 30
-def _run_git(args, cwd: str, timeout: int = _GIT_TIMEOUT):
+def _run_git(args, cwd: str, timeout: int = _GIT_TIMEOUT, env=None):
"""Run git capturing output; never raises on non-zero exit.
:func:`noninteractive_git_env` (GHSA-7x36-8jrh-v4pw): this runs unattended against whatever
@@ -31,7 +31,7 @@ def _run_git(args, cwd: str, timeout: int = _GIT_TIMEOUT):
"""
return subprocess.run(["git", *harden_git_argv(args)], cwd=cwd, capture_output=True,
text=True, encoding="utf-8", errors="replace", timeout=timeout,
- stdin=subprocess.DEVNULL, env=noninteractive_git_env())
+ stdin=subprocess.DEVNULL, env=env or noninteractive_git_env())
def local_backend_active() -> bool:
@@ -85,7 +85,13 @@ def create_subagent_worktree(parent_cwd: Optional[str], subagent_id: Optional[st
_ensure_gitignore_entry(repo_root)
base = _run_git(["rev-parse", "HEAD"], cwd=repo_root)
base_commit = base.stdout.strip() if base.returncode == 0 else ""
- result = _run_git(["worktree", "add", str(wt_path), "-b", branch, "HEAD"], cwd=repo_root)
+ # The checkout runs the repo-named smudge filter; skip isolation when it cannot be neutralized.
+ from hermes_cli._subprocess_compat import noninteractive_repo_git_env
+ add_env = noninteractive_repo_git_env(repo_root)
+ if add_env is None:
+ logger.warning("subagent worktree: filter discovery failed; not creating a worktree")
+ return None
+ result = _run_git(["worktree", "add", str(wt_path), "-b", branch, "HEAD"], cwd=repo_root, env=add_env)
except Exception as exc:
logger.warning("subagent worktree: creation failed: %s", exc)
return None