The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: trim lockfile-churn workspace tests to two invariants; restore docstring WHY
teknium1a10ca8fc3e32
diff --git a/hermes_cli/update_cmd_git.py b/hermes_cli/update_cmd_git.pyindex 37bdb8d7ad6..77a03a299e8 100644--- a/hermes_cli/update_cmd_git.py+++ b/hermes_cli/update_cmd_git.py@@ -422,7 +422,9 @@ def _ensure_non_trampoline_git(git_cmd: list) -> list:def _npm_lockfile_owners(repo_root: Path) -> set[Path]:- """Return manifest directories covered by the root npm lockfile."""+ """Manifest directories whose specs the single root ``package-lock.json`` records: the root plus every+ workspace from the root ``workspaces`` globs (same model as ``update_cmd_deps._npm_manifest_paths``).+ A manifest outside that graph (``website/``, ``scripts/whatsapp-bridge/``) has its own lockfile."""owners = {Path(".")}try:import json@@ -440,7 +442,11 @@ def _npm_lockfile_owners(repo_root: Path) -> set[Path]:def _discard_lockfile_churn(git_cmd, repo_root):- """Restore npm lockfile churn unless its owning manifest is dirty."""+ """Restore ``package-lock.json`` files npm rewrote non-deterministically, so the update sees a clean tree+ instead of autostashing every run. A lockfile is kept when a manifest it records is dirty: for the root+ lock that is the root or ANY workspace ``package.json`` (reverting it under a dirty ``apps/desktop``+ manifest desyncs spec and lock and every later ``npm ci`` fails, #112378); a nested lock is kept only+ with its sibling manifest. Best-effort."""from hermes_cli.update_cmd import _git_runwith suppress(Exception):diff = _git_run(git_cmd, ["diff", "--name-only"], repo_root)diff --git a/tests/hermes_cli/test_discard_lockfile_churn.py b/tests/hermes_cli/test_discard_lockfile_churn.pyindex 89b87d2612d..2c4347efa64 100644--- a/tests/hermes_cli/test_discard_lockfile_churn.py+++ b/tests/hermes_cli/test_discard_lockfile_churn.py@@ -1,77 +1,55 @@-"""Behavioral tests for npm lockfile ownership cleanup (#112378)."""+"""`hermes update` lockfile-churn cleanup must respect npm workspace ownership (#112378).++The single root ``package-lock.json`` spans every workspace declared in the root ``package.json``+``workspaces`` globs, so a dirty workspace manifest (``apps/desktop/package.json``) protects it;+a dirty manifest outside the workspace graph does not.+"""import json+import osimport subprocess-from pathlib import Pathfrom hermes_cli.update_cmd_git import _discard_lockfile_churn--def _repo(tmp_path):- subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)- (tmp_path / "package.json").write_text(json.dumps({"workspaces": ["apps/*", "ui-tui", "web"]}))- (tmp_path / "package-lock.json").write_text("root clean\n")- (tmp_path / "apps" / "desktop").mkdir(parents=True)- (tmp_path / "apps" / "desktop" / "package.json").write_text("desktop clean\n")- (tmp_path / "ui-tui").mkdir()- (tmp_path / "ui-tui" / "package.json").write_text("tui clean\n")- subprocess.run(["git", "add", "."], cwd=tmp_path, check=True)- subprocess.run(["git", "commit", "-qm", "initial"], cwd=tmp_path, check=True)- return tmp_path---def _dirty(path, text="dirty\n"):- path.write_text(text)+_GIT_ENV = {+ **os.environ,+ "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "[email protected]",+ "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "[email protected]",+}-def _changed(repo):- return subprocess.run(["git", "diff", "--name-only"], cwd=repo, text=True, capture_output=True, check=True).stdout.splitlines()+def _git(repo, *args):+ return subprocess.run(["git", *args], cwd=repo, text=True, capture_output=True, check=True, env=_GIT_ENV).stdout-def test_dirty_workspace_manifest_preserves_root_lock(tmp_path):- repo = _repo(tmp_path)- _dirty(repo / "apps/desktop/package.json")- _dirty(repo / "package-lock.json")- _discard_lockfile_churn(["git"], repo)- assert "package-lock.json" in _changed(repo)---def test_lock_only_churn_is_discarded(tmp_path):- repo = _repo(tmp_path)- _dirty(repo / "package-lock.json")- _discard_lockfile_churn(["git"], repo)- assert "package-lock.json" not in _changed(repo)---def test_dirty_root_manifest_preserves_root_lock(tmp_path):- repo = _repo(tmp_path)- _dirty(repo / "package.json")- _dirty(repo / "package-lock.json")- _discard_lockfile_churn(["git"], repo)- assert set(_changed(repo)) == {"package-lock.json", "package.json"}+def _repo(tmp_path):+ _git(tmp_path, "init", "-q")+ (tmp_path / "package.json").write_text(json.dumps({"workspaces": ["apps/*", "ui-tui"]}), encoding="utf-8")+ (tmp_path / "package-lock.json").write_text("lock v1\n", encoding="utf-8")+ for rel in ("apps/desktop", "ui-tui", "vendor/foo"):+ (tmp_path / rel).mkdir(parents=True)+ (tmp_path / rel / "package.json").write_text("{}\n", encoding="utf-8")+ _git(tmp_path, "add", ".")+ _git(tmp_path, "commit", "-qm", "initial")+ return tmp_path-def test_sibling_workspace_manifest_preserves_root_lock(tmp_path):- repo = _repo(tmp_path)- _dirty(repo / "ui-tui/package.json")- _dirty(repo / "package-lock.json")+def _still_dirty_after_cleanup(repo, *dirty):+ for rel in dirty:+ (repo / rel).write_text("dirty\n", encoding="utf-8")_discard_lockfile_churn(["git"], repo)- assert "package-lock.json" in _changed(repo)+ return set(_git(repo, "diff", "--name-only").splitlines())-def test_unrelated_nested_lock_is_discarded(tmp_path):+def test_dirty_workspace_manifest_preserves_root_lock(tmp_path):repo = _repo(tmp_path)- nested = repo / "ui-tui/package-lock.json"- _dirty(nested)- _discard_lockfile_churn(["git"], repo)- assert "ui-tui/package-lock.json" not in _changed(repo)+ assert _still_dirty_after_cleanup(repo, "apps/desktop/package.json", "package-lock.json") == {+ "apps/desktop/package.json", "package-lock.json",+ }-def test_non_workspace_manifest_does_not_protect_root_lock(tmp_path):+def test_manifest_outside_workspace_graph_does_not_protect_root_lock(tmp_path):repo = _repo(tmp_path)- vendor = repo / "vendor/foo"- vendor.mkdir(parents=True)- _dirty(vendor / "package.json")- _dirty(repo / "package-lock.json")- _discard_lockfile_churn(["git"], repo)- assert "package-lock.json" not in _changed(repo)+ assert _still_dirty_after_cleanup(repo, "vendor/foo/package.json", "package-lock.json") == {+ "vendor/foo/package.json",+ }