fix(memory): key the stuck-call registries by the home providers were bound to

The registry key used the home captured when the manager was built.
initialize_all() can bind the providers to another profile (explicit
hermes_home=, recorded in _bound_scope since #140058), so two managers built in
one ambient home but initialized for profiles A and B shared a key, and A's
stuck calls gated B. Both gates now key on the bound home, falling back to the
build-time home before initialize_all() runs.
This commit is contained in:
kshitijk4poor
2026-10-18 15:57:40 +05:30
committed by kshitij
parent b6c8ed0ba5
commit 9f0c6824bc
3 changed files with 28 additions and 3 deletions

View File

@@ -332,7 +332,7 @@ class MemoryManager:
result_box: dict[str, Any] = {}
key = (self._home_key, provider.name)
key = self._registry_key(provider)
def _run() -> None:
try:
@@ -493,7 +493,7 @@ class MemoryManager:
if provider.name == "builtin":
call()
return
key = (self._home_key, provider.name)
key = self._registry_key(provider)
token = object()
now = time.monotonic()
with self._BACKGROUND_CALLS_LOCK:
@@ -515,6 +515,11 @@ class MemoryManager:
if _forget_inflight(self._BACKGROUND_CALLS, key, token):
self._BACKGROUND_SKIPS.pop(key, None)
def _registry_key(self, provider: MemoryProvider) -> tuple[str, str]:
"""Process-wide registry key: the home the providers were bound to (``initialize_all``, which may
name another profile than the one this manager was built in), else the build-time home."""
return ((self._bound_scope or {}).get("home_key") or self._home_key, provider.name)
def _forget_background_future(self, future: Future) -> None:
with self._sync_executor_lock:
self._background_futures.pop(future, None)

View File

@@ -560,6 +560,26 @@ class TestMemoryManager:
finally:
orphan.release.set()
def test_registry_is_keyed_by_the_home_providers_were_bound_to(self, tmp_path, monkeypatch):
"""Two managers built in one ambient home but initialized for profiles A and B: A's stuck
prefetch must not gate B, whose providers belong to another profile."""
import agent.memory_manager as mm
monkeypatch.setattr(mm, "_MAX_STUCK_CALLS", 1)
stuck, other = BlockingPrefetchProvider("bound-home"), FakeMemoryProvider("bound-home")
other._prefetch_result = "profile b memory"
mgr_a = MemoryManager(external_prefetch_timeout=0.01)
mgr_b = MemoryManager(external_prefetch_timeout=5.0)
for mgr, provider, home in ((mgr_a, stuck, "a"), (mgr_b, other, "b")):
mgr.add_provider(provider)
mgr.initialize_all(session_id=home, hermes_home=str(tmp_path / home))
try:
mgr_a._prefetch_provider(stuck, "q", session_id="a")
assert stuck.started.wait(timeout=2.0)
assert mgr_b._prefetch_provider(other, "q", session_id="b") == "profile b memory"
finally:
stuck.release.set()
def test_healthy_concurrent_prefetches_from_two_sessions_both_recall(self):
"""Only a TIMED-OUT call gates other managers: a slow-but-healthy prefetch in one session
must not blank the recall of another session that prefetches meanwhile."""

View File

@@ -93,7 +93,7 @@ def test_sustained_wedge_pins_at_most_max_stuck_threads():
assert fresh.flush_pending(timeout=5) # returns: nothing blocked behind the hung calls
fresh.shutdown_all()
key = (hung[0][0]._home_key, hung[0][1].name)
key = hung[0][0]._registry_key(hung[0][1])
assert MemoryManager._BACKGROUND_SKIPS[key] == 10
assert len(MemoryManager._BACKGROUND_CALLS[key]) == mm._MAX_STUCK_CALLS
# Idle workers of shut-down executors exit asynchronously.