The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(urllib): key the CA-context memo on the preferred bundle only
kshitijk4poor7a0c8287ce87
diff --git a/hermes_cli/urllib_security.py b/hermes_cli/urllib_security.pyindex 026518557c7..85f311885b7 100644--- a/hermes_cli/urllib_security.py+++ b/hermes_cli/urllib_security.py@@ -85,9 +85,9 @@ class _CrossOriginRequestSanitizer(urllib.request.BaseHandler):# Building the context parses the whole CA bundle (~4 ms for certifi) on EVERY Hermes-owned request,-# since no opener is ever installed globally. Memoise on the bundles' ``_bundle_signature`` so a-# rotated/reconfigured bundle is still picked up. The context is shared and never mutated by callers.-# No lock: a race costs one duplicate parse.+# since no opener is ever installed globally. Memoise on the preferred bundle's ``_bundle_signature``+# (the only file a memoised context was built from) so a rotated/reconfigured bundle is still picked+# up. The context is shared and never mutated by callers. No lock: a race costs one duplicate parse._HTTPS_CONTEXT_CACHE: tuple[tuple, ssl.SSLContext] | None = None@@ -137,18 +137,22 @@ def _bundle_signature(path: str) -> tuple:def _resolved_https_context() -> ssl.SSLContext | None:"""Return the shared explicit-CA context for Hermes-owned urllib openers.- Memoised on the resolved bundle's signature; the returned context is shared and must not be+ Memoised on the preferred bundle's signature only: that is the one file a memoised context was+ built from, so a change to a fallback bundle (certifi on macOS) that was never read must not+ invalidate it — and costs no stat per request. The returned context is shared and must not bemutated by callers."""global _HTTPS_CONTEXT_CACHEcandidates = _ca_bundle_candidates()- key = tuple(_bundle_signature(path) for path in candidates)+ if not candidates:+ return None+ key = _bundle_signature(candidates[0])cached = _HTTPS_CONTEXT_CACHEif cached is not None and cached[0] == key:return cached[1]context, used_path = _build_https_context(candidates)- if candidates and used_path == candidates[0]:+ if used_path == candidates[0]:# Only a context built from the preferred bundle is memoised (used_path is None whenever# context is None, so this also excludes a failed load). A failed (possibly transient)# load — whether it left us with no context or with a fallback bundle (certifi on macOS) —diff --git a/tests/hermes_cli/test_urllib_security.py b/tests/hermes_cli/test_urllib_security.pyindex 2c92114bac1..3e901402f27 100644--- a/tests/hermes_cli/test_urllib_security.py+++ b/tests/hermes_cli/test_urllib_security.py@@ -580,6 +580,26 @@ def test_rotated_ca_bundle_is_picked_up(monkeypatch, tmp_path):assert calls == [str(ca_bundle), str(ca_bundle)]+def test_fallback_bundle_change_does_not_invalidate_the_memo(monkeypatch, tmp_path):+ """The memo keys on the preferred bundle only: certifi was never read, so its rotation is moot."""+ import hermes_cli.urllib_security as urllib_security++ _clear_ca_bundle_env(monkeypatch)+ preferred = tmp_path / "corporate-ca.pem"+ fallback = tmp_path / "cacert.pem"+ preferred.write_text("preferred")+ fallback.write_text("first")+ factory, calls = _counting_context_factory()+ monkeypatch.setattr(ssl, "create_default_context", factory)+ monkeypatch.setattr(urllib_security, "_ca_bundle_candidates", lambda: (str(preferred), str(fallback)))++ first = urllib_security._resolved_https_context()+ fallback.write_text("a rotated fallback bundle with a different length")++ assert urllib_security._resolved_https_context() is first+ assert calls == [str(preferred)]++@pytest.mark.parametrize(("candidates", "first_load_fails_for", "expected_load_sequence"),[