Skip to content

The agent that grows with you

fix(update): fail closed on incomplete restore checks

fangliquanflq53cf38c3b8e6
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 929fb333fe0..bb33bb05af6 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -711,14 +711,12 @@ def _critical_module_import_failures(
" if %r:\n"
" failures.append((name, str(exc)))\n"
" except BaseException as exc:\n"
- " if %r:\n"
- " failures.append((name, str(exc)))\n"
+ " failures.append((name, str(exc)))\n"
"sys.stdout.write('\\n%s' + json.dumps(failures))\n"
% (
_UPDATE_CRITICAL_MODULES,
tuple(sorted(FIRST_PARTY_MODULE_ROOTS)),
report_runtime_errors,
- report_runtime_errors,
marker,
)
)
@@ -2498,7 +2496,9 @@ def _git_untracked_paths(git_cmd: list[str], cwd: Path) -> set[str] | None:
return {path for path in result.stdout.split("\0") if path}
-def _restored_python_paths(git_cmd: list[str], cwd: Path) -> tuple[str, ...]:
+def _restored_python_paths(
+ git_cmd: list[str], cwd: Path
+) -> tuple[str, ...] | None:
"""Return restored ``.py`` paths changed from ``HEAD``.
This deliberately validates Python source only; non-Python entry scripts
@@ -2512,10 +2512,14 @@ def _restored_python_paths(git_cmd: list[str], cwd: Path) -> tuple[str, ...]:
encoding="utf-8",
errors="surrogateescape",
)
- paths = set(changed.stdout.split("\0")) if changed.returncode == 0 else set()
+ if changed.returncode != 0:
+ print(" ⚠ Could not enumerate tracked Python files restored from the stash.")
+ return None
+ paths = set(changed.stdout.split("\0"))
untracked = _git_untracked_paths(git_cmd, cwd)
- if untracked is not None:
- paths.update(path for path in untracked if path.endswith(".py"))
+ if untracked is None:
+ return None
+ paths.update(path for path in untracked if path.endswith(".py"))
paths.discard("")
return tuple(sorted(paths))
@@ -2669,6 +2673,15 @@ def _restore_stashed_changes(
return False
restored_python = _restored_python_paths(git_cmd, cwd)
+ if restored_python is None:
+ _reject_unsafe_stash_restore(
+ git_cmd,
+ cwd,
+ stash_ref,
+ preexisting_untracked,
+ "restored Python source discovery",
+ "could not determine which restored Python files require validation",
+ )
syntax_ok, failing_path, syntax_error = _validate_python_files_syntax(
cwd, restored_python
)
diff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.py
index a2e73f7046c..696e7baf413 100644
--- a/tests/hermes_cli/test_update_autostash.py
+++ b/tests/hermes_cli/test_update_autostash.py
@@ -717,6 +717,49 @@ def test_restore_stays_parked_when_untracked_baseline_is_unknown(
assert "git stash apply stash@{0}" in output
+def test_restore_rejects_unknown_restored_python_paths(
+ monkeypatch, tmp_path, capsys
+):
+ """A failed post-apply path query cannot skip restored syntax validation."""
+ import subprocess
+ from hermes_cli import update_cmd
+
+ def git(*args, check=True):
+ return subprocess.run(
+ ["git", *args],
+ cwd=tmp_path,
+ capture_output=True,
+ text=True,
+ check=check,
+ )
+
+ git("init", "-q", "-b", "main")
+ git("config", "user.email", "[email protected]")
+ git("config", "user.name", "t")
+ source = tmp_path / "consumer.py"
+ source.write_text("VALUE = 1\n", encoding="utf-8")
+ git("add", "-A")
+ git("commit", "-qm", "init")
+ source.write_text("VALUE = 2\n", encoding="utf-8")
+ stash_ref = hermes_main._stash_local_changes_if_needed(["git"], tmp_path)
+ assert stash_ref
+ monkeypatch.setattr(update_cmd, "_UPDATE_CRITICAL_MODULES", ())
+ monkeypatch.setattr(update_cmd, "_restored_python_paths", lambda *_args: None)
+
+ with pytest.raises(SystemExit) as exc_info:
+ hermes_main._restore_stashed_changes(
+ ["git"], tmp_path, stash_ref, prompt_user=False
+ )
+
+ assert exc_info.value.code == 1
+ assert source.read_text(encoding="utf-8") == "VALUE = 1\n"
+ assert git("status", "--porcelain").stdout == ""
+ assert git("stash", "list").stdout.strip()
+ output = capsys.readouterr().out
+ assert "restored Python source discovery" in output
+ assert "gateway was not restarted" in output
+
+
def test_gateway_restore_prompt_defaults_to_keep_stash(tmp_path, capsys):
prompts = []
diff --git a/tests/hermes_cli/test_update_import_guard.py b/tests/hermes_cli/test_update_import_guard.py
index 36a32797a01..51d62d9e8f1 100644
--- a/tests/hermes_cli/test_update_import_guard.py
+++ b/tests/hermes_cli/test_update_import_guard.py
@@ -124,6 +124,18 @@ def test_import_guard_reports_probe_termination_by_default(monkeypatch, tmp_path
assert error == "terminated before reporting import health (exit code 9)"
+def test_import_guard_reports_system_exit_by_default(monkeypatch, tmp_path):
+ """Catchable terminating imports must not complete with a healthy marker."""
+ (tmp_path / "consumer.py").write_text("raise SystemExit('stopped')\n")
+ monkeypatch.setattr(update_cmd, "_UPDATE_CRITICAL_MODULES", ("consumer",))
+
+ ok, module, error = hermes_main._validate_critical_modules_import(tmp_path)
+
+ assert ok is False
+ assert module == "consumer"
+ assert error == "stopped"
+
+
def test_import_guard_does_not_accept_forged_static_marker(monkeypatch, tmp_path):
"""Imported stdout cannot impersonate the per-probe completion marker."""
(tmp_path / "consumer.py").write_text(