The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): manual /compress runs through the configured context engine
Teknium05025ee96ef9
diff --git a/gateway/session_mutation_compress.py b/gateway/session_mutation_compress.pyindex b45d2b8d2d2..7756cc30d92 100644--- a/gateway/session_mutation_compress.py+++ b/gateway/session_mutation_compress.py@@ -3,6 +3,7 @@ import asyncioimport jsonimport loggingfrom pathlib import Path+from types import SimpleNamespacefrom hermes_state_runtime import RuntimeStoreError@@ -69,6 +70,15 @@ async def prepare_compress(authority, live, payload, prepared):with policy_scope(policy, authority=authority):from agent.conversation_compression import _pre_compress_memory_contextmemory_context = _pre_compress_memory_context(resident, head, False) if resident is not None else ''+ engine = _plugin_engine(authority, policy, config, model, runtime, resident, snapshot['target'])+ if engine is not None:+ from agent.conversation_compression import _supported_compression_kwargs+ compressed = engine.compress(json.loads(json.dumps(head)), **_supported_compression_kwargs(+ engine.compress, current_tokens=estimate_request_tokens_rough(head),+ focus_topic=request.focus_topic, force=True, memory_context=memory_context))+ if compressed == head:+ return None, engine+ return rejoin_compressed_head_and_tail(compressed, tail), enginecompressor = ContextCompressor(model, base_url=runtime.get('base_url') or '',api_key=runtime.get('api_key') or '', provider=runtime.get('provider') or '',api_mode=runtime.get('api_mode') or '', quiet_mode=True, abort_on_summary_failure=True,@@ -96,10 +106,69 @@ async def prepare_compress(authority, live, payload, prepared):hook = None if policy.safe_mode else { # same rough basis as the agent path's compaction record'before_tokens': estimate_messages_tokens_rough(messages),'after_tokens': estimate_messages_tokens_rough(compressed), 'messages_removed': len(messages) - len(compressed)}- return dict(prepared, messages=compressed, summary=summary, post_compaction=hook,+ # The committed boundary is announced to the engine that compressed (``on_session_start``+ # with boundary_reason="compression"), as the agent path does after its own commit.+ engine_owner = SimpleNamespace(context_compressor=compressor, platform=_platform(live.source),+ _gateway_session_key=live.route,+ owned=resident is None and not isinstance(compressor, ContextCompressor))+ return dict(prepared, messages=compressed, summary=summary, post_compaction=hook, engine_owner=engine_owner,in_place=is_truthy_value(options.get('in_place'), default=True))+def _plugin_engine(authority, policy, config, model, runtime, resident, target):+ """The session's configured plugin context engine, or None for the built-in compressor.++ A resident agent's engine is the one bound to this session. A cold or managed-worker session+ gets the engine its frozen policy names, selected and bound the way agent init does, so a+ plugin engine compresses its own history instead of having it rewritten underneath it."""+ from agent.context_compressor import ContextCompressor+ if resident is not None:+ engine = getattr(resident, 'context_compressor', None)+ return None if engine is None or isinstance(engine, ContextCompressor) else engine+ if policy.safe_mode:+ return None+ from agent.agent_init import _select_context_engine+ engine = _select_context_engine(config)+ if engine is None:+ return None+ from agent.model_metadata import get_model_context_length+ base_url, api_key = runtime.get('base_url') or '', runtime.get('api_key') or ''+ provider = runtime.get('provider') or ''+ context_length = get_model_context_length(model, base_url=base_url, api_key=api_key, provider=provider,+ custom_providers=config.get('custom_providers'))+ engine.update_model(model=model, context_length=context_length, base_url=base_url, api_key=api_key,+ provider=provider, api_mode=runtime.get('api_mode') or '')+ bind = getattr(engine, 'bind_session_state', None)+ if callable(bind):+ bind(session_db=authority.db, session_id=target)+ return engine+++def _platform(source):+ platform = getattr(source, 'platform', None)+ return getattr(platform, 'value', platform) or 'cli'+++async def notify_committed_compress(authority, prepared, result):+ """The agent path's post-commit notification for a committed ``/compress``: the context engine's+ compression boundary plus relay/metrics segment hand-off. A cold session's engine was built for+ this compression only, so it also gets the session-end a resident agent's boundary eviction gives."""+ from agent.conversation_compression import _notify_context_engine_compression_complete+ from gateway.run import _async_profile_runtime_scope+ owner, target = prepared['engine_owner'], result['target_session_id']++ def notify():+ _notify_context_engine_compression_complete(+ owner, new_session_id=target, old_session_id=result['previous_target_session_id'])+ if owner.owned:+ owner.context_compressor.on_session_end(target, prepared['messages'])+ try:+ async with _async_profile_runtime_scope(Path(authority.profile_id)):+ await asyncio.to_thread(notify)+ except Exception: # health: allow BLE001 -- the compaction is committed; an engine failure must not fail the reply+ logger.warning('context engine compression notification failed', exc_info=True)++async def fire_post_compaction_hook(authority, prepared, result):"""``post_compaction`` for a committed ``/compress``: this path summarizes outside the agent's``compress_context``, so it fires here, after the commit, in the session's profile."""diff --git a/gateway/session_mutations.py b/gateway/session_mutations.pyindex 78cb0cf4b63..9a85e3179c7 100644--- a/gateway/session_mutations.py+++ b/gateway/session_mutations.py@@ -268,6 +268,10 @@ async def write_mutation(authority, ref, params, editor_servers, *, principal_idfrom gateway.session_onboarding import ending_run_on_deletecommit = partial(ending_run_on_delete, authority.db, ref.session_id, commit)result = await asyncio.to_thread(commit)+ if operation == 'compress' and applied:+ # Before the projection's boundary eviction ends the resident agent's engine session.+ from gateway.session_mutation_compress import notify_committed_compress+ await notify_committed_compress(authority, prepared, result)try:await _project_result(authority, ref, live, params, result, editor_servers, applied=applied)finally:diff --git a/tests/gateway/test_session_mutation_compress_plugin_engine.py b/tests/gateway/test_session_mutation_compress_plugin_engine.pynew file mode 100644index 00000000000..c629e8b4a2d--- /dev/null+++ b/tests/gateway/test_session_mutation_compress_plugin_engine.py@@ -0,0 +1,129 @@+"""Gateway ``/compress`` runs through the session's configured plugin context engine (#143587).++The unified gateway summarized every manual ``/compress`` with a native ``ContextCompressor`` and+published the rewrite, so a plugin engine never compressed its own history nor saw the compaction+boundary; the next agent's engine then re-stored the retained replies as new rows.+"""+import asyncio+from contextlib import closing+from http.server import ThreadingHTTPServer+import json+from pathlib import Path+import sqlite3+import threading++import pytest+from tests.gateway.fixtures.local_recovery_probe import child_env, daemon, rpc, websocket+from tests.gateway.test_session_mutation_compress import SummaryModel+from tests.waiting import async_wait_until++ENGINE = '''+import json, os+from agent.context_engine import ContextEngine++def _record(event, **fields):+ with open(os.environ["ENGINE_LOG"], "a") as log:+ log.write(json.dumps({"event": event, **fields}) + "\\n")++class RecordingEngine(ContextEngine):+ @property+ def name(self):+ return "recording_engine"++ def update_from_response(self, usage):+ pass++ def should_compress(self, prompt_tokens=None):+ return False++ def compress(self, messages, current_tokens=None, focus_topic=None, force=False, memory_context=""):+ _record("compress", count=len(messages), focus_topic=focus_topic, force=force)+ return [messages[0], {"role": "assistant", "content": "ENGINE_SUMMARY"}, *messages[-2:]]++ def on_session_start(self, session_id, **kwargs):+ _record("start", session_id=session_id, boundary_reason=kwargs.get("boundary_reason"),+ old_session_id=kwargs.get("old_session_id"))++ def on_session_end(self, session_id, messages):+ _record("end", session_id=session_id)++def register(ctx):+ ctx.register_context_engine(RecordingEngine())+'''++[email protected]('resident', [True, False], ids=['resident-agent', 'cold-session'])+def test_manual_compress_uses_configured_plugin_engine(tmp_path, resident):+ root = Path(__file__).resolve().parents[2]+ home, user = tmp_path / 'state', tmp_path / 'user'+ home.mkdir(mode=0o700)+ user.mkdir()+ (home / 'plugins' / 'recording_engine').mkdir(parents=True)+ (home / 'plugins' / 'recording_engine' / '__init__.py').write_text(ENGINE)+ log = tmp_path / 'engine.jsonl'+ peer = ThreadingHTTPServer(('127.0.0.1', 0), SummaryModel)+ peer.requests = []+ thread = threading.Thread(target=peer.serve_forever, daemon=True)+ thread.start()+ url = f'http://127.0.0.1:{peer.server_port}/v1'+ cfg = {'gateway': {'multiplex_profiles': False}, 'context': {'engine': 'recording_engine'},+ 'model': {'provider': 'custom', 'default': 'original', 'base_url': url},+ 'auxiliary': {'title_generation': {'enabled': False},+ 'compression': {'provider': 'custom', 'model': 'summary', 'base_url': url}},+ 'platform_toolsets': {'cli': []}, 'compression': {'in_place': True}}+ (home / 'config.yaml').write_text(json.dumps(cfg))+ (home / 'models_dev_cache.json').write_text(json.dumps({'custom': {'id': 'custom', 'models': {+ name: {'id': name, 'name': name, 'limit': {'context': 1000000, 'output': 4000}}+ for name in ('original', 'summary')}}}))+ 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', ENGINE_LOG=str(log))++ async def call(ws, method, **params):+ response = await rpc(ws, method, **params)+ assert 'result' in response, response+ return response['result']++ async def history(desc):+ async with websocket(home, desc) as ws:+ sid = (await call(ws, 'session.create', source='cli', request_id='owner', toolsets=[],+ model='original', provider='custom', base_url=url))['session_id']+ for i in range(3):+ await call(ws, 'prompt.submit', session_id=sid, input_id=f'H{i}', text=f'HISTORY_{i}')++ def terminal(text=f'H{i}'):+ with closing(sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True)) as db:+ row = db.execute('SELECT status FROM session_admissions WHERE request_id=?', (text,)).fetchone()+ return row and row[0] == 'terminal'+ await async_wait_until(terminal, timeout=30, what=f'H{i} terminal')+ return sid++ async def compress(desc, sid):+ async with websocket(home, desc) as ws:+ before = await call(ws, 'session.resume', session_id=sid)+ result = await call(ws, 'session.mutate', session_id=sid, request_id='compress', operation='compress',+ payload={'focus': 'the plan'}, expected_revision=before['revision'],+ expected_generation=before['execution_generation'])+ return result, await call(ws, 'session.resume', session_id=sid)++ try:+ with daemon(root, home, env, barrier=False) as (_, desc):+ sid = asyncio.run(history(desc))+ if resident:+ result, after = asyncio.run(compress(desc, sid))+ if not resident: # a fresh gateway holds no agent for the session+ with daemon(root, home, env, barrier=False) as (_, desc):+ result, after = asyncio.run(compress(desc, sid))+ finally:+ peer.shutdown()+ peer.server_close()+ thread.join(timeout=5)++ events = [json.loads(line) for line in log.read_text().splitlines()]+ compresses = [e for e in events if e['event'] == 'compress']+ assert compresses and compresses[-1]['focus_topic'] == 'the plan' and compresses[-1]['force'] is True+ assert 'ENGINE_SUMMARY' in json.dumps(after['messages'])+ assert not any('context checkpoint' in json.dumps(r.get('messages')) for r in peer.requests) # no native summary+ boundary = [e for e in events if e['event'] == 'start' and e['boundary_reason'] == 'compression']+ assert boundary == [{'event': 'start', 'session_id': result['target_session_id'],+ 'boundary_reason': 'compression', 'old_session_id': result['previous_target_session_id']}]