Skip to content

The agent that grows with you

fix(skills): reuse HTTP connections during inspect

KoNit-K7d793348e8dc
diff --git a/hermes_cli/skills_hub.py b/hermes_cli/skills_hub.py
index 9731b57ce17..23af390b929 100644
--- a/hermes_cli/skills_hub.py
+++ b/hermes_cli/skills_hub.py
@@ -245,10 +245,12 @@ def _full_identifier(identifier: str, sources, c) -> str:
def _resolve_identifier(identifier: str, sources, c) -> tuple:
"""Short-name resolution + (meta, bundle, source). identifier == "" means unresolved."""
- identifier = _full_identifier(identifier, sources, c)
- if not identifier:
- return "", None, None, None
- return (identifier, *_resolve_source_meta_and_bundle(identifier, sources))
+ from tools.skills_hub import skills_hub_http_session
+ with skills_hub_http_session():
+ identifier = _full_identifier(identifier, sources, c)
+ if not identifier:
+ return "", None, None, None
+ return (identifier, *_resolve_source_meta_and_bundle(identifier, sources))
def _is_valid_installed_skill_name(name: str) -> bool:
diff --git a/tests/hermes_cli/test_skills_hub.py b/tests/hermes_cli/test_skills_hub.py
index 088130671e2..4d8fbc426f5 100644
--- a/tests/hermes_cli/test_skills_hub.py
+++ b/tests/hermes_cli/test_skills_hub.py
@@ -285,6 +285,55 @@ def test_resolve_keeps_catalog_meta_when_later_sources_do_not_fetch():
assert matched.__class__ is CatalogSource
+def test_inspect_reuses_one_ssrf_safe_client_for_metadata_and_bundle(monkeypatch):
+ """A preview's sequential resolver calls must share its guarded connection pool."""
+ import hermes_cli.skills_hub as cli_hub
+ import tools.skills_hub as hub
+ from tools.skills_hub_models import SkillBundle, SkillMeta
+
+ clients = []
+
+ class Response:
+ status_code = 200
+ text = "ok"
+ content = b"ok"
+ headers = {}
+
+ class Client:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ return False
+
+ def get(self, *_args, **_kwargs):
+ return Response()
+
+ class Source:
+ def inspect(self, _identifier):
+ hub._guarded_http_get("https://example.com/metadata")
+ return SkillMeta("example", "metadata", "test", "example/id", "community")
+
+ def fetch(self, _identifier):
+ hub._guarded_http_get("https://example.com/SKILL.md")
+ return SkillBundle("example", {"SKILL.md": "# Example"}, "test", "example/id", "community")
+
+ def create_client(**_kwargs):
+ client = Client()
+ clients.append(client)
+ return client
+
+ monkeypatch.setattr(cli_hub, "_sources", lambda: [Source()])
+ monkeypatch.setattr(hub, "create_ssrf_safe_client", create_client, raising=False)
+ monkeypatch.setattr(hub, "is_safe_url", lambda _url: True)
+ monkeypatch.setattr(hub, "check_website_access", lambda _url: None)
+
+ result = cli_hub.inspect_skill("example/id")
+
+ assert result is not None
+ assert len(clients) == 1
+
+
# ---------------------------------------------------------------------------
diff --git a/tools/skills_hub.py b/tools/skills_hub.py
index 10e72a876e6..8066e058b06 100644
--- a/tools/skills_hub.py
+++ b/tools/skills_hub.py
@@ -14,6 +14,7 @@ Used by hermes_cli/skills_hub.py for CLI commands and the /skills slash command.
import json
import logging
import time
+from contextvars import ContextVar
from contextlib import ExitStack, contextmanager
from datetime import datetime, timezone
from pathlib import Path
@@ -24,6 +25,7 @@ import httpx
from hermes_constants import get_hermes_home
from tools.url_safety import is_safe_url
+from tools.url_safety import create_ssrf_safe_client
from tools.website_policy import check_website_access
from tools.skills_hub_models import _normalize_lock_install_path, _validate_skill_name
@@ -83,12 +85,42 @@ def __getattr__(name: str):
_REDIRECT_STATUS_CODES = {301, 302, 303, 307, 308}
_MAX_SKILL_FETCH_REDIRECTS = 5
+# An inspect resolves metadata and then the preview bundle through the same
+# source adapter. Keeping this context local to that operation lets httpx reuse
+# its verified connection without extending a client beyond the CLI request.
+_skills_hub_http_client: ContextVar[Optional[Any]] = ContextVar(
+ "skills_hub_http_client", default=None
+)
+
+
+@contextmanager
+def skills_hub_http_session() -> Iterator[None]:
+ """Reuse one SSRF-safe HTTP client for a single skills-hub resolution."""
+ if _skills_hub_http_client.get() is not None:
+ yield
+ return
+ with create_ssrf_safe_client(follow_redirects=False) as client:
+ token = _skills_hub_http_client.set(client)
+ try:
+ yield
+ finally:
+ _skills_hub_http_client.reset(token)
+
+
+def _skills_hub_http_get(url: str, **kwargs: Any) -> httpx.Response:
+ """GET through the active resolution pool, or preserve one-shot behavior."""
+ client = _skills_hub_http_client.get()
+ if client is not None:
+ return client.get(url, **kwargs)
+ return httpx.get(url, **kwargs)
+
def _ssrf_safe_http_get(url: str, *, timeout: int = 20,
headers: Optional[Dict[str, str]] = None) -> httpx.Response:
"""Fetch one URL with connect-time SSRF validation and no automatic redirects."""
- from tools.url_safety import create_ssrf_safe_client
-
+ client = _skills_hub_http_client.get()
+ if client is not None:
+ return client.get(url, timeout=timeout, headers=headers)
with create_ssrf_safe_client(timeout=timeout, follow_redirects=False) as client:
return client.get(url, headers=headers)
diff --git a/tools/skills_hub_github.py b/tools/skills_hub_github.py
index 67e8b780316..c62ca0a0945 100644
--- a/tools/skills_hub_github.py
+++ b/tools/skills_hub_github.py
@@ -15,7 +15,7 @@ from agent.retry_utils import parse_retry_after_seconds
from tools.skills_guard import TRUSTED_REPOS
from tools.skills_hub_models import (
SkillBundle, SkillMeta, SkillSource, _cache_metas, _cached_metas, _dedupe_by_trust,
- _hermes_tags, _matches_query, _parse_frontmatter, _referenced_support_paths,
+ _hermes_tags, _matches_query, _parse_frontmatter, _referenced_support_paths, hub,
_validate_bundle_rel_path,
)
@@ -460,7 +460,9 @@ class GitHubSource(SkillSource):
last_attempt = attempt >= max_retries - 1
wait = backoff
try:
- resp = httpx.get(url, params=params, headers=hdrs, timeout=timeout, follow_redirects=True)
+ resp = hub()._skills_hub_http_get(
+ url, params=params, headers=hdrs, timeout=timeout, follow_redirects=True
+ )
except httpx.HTTPError as e:
logger.debug("GitHub GET %s failed (attempt %d/%d): %s", url, attempt + 1, max_retries, e)
if last_attempt:
diff --git a/tools/skills_hub_models.py b/tools/skills_hub_models.py
index 961e43c9d4e..2563bb5741e 100644
--- a/tools/skills_hub_models.py
+++ b/tools/skills_hub_models.py
@@ -77,7 +77,7 @@ def _memo_json(key: str, compute: Callable[[], Any], valid: Callable[[Any], bool
def _get_json(url: str, *, timeout: int = 20, **kwargs) -> Optional[Any]:
"""Plain (unguarded) GET + JSON decode; None on non-200 or transport/decode error."""
try:
- resp = httpx.get(url, timeout=timeout, **kwargs)
+ resp = hub()._skills_hub_http_get(url, timeout=timeout, **kwargs)
return resp.json() if resp.status_code == 200 else None
except (httpx.HTTPError, json.JSONDecodeError):
return None
@@ -86,7 +86,7 @@ def _get_json(url: str, *, timeout: int = 20, **kwargs) -> Optional[Any]:
def _get_text(url: str, *, timeout: int = 20, **kwargs) -> Optional[str]:
"""Plain (unguarded) GET; body text on 200, None on any other status or transport error."""
try:
- resp = httpx.get(url, timeout=timeout, **kwargs)
+ resp = hub()._skills_hub_http_get(url, timeout=timeout, **kwargs)
except httpx.HTTPError:
return None
return resp.text if resp.status_code == 200 else None
diff --git a/tools/skills_hub_search.py b/tools/skills_hub_search.py
index ce06ba96aec..0c43bb8f436 100644
--- a/tools/skills_hub_search.py
+++ b/tools/skills_hub_search.py
@@ -51,8 +51,11 @@ def _load_hermes_index() -> Optional[dict]:
data = None
for accept_encoding in ("gzip, deflate", "identity"):
try:
- resp = httpx.get(HERMES_INDEX_URL, timeout=15, follow_redirects=True,
- headers={"Accept-Encoding": accept_encoding})
+ from tools.skills_hub_models import hub
+ resp = hub()._skills_hub_http_get(
+ HERMES_INDEX_URL, timeout=15, follow_redirects=True,
+ headers={"Accept-Encoding": accept_encoding},
+ )
if resp.status_code != 200:
logger.debug("Hermes index fetch returned %d", resp.status_code)
return _load_stale_index_cache()