Skip to content

The agent that grows with you

fix(update): preserve unknown restore cleanup state

fangliquanflq3e4dc5f2c31a
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index c2b679efe48..929fb333fe0 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -741,6 +741,10 @@ def _critical_module_import_failures(
errors="replace",
timeout=120,
)
+ except subprocess.TimeoutExpired:
+ return {
+ "critical-module probe": "timed out before reporting import health"
+ }
except (OSError, subprocess.SubprocessError):
# Can't run the probe — don't block the update on our own tooling.
return {}
@@ -2475,8 +2479,8 @@ def _park_stashed_changes(stash_ref: str) -> None:
print(f" Restore manually with: git stash apply {stash_ref}")
-def _git_untracked_paths(git_cmd: list[str], cwd: Path) -> set[str]:
- """Return untracked, non-ignored paths without Git's quoting ambiguity."""
+def _git_untracked_paths(git_cmd: list[str], cwd: Path) -> set[str] | None:
+ """Return untracked paths, or ``None`` when Git cannot enumerate them."""
result = subprocess.run(
git_cmd + ["ls-files", "--others", "--exclude-standard", "-z"],
cwd=cwd,
@@ -2490,7 +2494,7 @@ def _git_untracked_paths(git_cmd: list[str], cwd: Path) -> set[str]:
" ⚠ Could not enumerate untracked files while validating the "
"restored stash."
)
- return set()
+ return None
return {path for path in result.stdout.split("\0") if path}
@@ -2509,7 +2513,9 @@ def _restored_python_paths(git_cmd: list[str], cwd: Path) -> tuple[str, ...]:
errors="surrogateescape",
)
paths = set(changed.stdout.split("\0")) if changed.returncode == 0 else set()
- paths.update(path for path in _git_untracked_paths(git_cmd, cwd) if path.endswith(".py"))
+ untracked = _git_untracked_paths(git_cmd, cwd)
+ if untracked is not None:
+ paths.update(path for path in untracked if path.endswith(".py"))
paths.discard("")
return tuple(sorted(paths))
@@ -2530,7 +2536,12 @@ def _reject_unsafe_stash_restore(
for line in str(detail).splitlines()[:6]:
print(f" {line}")
- restored_untracked = _git_untracked_paths(git_cmd, cwd) - preexisting_untracked
+ current_untracked = _git_untracked_paths(git_cmd, cwd)
+ restored_untracked = (
+ current_untracked - preexisting_untracked
+ if current_untracked is not None
+ else set()
+ )
subprocess.run(
git_cmd + ["reset", "--hard", "HEAD"], cwd=cwd, capture_output=True
)
@@ -2540,6 +2551,9 @@ def _reject_unsafe_stash_restore(
cwd=cwd,
capture_output=True,
)
+ elif current_untracked is None:
+ print(" ⚠ Untracked restore leftovers could not be cleaned automatically.")
+ print(" Inspect `git status` before retrying the stash.")
print(" The clean updated tree has been restored; the gateway was not restarted.")
print(" Platform connectivity alone does not mean the agent can execute turns.")
@@ -2586,6 +2600,10 @@ def _restore_stashed_changes(
return False
preexisting_untracked = _git_untracked_paths(git_cmd, cwd)
+ if preexisting_untracked is None:
+ print(" The stash was not restored because its cleanup baseline is unknown.")
+ print(f" Restore manually with: git stash apply {stash_ref}")
+ return False
clean_import_failures = _critical_module_import_failures(
cwd, report_runtime_errors=True
)
diff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.py
index 6839c895de3..a2e73f7046c 100644
--- a/tests/hermes_cli/test_update_autostash.py
+++ b/tests/hermes_cli/test_update_autostash.py
@@ -699,6 +699,24 @@ def test_restore_rejects_probe_termination(monkeypatch, tmp_path, capsys):
assert "gateway was not restarted" in output
+def test_restore_stays_parked_when_untracked_baseline_is_unknown(
+ monkeypatch, tmp_path, capsys
+):
+ """Unknown cleanup scope must not turn into a destructive empty baseline."""
+ from hermes_cli import update_cmd
+
+ monkeypatch.setattr(update_cmd, "_git_untracked_paths", lambda *_args: None)
+
+ restored = hermes_main._restore_stashed_changes(
+ ["git"], tmp_path, "stash@{0}", prompt_user=False
+ )
+
+ assert restored is False
+ output = capsys.readouterr().out
+ assert "cleanup baseline is unknown" in output
+ assert "git stash apply stash@{0}" 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 5854252f85b..36a32797a01 100644
--- a/tests/hermes_cli/test_update_import_guard.py
+++ b/tests/hermes_cli/test_update_import_guard.py
@@ -160,6 +160,21 @@ def test_import_guard_rejects_malformed_health_payload(monkeypatch, tmp_path):
assert error == "reported malformed import health data"
+def test_import_guard_reports_probe_timeout(monkeypatch, tmp_path):
+ import subprocess
+
+ def timeout(*_args, **_kwargs):
+ raise subprocess.TimeoutExpired(["python", "-c", "probe"], 120)
+
+ monkeypatch.setattr(update_cmd.subprocess, "run", timeout)
+
+ ok, module, error = hermes_main._validate_critical_modules_import(tmp_path)
+
+ assert ok is False
+ assert module == "critical-module probe"
+ assert error == "timed out before reporting import health"
+
+
def test_untracked_enumeration_failure_is_visible(monkeypatch, tmp_path, capsys):
class Result:
returncode = 1
@@ -167,7 +182,7 @@ def test_untracked_enumeration_failure_is_visible(monkeypatch, tmp_path, capsys)
monkeypatch.setattr(update_cmd.subprocess, "run", lambda *_a, **_kw: Result())
- assert update_cmd._git_untracked_paths(["git"], tmp_path) == set()
+ assert update_cmd._git_untracked_paths(["git"], tmp_path) is None
assert "Could not enumerate untracked files" in capsys.readouterr().out