The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): keep release holds live and bound schema checks
ethernet391df5c7897c
diff --git a/hermes_cli/source_check.py b/hermes_cli/source_check.pyindex e7b8c4a2a60..3128f8f2065 100644--- a/hermes_cli/source_check.py+++ b/hermes_cli/source_check.py@@ -24,6 +24,7 @@ logger = logging.getLogger(__name__)UPDATE_AVAILABLE_NO_COUNT = -1_UPDATE_CHECK_CACHE_SECONDS = 24 * 3600_UPDATE_CHECK_FAILURE_CACHE_SECONDS = 3600+_RUNTIME_LOOKUP_SECONDS = 8.0def _quiet(fn, default=None):@@ -288,49 +289,83 @@ def _write_cache(cache_file: Path, identity: dict, now: float, result: dict) ->logger.debug("Could not cache source check: %s", exc)-def _github_predates_runtime(repository: str, sha: str, cache_file: Path) -> Optional[bool]:- """Whether release ``sha`` predates the gateway runtime, read from GitHub: the checkout has not- fetched it, and this check never fetches. None when GitHub cannot tell us.-- One API call lists the release's root tree; the state modules come from raw.githubusercontent.com,- which does not spend the 60/hour anonymous API quota. A commit's tree never changes, so a definite- answer is cached per sha for good."""+def _read_runtime_verdict(repository: str, sha: str) -> Optional[bool]:+ """Read the tree and at most four schema blobs at once, within one lookup deadline."""+ from contextvars import copy_contextfrom fnmatch import fnmatchcase+ from itertools import islice+ from queue import Empty, Queue+ from threading import Threadfrom hermes_cli.update_downgrade_guard import STATE_MODULES, defines_runtime+ deadline = time.monotonic() + _RUNTIME_LOOKUP_SECONDS+ replies: Queue[Optional[str]] = Queue()++ def launch(url: str, *, api: bool = False) -> None:+ def read() -> None:+ replies.put(_quiet(lambda: _request(url) if api else _request_with(url, "*/*", None)))++ # A socket timeout alone does not bound a response that keeps trickling bytes. Daemon+ # workers let --version return at the deadline without waiting for stalled downloads.+ Thread(target=copy_context().run, args=(read,), daemon=True).start()++ def receive() -> Optional[str]:+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ raise Empty+ return replies.get(timeout=remaining)++ launch(f"https://api.github.com/repos/{repository}/git/trees/{sha}", api=True)+ try:+ tree = _quiet(lambda: json.loads(receive() or ""))+ entries = tree.get("tree") if isinstance(tree, dict) and not tree.get("truncated") else None+ if not isinstance(entries, list):+ return None+ paths = [e["path"] for e in entries if isinstance(e, dict) and e.get("type") == "blob"+ and isinstance(e.get("path"), str) and fnmatchcase(e["path"], STATE_MODULES)]+ if not paths:+ return None # not a recognised Hermes tree: no claim either way+ urls = iter(f"https://raw.githubusercontent.com/{repository}/{sha}/{quote(path)}" for path in paths)+ pending = 0+ for url in islice(urls, 4):+ launch(url)+ pending += 1+ while pending:+ text = receive()+ if text is None:+ return None # an unread module may hold the marker+ if defines_runtime([text]):+ return False+ pending -= 1+ url = next(urls, None)+ if url is not None:+ launch(url)+ pending += 1+ return True+ except Empty:+ return None+++def _github_predates_runtime(repository: str, sha: str, cache_file: Path) -> Optional[bool]:+ """Read an unfetched release without Git writes. Definite verdicts are immutable per SHA;+ unknown verdicts expire so passive cache hits do not repeatedly contact an unreachable GitHub.+ Schema blobs use raw.githubusercontent.com rather than spending the anonymous API quota."""known = _read_json(cache_file)known = known if isinstance(known, dict) else {}- if isinstance(known.get(sha), bool):- return known[sha]- tree = _quiet(lambda: json.loads(_request(f"https://api.github.com/repos/{repository}/git/trees/{sha}")))- entries = tree.get("tree") if isinstance(tree, dict) and not tree.get("truncated") else None- if not isinstance(entries, list):+ cached = known.get(sha)+ if isinstance(cached, bool):+ return cached+ now = time.time()+ retry_at = cached.get("retryAt") if isinstance(cached, dict) else None+ if isinstance(retry_at, (int, float)) and now < retry_at:return None- paths = [e["path"] for e in entries- if isinstance(e, dict) and e.get("type") == "blob" and fnmatchcase(str(e.get("path")), STATE_MODULES)]- if not paths:- return None # not a hermes-agent tree we recognise: no claim either way- unread = []-- def sources():- for path in paths:- text = _quiet(lambda: _request_with(- f"https://raw.githubusercontent.com/{repository}/{sha}/{quote(path)}", "*/*", None))- if text is None:- unread.append(path)- return- yield text- if defines_runtime(sources()):- verdict = False- elif unread:- return None # a module we could not read may hold the marker- else:- verdict = True+ verdict = _read_runtime_verdict(repository, sha)try:from utils import atomic_json_writecache_file.parent.mkdir(parents=True, exist_ok=True)- atomic_json_write(cache_file, {**known, sha: verdict})+ atomic_json_write(cache_file, {**known, sha: verdict if verdict is not None else+ {"retryAt": time.time() + _UPDATE_CHECK_FAILURE_CACHE_SECONDS}})except OSError as exc:logger.debug("Could not cache the release schema verdict: %s", exc)return verdict@@ -355,14 +390,14 @@ def _update_refuses(co: _Checkout, target: str, home: Path) -> bool:def _hold_refused_release(result: dict, co: _Checkout, home: Path) -> None:"""Hold an offered release `hermes update` refuses (it predates the gateway runtime these sessionstores use). Covers releases off this checkout's history too, which the ancestry-only rollback- test cannot see. Runs on cached offers as well, because the stores gain runtime history between- checks."""+ test cannot see. Apply only after caching the release facts: stores can gain runtime history,+ or the only ledgered profile can be removed, between checks."""target = result.get("targetSha")if result.get("updateAvailable") and target and _update_refuses(co, target, home):result.update(updateAvailable=False, behind=0, held="downgrade")-def _resolve_channel(result: dict, channel: str, co: _Checkout, home: Path, *, forward_only: bool = False):+def _resolve_channel(result: dict, channel: str, co: _Checkout, *, forward_only: bool = False):"""Resolve a release channel's target into ``result``; the SourceTarget, or None on error.A target with a pinned commit is final; one without names a branch to follow instead.@@ -387,7 +422,6 @@ def _resolve_channel(result: dict, channel: str, co: _Checkout, home: Path, *, f# not a new update. Local ancestry only; an unknown relation stays an update.result["rollback"] = bool(co.head and co.head != target and not co.embedded and _git_ok(["merge-base", "--is-ancestor", target, co.head], cwd=co.root, git=co.git))- _hold_refused_release(result, co, home)if source_target.retired:result["retirement"] = {"destination": source_target.channel, "sourceOnly": True}return source_target@@ -512,7 +546,7 @@ def check_for_updates(*, install_root: Path | None = None, home: Path | None = Nelse:result["branch"] = selected_branchidentity = {"root": str(root), "home": str(home), "head": co.head, "origin": co.origin, "branch": selected_branch,- "channel": channel, "embedded": embedded, "branchOverride": branch is not None, "channelProtocol": 2,+ "channel": channel, "embedded": embedded, "branchOverride": branch is not None, "channelProtocol": 3,"forwardOnly": forward_only}cache_file = Path(cache_path) if cache_path is not None else home / "source-checks" / f"{install_id(root)}.json"now = time.time()@@ -527,7 +561,7 @@ def check_for_updates(*, install_root: Path | None = None, home: Path | None = Nif not _is_full_sha(co.head):result.update(error="head-unavailable", message="Could not read the installed revision.")elif branch is None:- source_target = _resolve_channel(result, channel, co, home, forward_only=forward_only)+ source_target = _resolve_channel(result, channel, co, forward_only=forward_only)if source_target is not None and not source_target.commit:# The record supplies a default, not permission to leave the user's branch.selected_branch = configured_branch or _checked_out_branch(co.current_branch, source_target.branch)@@ -537,6 +571,8 @@ def check_for_updates(*, install_root: Path | None = None, home: Path | None = N_check_branch(result, co, selected_branch,heal=(branch_config_path, desktop_config) if heal else None)_write_cache(cache_file, identity, now, result)+ if result.get("channel"):+ _hold_refused_release(result, co, home)return resultdiff --git a/hermes_cli/update_downgrade_guard.py b/hermes_cli/update_downgrade_guard.pyindex 5fc81824f34..06b29ba8517 100644--- a/hermes_cli/update_downgrade_guard.py+++ b/hermes_cli/update_downgrade_guard.py@@ -51,16 +51,18 @@ def target_predates_runtime(git_cmd, root: Path, target_ref: str, *,tree. Any git failure proves nothing (False). ``git`` runs one git command and returns its exitcode; the update check passes its own read-only probe.- ``remote`` answers the same question for a target this checkout has not fetched (True, False, or- None when it cannot tell). The update check passes one because it never fetches, and a release- off the checkout's tracked branch is usually absent until ``hermes update`` fetches it."""+ ``remote`` answers the same question for a target whose commit, tree or schema blobs are missing+ locally (True, False, or None when it cannot tell). The update check passes one because it never+ fetches; the updater stays git-only."""git = git or _updater_git(git_cmd, root)if git("merge-base", "--is-ancestor", "HEAD", target_ref) == 0:return False # forward update: whatever the target's schema, it was written knowing this one- if git("cat-file", "-e", f"{target_ref}^{{commit}}") != 0:+ # grep can return 1 for unreadable blobs too: establish completeness before trusting no match.+ if git("rev-list", "--objects", "--no-walk", "--missing=error", target_ref, "--", STATE_MODULES) != 0:return remote is not None and remote(target_ref) is True- # Exit 1 = no match (the target lacks the ledger); 0 = found; anything else is a git error.- return git("grep", "-q", "-F", _RUNTIME_SCHEMA_MARKER, target_ref, "--", STATE_MODULES) == 1+ # Exit 1 = no match in a complete local schema; 0 = found; anything else is a git error.+ result = git("grep", "-q", "-F", _RUNTIME_SCHEMA_MARKER, target_ref, "--", STATE_MODULES)+ return result == 1 if result in (0, 1) else remote is not None and remote(target_ref) is Truedef _has_ledger_rows(db_path: Path) -> bool:diff --git a/hermes_cli/web_routers/actions.py b/hermes_cli/web_routers/actions.pyindex 85c784d6c47..df183e424bc 100644--- a/hermes_cli/web_routers/actions.py+++ b/hermes_cli/web_routers/actions.py@@ -281,6 +281,11 @@ _NON_APPLYABLE_MESSAGES = {}+def _source_release_label(status: dict[str, Any]) -> str:+ version = status.get("sourceVersion")+ return f"{status.get('channel') or 'stable'} release" + (f" v{version}" if version else "")++@router.get("/api/hermes/update/check")async def check_hermes_update(force: bool = False, profile: Optional[str] = None):"""Report whether a Hermes update is available, without applying it.@@ -324,6 +329,7 @@ async def check_hermes_update(force: bool = False, profile: Optional[str] = None# source_check.check_for_updates() handles git / nix-revision paths through the GitHub API and# caches the result for 24h. ``force`` busts the cache so "Check now" reflects reality.+ status: dict[str, Any] = {}try:from hermes_cli.source_check import check_for_updates@@ -340,7 +346,7 @@ async def check_hermes_update(force: bool = False, profile: Optional[str] = Noneelif behind == 0 and status.get("held"):version = status.get("sourceVersion")payload.update(held=status["held"], source_version=version,- message=f"This build is newer than stable release v{version}, which predates the gateway "+ message=f"This build is newer than {_source_release_label(status)}, which predates the gateway ""runtime its sessions use. It stays on this build until a newer release ships.")elif behind == 0:payload["message"] = "You're on the latest version."@@ -349,7 +355,7 @@ async def check_hermes_update(force: bool = False, profile: Optional[str] = Noneif status.get("rollback"):# A stable release this build already contains: applying it moves back.payload.update(rollback_to=status.get("sourceVersion"),- message=f"This build is newer than stable release v{status.get('sourceVersion')}. "+ message=f"This build is newer than {_source_release_label(status)}. ""Updating moves it back to that release.")# "What's changed" for the desktop's remote update overlay; best-effort# (empty list on any failure). Channel + release name pick its notes source.diff --git a/tests/hermes_cli/test_dashboard_admin_endpoints.py b/tests/hermes_cli/test_dashboard_admin_endpoints.pyindex f239b985706..5a55ff2001b 100644--- a/tests/hermes_cli/test_dashboard_admin_endpoints.py+++ b/tests/hermes_cli/test_dashboard_admin_endpoints.py@@ -827,6 +827,28 @@ class TestUpdateCheckEndpoint:assert body["can_apply"] is True+ @pytest.mark.parametrize("version", [None, "", "1.2.3"])+ @pytest.mark.parametrize("held", [True, False], ids=["held", "rollback"])+ def test_release_message_names_only_a_known_version(self, monkeypatch, version, held):+ monkeypatch.setattr(_cfg_mod, "detect_install_method", lambda *a, **k: "git")+ monkeypatch.setattr("hermes_cli.source_check.check_for_updates", lambda **kw: {+ "behind": 0 if held else -1, "held": "downgrade" if held else None,+ "rollback": not held, "sourceVersion": version, "channel": "preview", "commits": [],+ })++ response = self.client.get("/api/hermes/update/check")+ assert response.status_code == 200+ body = response.json()+ release = "preview release" + (f" v{version}" if version else "")+ assert body["message"].startswith(f"This build is newer than {release}," if held else+ f"This build is newer than {release}.")+ assert body["update_available"] is not held+ assert body["source_version"] == version+ if held:+ assert body["held"] == "downgrade"+ else:+ assert "Updating moves it back" in body["message"]+def test_managed_runtime_dashboard_is_not_applyable(self, monkeypatch):monkeypatch.setattr(_web_server_files, "_dashboard_local_update_managed_externally", lambda: True)diff --git a/tests/hermes_cli/test_source_check.py b/tests/hermes_cli/test_source_check.pyindex b3676145bd9..e507745f7a5 100644--- a/tests/hermes_cli/test_source_check.py+++ b/tests/hermes_cli/test_source_check.py@@ -1,5 +1,6 @@"""Exercise the passive checker with real linked worktrees and loopback HTTP."""import json+import shutilimport subprocessimport threadingimport urllib.request@@ -598,8 +599,9 @@ def test_stable_release_the_updater_refuses_is_held_not_offered(installation, tmfrom hermes_cli.source_check import check_for_updatesfrom tests.hermes_cli.test_update_downgrade_guard import _ledgered_store- root, _linked, home, base, _head, responses, _requests, git = installation- monkeypatch.setattr("hermes_cli.backup._sibling_profile_homes", lambda h: [])+ root, _linked, home, base, _head, responses, requests, git = installation+ sibling = tmp_path / "sibling"+ monkeypatch.setattr("hermes_cli.backup._sibling_profile_homes", lambda h: [("sibling", sibling)])(root / "hermes_state_common.py").write_text("SCHEMA_SQL = 'CREATE TABLE IF NOT EXISTS session_admissions (seq)'\n")git("add", "hermes_state_common.py")git("commit", "-q", "-m", "gateway runtime")@@ -614,7 +616,7 @@ def test_stable_release_the_updater_refuses_is_held_not_offered(installation, tmgit("remote", "set-url", "origin", str(bare))git("checkout", "-q", "main")if ledgered:- _ledgered_store(home)+ _ledgered_store(sibling)api = "/repos/NousResearch/hermes-agent"responses[f"{api}/releases/latest"] = (200, {"draft": False, "prerelease": False, "tag_name": "v1.2.3"})responses[f"{api}/commits/v1.2.3"] = (200, {"sha": release})@@ -628,10 +630,18 @@ def test_stable_release_the_updater_refuses_is_held_not_offered(installation, tmif not ledgered:# The first runtime turn lands after an offer was cached: the next passive check holds it.- _ledgered_store(home)+ _ledgered_store(sibling)cached = check_for_updates(install_root=root, home=home, channel="stable")assert cached["updateAvailable"] is False and cached.get("held") == "downgrade", cached+ # Removing the only ledgered profile clears a cached hold without re-resolving the release.+ shutil.rmtree(sibling)+ seen = list(requests)+ allowed = check_for_updates(install_root=root, home=home, channel="stable")+ assert allowed["updateAvailable"] is True and allowed["behind"] == -1, allowed+ assert "held" not in allowed and allowed["fetchedAt"] == status["fetchedAt"]+ assert requests == seen+@pytest.mark.parametrize("ledgered", [True, False])def test_an_unfetched_release_is_judged_from_github(installation, tmp_path, monkeypatch, ledgered):@@ -679,3 +689,89 @@ def test_an_unfetched_release_is_judged_from_github(installation, tmp_path, monkif ledgered:check_for_updates(install_root=root, home=home, channel="stable", force=True)assert len([p for p in requests if "/git/trees/" in p]) == 1++[email protected]("blocked", ["tree", "modules"])+def test_runtime_lookup_has_one_deadline_with_bounded_parallel_reads(installation, monkeypatch, blocked):+ from hermes_cli import source_check++ _root, _linked, home, _base, sha, responses, _requests, _git = installation+ tree = f"/repos/fixture/fork/git/trees/{sha}"+ paths = [f"hermes_state_{i}.py" for i in range(12)]+ responses[tree] = (200, {"tree": [{"path": path, "type": "blob"} for path in paths]})+ release = threading.Event()+ overlapped = threading.Event()+ finished = threading.Event()+ lock = threading.Lock()+ active = 0+ peak = 0+ verdicts = []++ def stalled(_handler):+ nonlocal active, peak+ with lock:+ active += 1+ peak = max(peak, active)+ if active == 4:+ overlapped.set()+ try:+ release.wait(20)+ return 200, "SCHEMA_SQL = 'old schema'"+ finally:+ with lock:+ active -= 1++ if blocked == "tree":+ responses[tree] = stalled+ for path in paths:+ responses[f"/fixture/fork/{sha}/{path}"] = stalled+ monkeypatch.setattr(source_check, "_RUNTIME_LOOKUP_SECONDS", 3.0)++ def check():+ try:+ verdicts.append(source_check._github_predates_runtime("fixture/fork", sha, home / "runtime.json"))+ finally:+ finished.set()++ checker = threading.Thread(target=check, daemon=True)+ checker.start()+ try:+ if blocked == "modules":+ assert overlapped.wait(5), "independent schema reads must overlap"+ assert finished.wait(5), "a slow successful response must not extend the whole lookup deadline"+ assert verdicts == [None]+ assert peak <= 4+ finally:+ release.set()+ checker.join(5)++[email protected]("failure", ["tree", "module"])[email protected]("runtime", [True, False])+def test_unknown_runtime_verdict_expires_but_definite_verdict_does_not(installation, monkeypatch, failure, runtime):+ from hermes_cli import source_check++ _root, _linked, home, _base, sha, responses, requests, _git = installation+ clock = [1000000.0]+ monkeypatch.setattr(source_check.time, "time", lambda: clock[0])+ tree = f"/repos/fixture/fork/git/trees/{sha}"+ module = f"/fixture/fork/{sha}/hermes_state_common.py"+ responses[tree] = (200, {"tree": [{"path": "hermes_state_common.py", "type": "blob"}]})+ from hermes_state_common import SCHEMA_SQL+ responses[module] = (200, SCHEMA_SQL if runtime else "SCHEMA_SQL = 'old schema'")+ failed_path = tree if failure == "tree" else module+ good = responses[failed_path]+ responses[failed_path] = (503, {})+ check = lambda: source_check._github_predates_runtime("fixture/fork", sha, home / "runtime.json")++ assert check() is None+ seen = list(requests)+ responses[failed_path] = good+ assert check() is None+ assert requests == seen, "cache hits must not retry an unreachable GitHub"+ clock[0] += source_check._UPDATE_CHECK_FAILURE_CACHE_SECONDS + 1+ assert check() is not runtime+ seen = list(requests)+ clock[0] += 10 * source_check._UPDATE_CHECK_CACHE_SECONDS+ assert check() is not runtime+ assert requests == seendiff --git a/tests/hermes_cli/test_update_downgrade_guard.py b/tests/hermes_cli/test_update_downgrade_guard.pyindex f7fd23111b3..842ea849944 100644--- a/tests/hermes_cli/test_update_downgrade_guard.py+++ b/tests/hermes_cli/test_update_downgrade_guard.py@@ -4,10 +4,11 @@ Downgrading is unsupported (#106742, jquesnelle item 6): the older release cannothe sessions with runtime history, so the move is refused before the tree changes and the messagenames the pre-update snapshot that predates the cutover."""+import jsonimport sqlite3import subprocessfrom contextlib import closing-from pathlib import Path+from unittest.mock import Mockimport pytest@@ -133,12 +134,58 @@ def test_check_holds_a_release_that_update_would_refuse(repo, tmp_path, monkeypadef test_the_runtime_schema_marker_is_still_in_this_trees_state_schema():- """target_predates_runtime greps a release tree for this literal, so it must match the DDL as written.-- If the marker drifts from hermes_state*.py (a renamed file, reformatted DDL), every later- forward release would read as pre-runtime: refused on --branch, and silently held on a channel.- """- root = Path(__file__).resolve().parents[2]- schema = [p.read_text(encoding="utf-8") for p in root.glob("hermes_state*.py")]- assert schema, "no hermes_state*.py next to tests/"- assert any(guard._RUNTIME_SCHEMA_MARKER in text for text in schema)+ from hermes_state_common import SCHEMA_SQL++ assert guard.defines_runtime([SCHEMA_SQL])++[email protected]("remote_result", [True, False, None, "no-reader"])+def test_present_commit_with_missing_schema_blobs_uses_remote_without_git_writes(+ repo, tmp_path, monkeypatch, remote_result):+ from hermes_cli.source_check import source_git_env++ _git(repo, "config", "uploadpack.allowFilter", "true")+ clone = tmp_path / "partial"+ _git(tmp_path, "clone", "--filter=blob:none", "--no-checkout", repo.as_uri(), str(clone))+ target = _git(clone, "rev-parse", "old-release")+ trace = tmp_path / "git-trace.jsonl"+ monkeypatch.setenv("GIT_TRACE2_EVENT", str(trace))++ def probe(*args):+ return subprocess.run(["git", *args], cwd=clone, env=source_git_env(),+ capture_output=True, text=True, stdin=subprocess.DEVNULL,+ timeout=10, check=False)++ def git(*args):+ return probe(*args).returncode++ before = {p.relative_to(clone): p.read_bytes() for p in (clone / ".git").rglob("*") if p.is_file()}+ assert git("cat-file", "-e", f"{target}^{{commit}}") == 0+ assert git("cat-file", "-e", f"{target}:hermes_state_common.py") != 0+ grep = probe("grep", "-q", "-F", guard._RUNTIME_SCHEMA_MARKER, target, "--", guard.STATE_MODULES)+ assert grep.returncode != 0 and grep.stderr+ remote = None if remote_result == "no-reader" else Mock(return_value=remote_result)+ assert guard.target_predates_runtime(None, clone, target, git=git, remote=remote) is (remote_result is True)+ if remote is not None:+ remote.assert_called_once_with(target)+ assert {p.relative_to(clone): p.read_bytes() for p in (clone / ".git").rglob("*") if p.is_file()} == before+ events = [json.loads(line) for line in trace.read_text().splitlines()]+ assert not any(event["event"] == "child_start" for event in events)++[email protected]("grep_error", [False, True])+def test_local_schema_decides_without_remote_unless_the_probe_fails(repo, grep_error):+ runtime = _git(repo, "rev-parse", "HEAD")+ (repo / "notes.txt").write_text("later build\n")+ _git(repo, "add", "notes.txt")+ _git(repo, "commit", "-qm", "later build")+ remote = Mock(side_effect=lambda ref: ref == "old-release")+ run = guard._updater_git(["git"], repo)++ def git(*args):+ return -1 if grep_error and args[0] == "grep" else run(*args)++ assert guard.target_predates_runtime(None, repo, "old-release", git=git, remote=remote)+ assert not guard.target_predates_runtime(None, repo, runtime, git=git, remote=remote)+ assert not guard.target_predates_runtime(None, repo, "HEAD", git=git, remote=remote)+ assert remote.call_count == (2 if grep_error else 0)