From c2325f1a179030f3e4378fcdec127a60a38233a5 Mon Sep 17 00:00:00 2001 From: Molecule AI Infra-Runtime-BE Date: Wed, 13 May 2026 11:09:54 +0000 Subject: [PATCH] fix(a2a): restore TTL cache check in enrich_peer_metadata_nonblocking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stdio-fallback branch removed the cache-first check from enrich_peer_metadata_nonblocking, causing 5 tests to fail: test_envelope_enrichment_uses_cache_when_present test_envelope_enrichment_fetches_on_cache_miss test_envelope_enrichment_re_fetches_after_ttl test_enrich_peer_metadata_nonblocking_cache_hit_returns_immediately test_enrich_peer_metadata_nonblocking_cache_miss_schedules_fetch The removed lines checked the peer metadata cache (TTL-bounded) and returned immediately on a cache hit. Without this, every push for a known peer schedules a background fetch — a performance regression and a deviation from the documented contract (PR #2484). This patch restores the cache check to the exact original logic. Co-Authored-By: Claude Opus 4.7 --- workspace/a2a_client.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/workspace/a2a_client.py b/workspace/a2a_client.py index 7cc79b5f..2de63044 100644 --- a/workspace/a2a_client.py +++ b/workspace/a2a_client.py @@ -187,11 +187,19 @@ def enrich_peer_metadata_nonblocking( canon = _validate_peer_id(peer_id) if canon is None: return None - # Schedule background fetch unless one is already in flight for this - # peer. The synchronous version atomically reads-then-writes; the - # async version splits that into "schedule fetch" + "fetch fills - # cache later." The in-flight set keeps a flurry of pushes from - # one peer (e.g., a chatty agent) from spawning N parallel GETs. + # Cache hit (fresh): return without blocking on a registry GET. + # This is the hot path for active peer conversations — avoids + # spawning a background thread for every push from a known peer. + current = time.monotonic() + cached = _peer_metadata_get(canon) + if cached is not None: + fetched_at, record = cached + if current - fetched_at < _PEER_METADATA_TTL_SECONDS: + return record + # Cache miss or TTL expired: schedule background fetch unless one is + # already in flight for this peer. The in-flight set keeps a flurry + # of pushes from one peer (e.g., a chatty agent) from spawning N + # parallel GETs. with _enrich_in_flight_lock: if canon in _enrich_in_flight: return None