fix(memory): tolerate bare-signature v2 providers when forwarding checkpoint requirement
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.
This commit is contained in:
+38
-1
@@ -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)
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user