Skip to content

The agent that grows with you

fix(webapp): let a retried inspection collect the slow clone it timed out on

unsupportedpastels8c3c61dbb9c5
diff --git a/apps/desktop/src/store/agent-plugins.ts b/apps/desktop/src/store/agent-plugins.ts
index eb5db5cbd7b..c98a8750ff8 100644
--- a/apps/desktop/src/store/agent-plugins.ts
+++ b/apps/desktop/src/store/agent-plugins.ts
@@ -409,7 +409,8 @@ export async function inspectAgentPlugin(
},
opts.profile
),
- // A clone runs on the backend, as an install's does.
+ // A clone runs on the backend, as an install's does. One that outlasts this wait keeps running
+ // there, and Retry collects its answer instead of starting another clone.
PLUGIN_INSTALL_REQUEST_TIMEOUT_MS
)
diff --git a/hermes_cli/plugins_cmd_inspect.py b/hermes_cli/plugins_cmd_inspect.py
index 9212856cb08..6ef29077023 100644
--- a/hermes_cli/plugins_cmd_inspect.py
+++ b/hermes_cli/plugins_cmd_inspect.py
@@ -13,6 +13,9 @@ from __future__ import annotations
import json
import tempfile
+import threading
+import time
+from concurrent.futures import Future
from pathlib import Path
from typing import Any, Optional
@@ -20,6 +23,11 @@ import hermes_yaml as yaml
#: A manifest is a few hundred bytes; never read a hostile multi-megabyte one into memory.
_MANIFEST_MAX_BYTES = 256 * 1024
+#: How long a successful inspection answers a repeat of the same request (see ``inspect_plugin_source``).
+_RESULT_TTL_SECONDS = 600
+#: ``(home, catalog_name, identifier)`` -> ``[future, finished_at]``; ``finished_at`` is None while it runs.
+_inspections: dict[tuple[str, str, str], list] = {}
+_inspections_lock = threading.Lock()
def _pc():
@@ -69,7 +77,49 @@ def _failure(error: str, warnings: list[str], insecure: bool) -> dict[str, Any]:
def inspect_plugin_source(identifier: str, *, catalog_name: Optional[str] = None) -> dict[str, Any]:
"""``{ok, agent, desktop, agent_name, desktop_name, warnings, insecure}``, or ``ok: False`` with
``error``. *catalog_name* inspects the curated entry at its reviewed pin (its repo replaces
- *identifier*), so the dialog shows exactly what the catalog install will put down."""
+ *identifier*), so the dialog shows exactly what the catalog install will put down.
+
+ A repeat of a request (same profile home, catalog name and source) joins the clone still running
+ for it, or reads a successful answer it produced within :data:`_RESULT_TTL_SECONDS`. The browser
+ stops waiting before a slow clone can finish (``plugins.clone_timeout_seconds`` reaches an hour per
+ Git step); its Retry must collect that clone's answer, not start another under the same deadline.
+ A failed read is never kept, so a Retry after one inspects afresh."""
+ from hermes_constants import get_hermes_home
+ key = (str(get_hermes_home()), catalog_name or "", identifier)
+ now = time.monotonic()
+ with _inspections_lock:
+ for stale in [k for k, (_, done) in _inspections.items()
+ if done is not None and now - done > _RESULT_TTL_SECONDS]:
+ del _inspections[stale]
+ slot = _inspections.get(key)
+ owner = slot is None
+ if owner:
+ slot = _inspections[key] = [Future(), None]
+ future: Future = slot[0]
+ if not owner:
+ return _copy(future.result())
+ try:
+ result = _inspect(identifier, catalog_name)
+ except BaseException as exc:
+ with _inspections_lock:
+ _inspections.pop(key, None)
+ future.set_exception(exc)
+ raise
+ with _inspections_lock:
+ if result["ok"]:
+ slot[1] = time.monotonic()
+ else:
+ _inspections.pop(key, None)
+ future.set_result(result)
+ return _copy(result)
+
+
+def _copy(result: dict[str, Any]) -> dict[str, Any]:
+ """Each caller gets its own answer: one shared by joined requests must not be edited through another."""
+ return {**result, "warnings": list(result["warnings"])}
+
+
+def _inspect(identifier: str, catalog_name: Optional[str]) -> dict[str, Any]:
pc = _pc()
revision = None
names: tuple[str, ...] = ()
diff --git a/tests/hermes_cli/test_plugins_cmd_inspect.py b/tests/hermes_cli/test_plugins_cmd_inspect.py
index b3069a523f0..eb7b900b574 100644
--- a/tests/hermes_cli/test_plugins_cmd_inspect.py
+++ b/tests/hermes_cli/test_plugins_cmd_inspect.py
@@ -5,12 +5,13 @@ but the catalog lookup and the kill list.
"""
import subprocess
+import threading
from pathlib import Path
from types import SimpleNamespace
import pytest
-from hermes_cli import plugins_cmd_catalog
+from hermes_cli import plugins_cmd, plugins_cmd_catalog, plugins_cmd_inspect
from hermes_cli.plugins_cmd import PluginOperationError
from hermes_cli.plugins_cmd_inspect import inspect_plugin_source
@@ -46,6 +47,29 @@ def sentinel(tmp_path):
return tmp_path / "imported.flag"
[email protected](autouse=True)
+def _fresh_inspections():
+ plugins_cmd_inspect._inspections.clear()
+ yield
+ plugins_cmd_inspect._inspections.clear()
+
+
+def clones(monkeypatch):
+ """Count real clones; ``gate`` (when set) holds each clone until it is released."""
+ real = plugins_cmd._clone_plugin_repo
+ state = SimpleNamespace(count=0, started=threading.Event(), gate=None)
+
+ def counting(*args, **kwargs):
+ state.count += 1
+ state.started.set()
+ if state.gate is not None:
+ assert state.gate.wait(30)
+ return real(*args, **kwargs)
+ monkeypatch.setattr(plugins_cmd, "_clone_plugin_repo", counting)
+ return state
+
+
def _agent_files(sentinel: Path, name: str = "demo-agent") -> dict[str, str]:
# Importing this package would leave the sentinel behind: inspection must never import it.
return {"plugin.yaml": f"name: {name}\nversion: 1.0.0\n",
@@ -152,3 +176,57 @@ def test_inspection_writes_nothing_into_the_profile(tmp_path, sentinel, monkeypa
assert inspect_plugin_source(repo.as_uri())["ok"] is True
assert not (home / "plugins").exists() or not any((home / "plugins").iterdir())
+
+
+def test_retry_during_a_slow_clone_joins_it_instead_of_cloning_again(tmp_path, sentinel, clones):
+ # The browser gives up on a slow inspection and retries; the retry must get the running clone's answer.
+ repo = _repo(tmp_path, _agent_files(sentinel))
+ clones.gate = threading.Event()
+ answers = []
+ first = threading.Thread(target=lambda: answers.append(inspect_plugin_source(repo.as_uri())))
+ first.start()
+ assert clones.started.wait(30)
+ retry = threading.Thread(target=lambda: answers.append(inspect_plugin_source(repo.as_uri())))
+ retry.start()
+ # Still waiting while the first clone is held: it joined that clone, not a finished answer.
+ retry.join(0.5)
+ assert retry.is_alive() and clones.count == 1
+ clones.gate.set()
+ first.join(30)
+ retry.join(30)
+
+ assert clones.count == 1
+ assert [a["agent_name"] for a in answers] == ["demo-agent", "demo-agent"]
+ assert answers[0] is not answers[1] and answers[0]["warnings"] is not answers[1]["warnings"]
+
+
+def test_a_retry_after_the_clone_finished_reads_its_answer_until_it_expires(tmp_path, sentinel, clones,
+ monkeypatch):
+ repo = _repo(tmp_path, _agent_files(sentinel))
+
+ assert inspect_plugin_source(repo.as_uri())["ok"] is True
+ assert inspect_plugin_source(repo.as_uri())["ok"] is True
+ assert clones.count == 1
+
+ monkeypatch.setattr(plugins_cmd_inspect, "_RESULT_TTL_SECONDS", -1)
+ assert inspect_plugin_source(repo.as_uri())["ok"] is True
+ assert clones.count == 2
+
+
+def test_a_failed_read_is_not_kept(tmp_path, clones):
+ missing = (tmp_path / "nope").as_uri()
+
+ assert inspect_plugin_source(missing)["ok"] is False
+ assert inspect_plugin_source(missing)["ok"] is False
+ assert clones.count == 2
+ assert not plugins_cmd_inspect._inspections
+
+
+def test_another_profile_never_reads_this_profiles_answer(tmp_path, sentinel, clones, monkeypatch):
+ repo = _repo(tmp_path, _agent_files(sentinel))
+ for home in ("a", "b"):
+ (tmp_path / home).mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / home))
+ assert inspect_plugin_source(repo.as_uri())["ok"] is True
+
+ assert clones.count == 2