From 8b73720fa7e3ce7bbf931920bdf32d9149026e36 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 31 Aug 2026 08:21:04 -0700 Subject: [PATCH] fix(memory): tolerate bare-signature v2 providers when forwarding checkpoint requirement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hardening on top of @Soju06's forwarding fix: v2 providers written against the original docs example (def on_pre_compress(self, messages)) must not TypeError when the host forwards require_checkpoint — inspect the signature and fall back to the legacy call shape. Docs example updated to advertise the keyword. --- agent/memory_manager.py | 39 ++++++++++++++++- .../test_pre_compress_checkpoint_contract.py | 43 +++++++++++++++++++ .../developer-guide/memory-provider-plugin.md | 4 +- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/agent/memory_manager.py b/agent/memory_manager.py index c2b083d011..496562c2e8 100644 --- a/agent/memory_manager.py +++ b/agent/memory_manager.py @@ -41,6 +41,36 @@ from tools.registry import tool_error # historical best-effort contract (API v1). _LEGACY_PRE_COMPRESS_API_VERSION = 1 + +def _accepts_require_checkpoint(fn: Callable[..., Any]) -> bool: + """True if ``fn`` can receive the ``require_checkpoint`` keyword. + + Checkpoint (v2) providers written against the original docs example use + the bare ``on_pre_compress(self, messages)`` signature; calling them with + the keyword would raise ``TypeError`` — which, under + ``require_checkpoint=True``, the host would re-raise as a checkpoint + failure even though the provider's durable write succeeded. Inspect the + signature and fall back to the legacy call shape when the keyword (or a + ``**kwargs`` catch-all) is absent. Unreadable signatures (C callables, + exotic proxies) conservatively report False. + """ + try: + sig = inspect.signature(fn) + except (TypeError, ValueError): + return False + for param in sig.parameters.values(): + if param.kind is inspect.Parameter.VAR_KEYWORD: + return True + if ( + param.name == "require_checkpoint" + and param.kind in ( + inspect.Parameter.KEYWORD_ONLY, + inspect.Parameter.POSITIONAL_OR_KEYWORD, + ) + ): + return True + return False + logger = logging.getLogger(__name__) # How long shutdown_all() waits for in-flight background sync/prefetch work @@ -1122,12 +1152,19 @@ class MemoryManager: if is_checkpoint_provider and evidence_messages is not None: provider_messages = evidence_messages try: - if is_checkpoint_provider: + if is_checkpoint_provider and _accepts_require_checkpoint( + provider.on_pre_compress + ): result = provider.on_pre_compress( provider_messages, require_checkpoint=require_checkpoint, ) else: + # Legacy (v1) providers keep the strict one-argument + # contract. v2 providers written against the original + # docs example (``def on_pre_compress(self, messages)``) + # also land here instead of dying on an unexpected + # kwarg — they simply never see the requirement signal. result = provider.on_pre_compress(provider_messages) if result and result.strip(): parts.append(result) diff --git a/tests/agent/test_pre_compress_checkpoint_contract.py b/tests/agent/test_pre_compress_checkpoint_contract.py index ff146f0b3c..24ae249719 100644 --- a/tests/agent/test_pre_compress_checkpoint_contract.py +++ b/tests/agent/test_pre_compress_checkpoint_contract.py @@ -215,6 +215,49 @@ def test_checkpoint_provider_receives_false_in_best_effort_mode(): assert durable.require_checkpoint_calls == [False] +def test_v2_provider_with_bare_signature_still_works(): + """v2 providers written against the original docs example + (``def on_pre_compress(self, messages)``) must not TypeError when the + host forwards the checkpoint requirement — they fall back to the legacy + call shape and simply never see the signal.""" + manager = MemoryManager() + + class _BareSignatureCheckpointProvider(_BaseStubProvider): + pre_compress_checkpoint_api_version = PRE_COMPRESS_CHECKPOINT_API_VERSION + + bare = _BareSignatureCheckpointProvider("bare-v2") + manager.add_provider(bare) + + combined = manager.on_pre_compress( + [{"role": "user", "content": "evidence"}], + require_checkpoint=True, + ) + + assert combined == "bare-v2 context" + assert bare.pre_compress_calls + + +def test_v2_provider_with_kwargs_catchall_receives_signal(): + manager = MemoryManager() + observed = {} + + class _KwargsCheckpointProvider(_BaseStubProvider): + pre_compress_checkpoint_api_version = PRE_COMPRESS_CHECKPOINT_API_VERSION + + def on_pre_compress(self, messages, **kwargs): + observed.update(kwargs) + return "kw context" + + manager.add_provider(_KwargsCheckpointProvider("kw")) + + manager.on_pre_compress( + [{"role": "user", "content": "evidence"}], + require_checkpoint=True, + ) + + assert observed == {"require_checkpoint": True} + + def test_manager_require_checkpoint_propagates_checkpoint_provider_failure(): manager = MemoryManager() manager.add_provider(_FailingCheckpointProvider("durable")) diff --git a/website/docs/developer-guide/memory-provider-plugin.md b/website/docs/developer-guide/memory-provider-plugin.md index 26a54b7fcb..d73327da69 100644 --- a/website/docs/developer-guide/memory-provider-plugin.md +++ b/website/docs/developer-guide/memory-provider-plugin.md @@ -148,7 +148,9 @@ class MyArchivingProvider(MemoryProvider): # contract: best-effort semantics, raw message list. pre_compress_checkpoint_api_version = 2 - def on_pre_compress(self, messages): + def on_pre_compress(self, messages, *, require_checkpoint=False): + # require_checkpoint mirrors the operator's checkpoint_required + # setting: True means a raise here blocks the lossy rewrite. ids = self._archive(messages) # must be durable before returning return f"checkpoint: {ids}" # forwarded into the summary prompt ```