The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): skip the restore import check only for non-runtime files
Tekniuma3ed4a173070
diff --git a/hermes_cli/update_cmd_stash.py b/hermes_cli/update_cmd_stash.pyindex 2994ec07137..75bde52b835 100644--- a/hermes_cli/update_cmd_stash.py+++ b/hermes_cli/update_cmd_stash.py@@ -333,6 +333,53 @@ def _restored_python_paths(git_cmd: list[str], cwd: Path) -> tuple[str, ...] | Nreturn tuple(sorted(paths))+#: Closed allowlist of suffixes that no Python import can load or read during startup.+#: Everything else (.py, .so/.pyd/.dll/.dylib, .pyc, .pth, .egg-link, data files such as+#: .json/.yaml/.toml that modules read on import, extensionless files) keeps the import check.+_NON_RUNTIME_SUFFIXES = frozenset({+ ".md", ".mdx", ".rst", ".txt", ".adoc",+ ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico",+})+++def _restore_is_non_runtime(git_cmd: list[str], cwd: Path, stash_ref: str) -> bool:+ """True only when every path the stash restores is demonstrably unable to change an import.++ The set is read from the stash commit itself (worktree and index changes against its base,+ renames split into delete + add, plus its untracked tree), never from the working tree,+ which also holds what the clean import probe wrote. Each path must have an allowlisted+ suffix, must not be a symlink, and may only sit in a directory ``HEAD`` already tracks, so+ the restore cannot create a namespace-package directory. Any Git failure counts as+ runtime-affecting.+ """+ restored: set[str] = set()+ for side in (stash_ref, f"{stash_ref}^2"): # worktree, index+ changed = _git_paths_z(+ git_cmd, ["diff", "--name-only", "--no-renames", "-z", f"{stash_ref}^1", side, "--"], cwd)+ if changed is None:+ return False+ restored |= changed+ has_untracked = _git_quiet(git_cmd, ["rev-parse", "-q", "--verify", f"{stash_ref}^3"], cwd)+ if has_untracked is None:+ return False+ if has_untracked.returncode == 0:+ untracked = _git_paths_z(git_cmd, ["ls-tree", "-r", "--name-only", "-z", f"{stash_ref}^3"], cwd)+ if untracked is None:+ return False+ restored |= untracked+ if not restored:+ return True+ head_dirs = _git_paths_z(git_cmd, ["ls-tree", "-r", "-d", "--name-only", "-z", "HEAD"], cwd)+ if head_dirs is None:+ return False+ for path in restored:+ parent = Path(path).parent.as_posix()+ if (Path(path).suffix.lower() not in _NON_RUNTIME_SUFFIXES or (cwd / path).is_symlink()+ or (parent != "." and parent not in head_dirs)):+ return False+ return True++def _reject_unsafe_stash_restore(git_cmd: list[str], cwd: Path, stash_ref: str, preexisting_untracked: set[str], failing_target: str,detail: str | None, *, checkout_move=None,@@ -492,11 +539,13 @@ def _restore_stashed_changes(syntax_ok, failing_path, syntax_error = _validate_python_files_syntax(cwd, restored_python)if not syntax_ok:reject(failing_path or "restored Python source", syntax_error)- # Only restored Python can be blamed for an import failure. Probe outcomes also move with- # install state between the two runs (launch preparation, dependency sync), so a restore- # that touched no Python source must not be rejected on that difference (#130101).- for module, error in (_critical_module_import_failures(cwd, report_runtime_errors=True).items()- if restored_python else ()):+ # Probe outcomes also move with install state between the two runs (launch preparation,+ # dependency sync), so a restore that cannot affect an import (docs, images) must not be+ # rejected on that difference (#130101). Anything else, including a native extension or+ # bytecode that shadows a healthy module, is still compared.+ non_runtime = _restore_is_non_runtime(git_cmd, cwd, stash_ref)+ for module, error in (() if non_runtime else+ _critical_module_import_failures(cwd, report_runtime_errors=True).items()):if clean_import_failures.get(module) != error:reject(f"agent import {module or 'unknown'}", error[1])breakdiff --git a/tests/hermes_cli/test_update_autostash.py b/tests/hermes_cli/test_update_autostash.pyindex 9b30d0fc17b..57894cbdb7b 100644--- a/tests/hermes_cli/test_update_autostash.py+++ b/tests/hermes_cli/test_update_autostash.py@@ -172,6 +172,38 @@ def test_restore_without_python_is_not_judged_by_install_state_imports(probe_rooassert not git(probe_root, 'stash', 'list')[email protected]('kind', ['native-extension', 'data-file'])+def test_restore_without_python_that_breaks_an_import_is_rejected(probe_root, monkeypatch, capsys, kind):+ """Only demonstrably non-runtime restores skip the import check. A restored native extension+ takes precedence over the healthy ``consumer.py``, and a data file read at import time is+ runtime input too; neither is ``.py``, and both must still reject the restore."""+ import importlib.machinery++ git(probe_root, 'init', '-q', '-b', 'main')+ (probe_root / 'consumer.py').write_text(+ "import json, pathlib\nVALUE = json.loads(pathlib.Path(__file__).with_name('consumer.json').read_text())\n",+ encoding='utf-8')+ (probe_root / 'consumer.json').write_text('1\n', encoding='utf-8')+ git(probe_root, 'add', '.')+ git(probe_root, '-c', 'user.name=Fixture', '-c', '[email protected]',+ '-c', 'commit.gpgsign=false', 'commit', '-qm', 'base')+ if kind == 'native-extension':+ local = probe_root / ('consumer' + importlib.machinery.EXTENSION_SUFFIXES[0])+ local.write_bytes(b'not a native extension\n')+ else:+ local = probe_root / 'consumer.json'+ local.write_text('{not json\n', encoding='utf-8')+ ref = hermes_main._stash_local_changes_if_needed(['git'], probe_root)+ monkeypatch.setattr(update_cmd, '_UPDATE_CRITICAL_MODULES', ('consumer',))++ with pytest.raises(SystemExit) as error:+ hermes_main._restore_stashed_changes(['git'], probe_root, ref, prompt_user=False)+ assert error.value.code == 1+ assert 'agent import consumer' in capsys.readouterr().out+ assert not git(probe_root, 'status', '--porcelain')+ assert git(probe_root, 'stash', 'list')++@pytest.mark.parametrize('error', [EOFError(), UnicodeDecodeError('utf-8', b'\xff', 0, 1, 'invalid')])def test_unreadable_stash_prompt_keeps_work(tmp_path, monkeypatch, capsys, error):def unreadable(*_):