Skip to content

The agent that grows with you

fix(goals): run quality gates in the session workspace, never elsewhere

Jay Zhou42369fba9715
diff --git a/hermes_cli/goals.py b/hermes_cli/goals.py
index 500096ef87c..67a904aebfa 100644
--- a/hermes_cli/goals.py
+++ b/hermes_cli/goals.py
@@ -371,6 +371,27 @@ class GoalGate:
)
+def _gate_workspace() -> Tuple[Optional[str], Optional[str]]:
+ """``(cwd, refusal)`` for this check's gates. A multi-session backend's process directory is not
+ the session's project, so gates run in the scoped session workspace (#125369). A declared
+ workspace that is not a directory on this host (deleted, remote, container) is a refusal: a
+ relative gate run anywhere else would check a different project and could pass a failing goal.
+ No declared workspace keeps the classic resolution (TERMINAL_CWD, else the launch directory)."""
+ from agent.runtime_cwd import resolve_agent_cwd, scoped_session_cwd
+
+ declared = scoped_session_cwd()
+ if declared:
+ path = Path(declared).expanduser()
+ if path.is_dir():
+ return str(path), None
+ return None, (f"[gate not run: the session workspace {declared} is not a directory on this host, "
+ "and running the gate anywhere else would check a different project]")
+ try:
+ return str(resolve_agent_cwd()), None
+ except OSError:
+ return None, None # deleted launch directory: subprocess reports it per gate
+
+
def run_gate(gate: GoalGate, *, cwd: Optional[str] = None) -> Tuple[bool, int, str]:
"""Run one gate through the shell. Returns ``(passed, exit_code, output_tail)``; a timeout kills
the process and counts as exit code -1."""
@@ -1289,8 +1310,9 @@ class GoalManager:
if state is None or not state.gates:
return None
+ gate_cwd, refusal = _gate_workspace()
for gate in state.gates:
- passed, exit_code, tail = run_gate(gate)
+ passed, exit_code, tail = (False, -1, refusal) if refusal else run_gate(gate, cwd=gate_cwd)
gate.last_exit_code = exit_code
gate.last_output_tail = tail
if passed:
diff --git a/tests/hermes_cli/test_goal_gates.py b/tests/hermes_cli/test_goal_gates.py
index 8dcc68ed6f7..b82eee0790e 100644
--- a/tests/hermes_cli/test_goal_gates.py
+++ b/tests/hermes_cli/test_goal_gates.py
@@ -243,3 +243,78 @@ def test_no_gates_behaves_exactly_as_before():
mock_judge.assert_called_once()
assert decision["verdict"] == "continue"
assert decision["should_continue"] is True
+
+
+# ──────────────────────────────────────────────────────────────────────
+# Gate working directory (#125369)
+# ──────────────────────────────────────────────────────────────────────
+
+
+def backend_and_session(tmp_path, monkeypatch):
+ """A backend started in a project whose check passes, serving a session whose check fails."""
+ from agent.runtime_cwd import reset_session_cwd, set_session_cwd
+
+ backend, session = tmp_path / "backend", tmp_path / "session"
+ for folder, code in ((backend, 0), (session, 1)):
+ folder.mkdir()
+ (folder / "check.sh").write_text(f"pwd\nexit {code}\n", encoding="utf-8")
+ monkeypatch.chdir(backend)
+ monkeypatch.delenv("TERMINAL_CWD", raising=False)
+
+ def bind(cwd):
+ token = set_session_cwd(str(cwd))
+ return lambda: reset_session_cwd(token)
+
+ return backend, session, bind
+
+
+def _evaluate_with_done_judge(mgr):
+ with patch("hermes_cli.goals.judge_goal", return_value=("done", "all good", False, None, False)) as judge:
+ return mgr.evaluate_after_turn("ready"), judge
+
+
+def test_gate_runs_in_the_session_workspace_not_the_backend_directory(backend_and_session):
+ backend, session, bind = backend_and_session
+ mgr = _mgr_with_goal("gate-cwd-sid")
+ mgr.add_gate("sh check.sh")
+ unbind = bind(session)
+ try:
+ decision, judge = _evaluate_with_done_judge(mgr)
+ finally:
+ unbind()
+ judge.assert_not_called()
+ assert decision["verdict"] == "gate_failed"
+ assert mgr.state.gates[0].last_exit_code == 1
+ assert str(session.resolve()) in mgr.state.gates[0].last_output_tail
+
+
+def test_missing_session_workspace_fails_the_gate_instead_of_running_elsewhere(backend_and_session, tmp_path):
+ # A deleted, remote or container workspace: the backend's passing check must not stand in for it.
+ backend, _session, bind = backend_and_session
+ missing = tmp_path / "gone"
+ mgr = _mgr_with_goal("gate-missing-cwd-sid")
+ mgr.add_gate("sh check.sh")
+ unbind = bind(missing)
+ try:
+ decision, judge = _evaluate_with_done_judge(mgr)
+ finally:
+ unbind()
+ judge.assert_not_called()
+ assert decision["verdict"] == "gate_failed"
+ gate = mgr.state.gates[0]
+ assert gate.last_exit_code == -1
+ assert str(missing) in gate.last_output_tail and str(backend) not in gate.last_output_tail
+
+
+def test_gate_without_a_session_workspace_keeps_the_launch_directory(backend_and_session):
+ from agent.runtime_cwd import clear_session_cwd
+
+ backend, _session, _bind = backend_and_session
+ clear_session_cwd()
+ mgr = _mgr_with_goal("gate-launch-cwd-sid")
+ mgr.add_gate("sh check.sh")
+ decision, judge = _evaluate_with_done_judge(mgr)
+ judge.assert_called_once()
+ assert decision["verdict"] == "done"
+ assert str(backend.resolve()) in mgr.state.gates[0].last_output_tail