The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(memory): stage headless single-query writes
tachyon-r9023a4af8249
diff --git a/tests/tools/test_write_approval.py b/tests/tools/test_write_approval.pyindex 9518e012364..2d312afc2c6 100644--- a/tests/tools/test_write_approval.py+++ b/tests/tools/test_write_approval.py@@ -317,6 +317,36 @@ def test_memory_inline_deny_blocks(hermes_home, approval_callback_cleanup):assert store.memory_entries == []assert wa.pending_count("memory") == 0 # denied, not staged++def test_single_query_memory_stages_without_inline_prompt(+ hermes_home, approval_callback_cleanup, monkeypatch+):+ """Headless ``hermes chat -q`` runs have no human to answer a callback."""+ from tools.memory_tool import memory_tool, MemoryStore+ from tools.terminal_tool import set_approval_callback+ from tools import write_approval as wa++ _set_approval("memory", True)+ monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")++ calls = []++ def approve_if_prompted(*args, **kwargs):+ calls.append((args, kwargs))+ return "once"++ set_approval_callback(approve_if_prompted)+ store = MemoryStore(); store.load_from_disk()++ r = json.loads(memory_tool("add", "memory", "headless fact", store=store))++ assert calls == []+ assert r["success"] is True+ assert r["staged"] is True+ assert store.memory_entries == []+ assert wa.pending_count("memory") == 1++def test_memory_invalid_params_rejected_before_staging(hermes_home):# Param validation must run BEFORE the gate so a broken write is rejected# immediately instead of staged and failing at approve time.diff --git a/tools/write_approval.py b/tools/write_approval.pyindex b27db26e4df..eae1a9a9119 100644--- a/tools/write_approval.py+++ b/tools/write_approval.py@@ -216,6 +216,38 @@ def evaluate_gate(subsystem: str, *, inline_summary: str = "", inline_detail: streturn GateDecision(blocked=True, message="Memory write denied by user. The change was not saved.")+def _interactive_approval_available() -> bool:+ """True when a foreground memory write can be approved inline.++ Inline prompting requires both a live interactive surface and a per-thread+ approval callback registered by the interactive CLI+ (``tools.terminal_tool.set_approval_callback``). A callback alone is not+ sufficient: headless ``hermes chat -q`` / Kanban workers also register one,+ but have no human available to answer it.++ Every other surface stages instead:++ * **Single-query/Kanban workers** — headless even though a CLI callback is+ registered; inline prompting would wait until the approval timeout.+ * **Gateway/API sessions** — the dangerous-command ``/approve`` round-trip+ lives in the pending-approval queue (``submit_pending`` ++ ``_await_gateway_decision``), which ``prompt_dangerous_approval`` never+ reaches; trying to prompt from a gateway session would hit the+ ``input()`` fallback and silently deny. Staging gives the user a real+ review affordance (``/memory pending``) instead.+ * Scripts, cron, and background threads — no user present.+ """+ try:+ from tools.approval import _is_single_query_approval_context++ if _is_single_query_approval_context():+ return False+ from tools.terminal_tool import _get_approval_callback+ return _get_approval_callback() is not None+ except Exception:+ return False++def _prompt_inline_memory_approval(summary: str, detail: str) -> Optional[bool]:"""Prompt inline for a memory write: True approved, False denied, None → stage. Uses the per-threadCLI approval callback (``tools.terminal_tool.set_approval_callback``) directly, not@@ -225,6 +257,8 @@ def _prompt_inline_memory_approval(summary: str, detail: str) -> Optional[bool]:See #15216."""+ if not _interactive_approval_available():+ return Nonetry:from tools.terminal_tool import _get_approval_callbackexcept Exception: