Skip to content

The agent that grows with you

fix(acp): refresh the context indicator after /reset and /compress

unsupportedpastels62629e285614
diff --git a/acp_adapter/gateway_commands.py b/acp_adapter/gateway_commands.py
index a3b22de0f42..ea260aabb60 100644
--- a/acp_adapter/gateway_commands.py
+++ b/acp_adapter/gateway_commands.py
@@ -85,6 +85,8 @@ async def _mutate(agent, client, session_id, command, arg):
reply = _summary_text(result) or 'No changes from compression.'
else:
agent._snapshots[session_id] = await client.rpc('session.resume', session_id=session_id)
+ if operation in {'reset', 'compress'}:
+ await _refresh_context(agent, client, session_id, operation)
reply = {'model': lambda: _model_text('Model switched to', result),
'compress': lambda: _summary_text(result) or 'Context compressed.',
'reset': lambda: 'Conversation history cleared.'}[operation]()
@@ -92,6 +94,20 @@ async def _mutate(agent, client, session_id, command, arg):
return reply
+async def _refresh_context(agent, client, session_id, operation):
+ """The editor's context indicator after /reset or /compress. Both are session boundaries that
+ evict the owner's agent, so ``session.stats`` has no window until the next turn builds one: the
+ window shown stays (the model is unchanged) and its occupancy is the new transcript's, nothing
+ after a reset, the compressed messages' estimate (the summary's own figure) after a compress."""
+ stats = await client.rpc('session.stats', session_id=session_id)
+ if stats.get('context_length') is None:
+ from agent.model_metadata import estimate_request_tokens_rough
+ messages = agent._snapshots[session_id].get('messages') or []
+ stats = {**stats, 'context_length': (agent._usage.get(session_id) or {}).get('context_length'),
+ 'context_tokens': estimate_request_tokens_rough(messages) if operation == 'compress' else 0}
+ await agent._project_usage(session_id, agent._editor_id(session_id), stats)
+
+
def _summary_text(result):
summary = result.get('summary') or {}
return '\n'.join(filter(None, (summary.get('headline'), summary.get('token_line'), summary.get('note'))))
diff --git a/tests/acp_adapter/test_gateway_session_commands.py b/tests/acp_adapter/test_gateway_session_commands.py
index e0baa1baaee..73bf339af8c 100644
--- a/tests/acp_adapter/test_gateway_session_commands.py
+++ b/tests/acp_adapter/test_gateway_session_commands.py
@@ -1,6 +1,6 @@
"""``/queue``, ``/steer`` and the edit-policy mode on the gateway path, against a fake owner."""
import asyncio
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, MagicMock
import acp
import pytest
@@ -120,3 +120,39 @@ async def test_only_the_owners_edit_policy_can_be_selected():
with pytest.raises(GatewayClientError, match='acp_edit_policy_mutation_unavailable'):
await agent.set_session_mode('accept_edits', 's')
agent._gateway.rpc.assert_not_awaited()
+
+
+def _context_shown(agent):
+ return [(call.kwargs['update'].size, call.kwargs['update'].used)
+ for call in agent._conn.session_update.await_args_list
+ if call.kwargs['update'].session_update == 'usage_update']
+
+
+def _boundary_agent(messages):
+ """/reset and /compress commit, then evict the owner's agent: ``session.stats`` has no window."""
+ async def rpc(method, **params):
+ if method == 'session.resume':
+ return {'execution_generation': 8, 'messages': messages}
+ assert method == 'session.stats', method
+ return {'input_tokens': 0, 'context_tokens': 0, 'context_length': None}
+
+ agent = _agent(rpc)
+ agent._usage['s'] = {'context_length': 200000, 'context_tokens': 150000}
+ agent._mutations = MagicMock(apply=AsyncMock(return_value={'status': 'applied', 'summary': {}}))
+ return agent
+
+
+async def test_reset_empties_the_editors_context_indicator():
+ agent = _boundary_agent([])
+ await agent.prompt([acp.text_block('/reset')], 's')
+ assert _context_shown(agent) == [(200000, 0)]
+
+
+async def test_compress_shows_the_compressed_transcripts_occupancy():
+ from agent.model_metadata import estimate_request_tokens_rough
+ kept = [{'role': 'user', 'content': 'summary of the earlier turns ' * 40}]
+ agent = _boundary_agent(kept)
+ await agent.prompt([acp.text_block('/compress')], 's')
+ assert _context_shown(agent) == [(200000, estimate_request_tokens_rough(kept))]