Skip to content

The agent that grows with you

fix(memory): preserve callback when context probe import fails

tachyon-rf41f4276f943
diff --git a/tests/tools/test_write_approval.py b/tests/tools/test_write_approval.py
index 2d312afc2c6..5433dde9c21 100644
--- a/tests/tools/test_write_approval.py
+++ b/tests/tools/test_write_approval.py
@@ -318,6 +318,39 @@ def test_memory_inline_deny_blocks(hermes_home, approval_callback_cleanup):
assert wa.pending_count("memory") == 0 # denied, not staged
+def test_single_query_probe_import_failure_preserves_interactive_callback(
+ approval_callback_cleanup, monkeypatch
+):
+ """A broken headless-context probe must not disable a live CLI callback."""
+ import sys
+
+ from tools import write_approval as wa
+ from tools.terminal_tool import set_approval_callback
+
+ set_approval_callback(lambda *args, **kwargs: "once")
+ monkeypatch.setitem(sys.modules, "tools.approval", None)
+
+ assert wa._interactive_approval_available() is True
+
+
+def test_single_query_probe_exception_fails_safe_to_staging(
+ approval_callback_cleanup, monkeypatch
+):
+ """A broken headless-context probe must not fall through to prompting."""
+ from tools import approval
+ from tools import write_approval as wa
+ from tools.terminal_tool import set_approval_callback
+
+ set_approval_callback(lambda *args, **kwargs: "once")
+ monkeypatch.setattr(
+ approval,
+ "_is_single_query_approval_context",
+ lambda: (_ for _ in ()).throw(RuntimeError("probe failed")),
+ )
+
+ assert wa._interactive_approval_available() is False
+
+
def test_single_query_memory_stages_without_inline_prompt(
hermes_home, approval_callback_cleanup, monkeypatch
):
diff --git a/tools/write_approval.py b/tools/write_approval.py
index eae1a9a9119..f5620205d7d 100644
--- a/tools/write_approval.py
+++ b/tools/write_approval.py
@@ -239,10 +239,21 @@ def _interactive_approval_available() -> bool:
"""
try:
from tools.approval import _is_single_query_approval_context
-
- if _is_single_query_approval_context():
+ except ImportError:
+ # Keep the pre-existing callback path available if the optional context
+ # probe cannot be imported during early startup.
+ pass
+ else:
+ try:
+ if _is_single_query_approval_context():
+ return False
+ except Exception:
+ # An unreadable headless-context probe must fail safe to staging.
return False
+
+ try:
from tools.terminal_tool import _get_approval_callback
+
return _get_approval_callback() is not None
except Exception:
return False