Skip to content

The agent that grows with you

fix(gateway): /memory review verbs keep main's reach on local sessions

unsupportedpastelse7411431f03d
diff --git a/gateway/session_commands.py b/gateway/session_commands.py
index 25b6bb9fb86..6bcb2323be6 100644
--- a/gateway/session_commands.py
+++ b/gateway/session_commands.py
@@ -61,12 +61,12 @@ def _first_token(arg):
# The read-only argument forms of a read command whose other subcommands write: ``/usage reset``
-# redeems a credit, ``/memory approve|approval`` applies writes / edits config.yaml,
-# ``/suggestions accept|catalog`` schedules cron jobs delivering to this (non-messaging) chat.
+# redeems a credit, ``/suggestions accept|catalog`` schedules cron jobs delivering to this
+# (non-messaging) chat. The review queues read like main's CLI: only the first word counts.
_READ_ARGUMENTS = {
'skills': lambda arg: _first_token(arg) in {'', 'pending', 'diff'},
'usage': lambda arg: not arg.strip(),
- 'memory': lambda arg: arg.strip().lower() in {'', 'pending'},
+ 'memory': lambda arg: _first_token(arg) in {'', 'pending'},
'suggestions': lambda arg: not arg.strip(),
'kanban': lambda arg: _first_token(arg) in _KANBAN_READS,
}
diff --git a/tests/gateway/test_session_memory_review.py b/tests/gateway/test_session_memory_review.py
new file mode 100644
index 00000000000..ed1b823dae4
--- /dev/null
+++ b/tests/gateway/test_session_memory_review.py
@@ -0,0 +1,107 @@
+"""/memory and /skills review verbs on a LOCAL session match the classic CLI (#138901).
+
+The classic CLI hands every /memory argument (and /skills review word) to
+``hermes_cli.write_approval_commands.handle_pending_subcommand``; a local session must give each
+subcommand that handler serves the same reach: its listings are reads, its writes are controls."""
+import asyncio
+import json
+from http.server import ThreadingHTTPServer
+from pathlib import Path
+import signal
+import threading
+import time
+
+import pytest
+
+from tests.gateway.fixtures.local_recovery_probe import Model, child_env, daemon, rpc, websocket
+
+_WRITES = {verb: 'control' for verb in ('approve', 'apply', 'reject', 'deny', 'drop', 'approval', 'mode')}
+# Every review subcommand the classic CLI serves, by the reach it needs on a local session: a
+# listing is a read; an approve/reject or the approval switch is a control.
+_REVIEW_VERBS = {'memory': {'pending': 'read', **_WRITES}, 'skills': {'pending': 'read', 'diff': 'read', **_WRITES}}
+
+
[email protected]('subsystem', ['memory', 'skills'])
+def test_each_review_subcommand_the_cli_serves_keeps_its_reach_on_a_local_session(tmp_path, monkeypatch, subsystem):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ from gateway.session_commands import command_verdict
+ from hermes_cli.write_approval_commands import handle_pending_subcommand
+ for verb, reach in _REVIEW_VERBS[subsystem].items():
+ # The CLI answers it (an empty queue, an unknown id, an invalid switch value): it is a review verb.
+ assert handle_pending_subcommand(subsystem, [verb, 'x']) is not None, verb
+ for arg in (verb, f'{verb} x', f'{verb.upper()} x'):
+ assert command_verdict(subsystem, arg) == reach, (subsystem, arg)
+ assert command_verdict(subsystem, '') == 'read'
+
+
+def test_memory_has_no_diff_like_the_cli(tmp_path, monkeypatch):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ from gateway.session_commands import command_verdict
+ from hermes_cli.write_approval_commands import handle_pending_subcommand
+ assert handle_pending_subcommand('memory', ['diff', 'x']) is None
+ assert command_verdict('memory', 'diff x') == 'refused'
+
+
+def _stage(home, pending_id, content):
+ folder = home / 'pending' / 'memory'
+ folder.mkdir(parents=True, exist_ok=True)
+ (folder / f'{pending_id}.json').write_text(json.dumps({
+ 'id': pending_id, 'subsystem': 'memory', 'action': 'add', 'summary': f'add {content}',
+ 'origin': 'foreground', 'created_at': time.time(),
+ 'payload': {'action': 'add', 'target': 'memory', 'content': content}}))
+
+
+def test_local_session_reviews_staged_memory_writes(tmp_path):
+ root = Path(__file__).resolve().parents[2]
+ home, user, work = tmp_path / 'state', tmp_path / 'user', tmp_path / 'work'
+ home.mkdir(mode=0o700)
+ user.mkdir()
+ work.mkdir()
+ _stage(home, 'keepme01', 'MEMORY_REVIEW_APPROVED')
+ _stage(home, 'dropme02', 'MEMORY_REVIEW_REJECTED')
+ _stage(home, 'dropme03', 'MEMORY_REVIEW_REJECTED_ALL')
+ peer = ThreadingHTTPServer(('127.0.0.1', 0), Model)
+ peer.requests = []
+ peer.blocked, peer.release = threading.Event(), threading.Event()
+ thread = threading.Thread(target=peer.serve_forever, daemon=True)
+ thread.start()
+ url = f'http://127.0.0.1:{peer.server_port}/v1'
+ (home / 'config.yaml').write_text(json.dumps({
+ 'gateway': {'multiplex_profiles': False},
+ 'model': {'provider': 'custom', 'default': 'review-model', 'base_url': url},
+ 'auxiliary': {'title_generation': {'enabled': False}}}))
+ env = child_env()
+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),
+ OPENAI_API_KEY='loopback-only', OPENAI_BASE_URL=url, PYTHONUNBUFFERED='1')
+ out, config = {}, {}
+ commands = ('memory approve keepme01', 'memory reject dropme02', 'memory approval on',
+ 'memory mode off', 'memory apply nope', 'memory deny nope', 'memory drop all')
+
+ async def probe(desc):
+ async with websocket(home, desc) as ws:
+ created = await rpc(ws, 'session.create', request_id='review', source='cli', toolsets=['todo'], cwd=str(work))
+ sid = created['result']['session_id']
+ for command in commands:
+ reply = await rpc(ws, 'slash.exec', session_id=sid, command=command)
+ out[command] = reply.get('result', {}).get('output') or reply.get('error', {}).get('message')
+ if command.startswith(('memory approval', 'memory mode')):
+ config[command] = (home / 'config.yaml').read_text()
+ try:
+ with daemon(root, home, env, barrier=False) as (proc, desc):
+ asyncio.run(probe(desc))
+ proc.send_signal(signal.SIGINT)
+ proc.wait(timeout=20)
+ finally:
+ peer.shutdown()
+ peer.server_close()
+ thread.join(timeout=5)
+ assert all(reply != 'unsupported_command' for reply in out.values()), out
+ assert 'Approved 1 memory write' in out['memory approve keepme01'], out
+ assert any('MEMORY_REVIEW_APPROVED' in path.read_text() for path in home.rglob('MEMORY.md')), out
+ assert "Rejected pending memory write 'dropme02'" in out['memory reject dropme02'], out
+ assert 'write_approval: true' in config['memory approval on'], config
+ assert 'write_approval: false' in config['memory mode off'], config
+ assert "No pending memory write with id 'nope'" in out['memory deny nope'], out
+ assert 'Rejected 1 pending memory write' in out['memory drop all'], out
+ assert not list((home / 'pending' / 'memory').glob('*.json'))
diff --git a/tests/gateway/test_session_read_commands.py b/tests/gateway/test_session_read_commands.py
index 7a57a5b6ed2..fab483c1db0 100644
--- a/tests/gateway/test_session_read_commands.py
+++ b/tests/gateway/test_session_read_commands.py
@@ -64,7 +64,7 @@ def test_local_session_reads_report_this_session_and_refuse_their_write_forms(tm
assert time.monotonic() < deadline, ('warm turn never settled', snap)
await asyncio.sleep(.1)
for command in ('usage', 'insights', 'profile', 'topup', 'diff', 'memory', 'kanban list',
- 'suggestions', 'bundles', 'usage reset', 'memory approval on', 'kanban create x',
+ 'suggestions', 'bundles', 'usage reset', 'kanban create x',
'suggestions catalog', 'agents', 'sessions', 'platform',
'toolsets', 'platforms', 'skills pending', 'skills reject nope', 'skills install x',
'memory reject nope'):
@@ -107,7 +107,8 @@ def test_local_session_reads_report_this_session_and_refuse_their_write_forms(tm
assert out['memory reject nope'] != 'unsupported_command', out['memory reject nope']
# Write forms, messaging-model state and gateway-wide views stay refused (the per-session settings
# run: tests/gateway/test_session_setting_commands.py).
- for command in ('usage reset', 'memory approval on', 'kanban create x', 'suggestions catalog',
+ # The /memory write forms are controls (tests/gateway/test_session_memory_review.py).
+ for command in ('usage reset', 'kanban create x', 'suggestions catalog',
'agents', 'sessions', 'platform'):
assert out[command] == 'unsupported_command', (command, out[command])
assert (home / 'config.yaml').read_text() == config