Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(agent): interpolate current time into the compression prompt (#2867)
ydflow58014d4c5411
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex 4e63e6479..5e9ab43bc 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -590,12 +590,20 @@ class Agent:),)+ # Same time text as the runtime injection; keeps other braces intact+ timezone = self.injection_config.timezone+ now = datetime.now(_resolve_timezone(timezone))+ compression_prompt = cfg.compression_prompt.replace(+ "{current_time}",+ f"{now.strftime(self.injection_config.time_format)} ({timezone})",+ )+messages = (msgs_system+ msgs_to_compress+ instruction_msgs+ [- UserMsg(name="user", content=cfg.compression_prompt),+ UserMsg(name="user", content=compression_prompt),])@@ -650,7 +658,7 @@ class Agent:+ [UserMsg(name="user",- content=cfg.compression_prompt,+ content=compression_prompt,),])diff --git a/tests/compress_context_test.py b/tests/compress_context_test.pyindex c491a6c4f..7e3abd16a 100644--- a/tests/compress_context_test.py+++ b/tests/compress_context_test.py@@ -5,9 +5,11 @@ import hashlibimport jsonimport osimport tempfile+from datetime import datetimefrom typing import Anyfrom unittest.async_case import IsolatedAsyncioTestCase+from unittest.mock import patchfrom utils import MockModel, AnyString@@ -1513,6 +1515,79 @@ class ContextCompressionTest(IsolatedAsyncioTestCase):any(msg.get_content_blocks("hint") for msg in agent.state.context),)+ async def test_context_compression_resolves_current_time_placeholder(+ self,+ ) -> None:+ """The compression prompt reaches the model with the injection time."""+ model = RecordingStructuredMockModel(+ context_size=100,+ fail_structured_output_times=1,+ force_compression_overflow=True,+ )+ agent = Agent(+ name="Friday",+ system_prompt="".join(["0" for _ in range(20 * 4)]),+ model=model,+ context_config=ContextConfig(+ trigger_ratio=0.7,+ reserve_ratio=0.4,+ compression_prompt="Now is {current_time}. Keep {braces}.",+ ),+ injection_config=InjectionConfig(+ timezone="Asia/Shanghai",+ time_format="%Y-%m-%d %H:%M",+ ),+ state=AgentState(+ session_id="123",+ context=[+ UserMsg(+ "User",+ "".join(["1" for _ in range(30 * 4)]),+ id="1",+ ),+ AssistantMsg(+ "Friday",+ "".join(["2" for _ in range(10 * 4)]),+ id="2",+ ),+ UserMsg(+ "User",+ "".join(["3" for _ in range(10 * 4)]),+ id="3",+ ),+ ],+ ),+ toolkit=Toolkit(),+ )++ model.set_structured_response(+ StructuredResponse(+ content={+ "task_overview": "1",+ "current_state": "2",+ "important_discoveries": "3",+ "next_steps": "4",+ "context_to_preserve": "5",+ },+ ),+ )++ # The first call fails under overflow, so the retry path runs too+ with patch("agentscope.agent._agent.datetime") as mock_datetime:+ mock_datetime.now.return_value = datetime(2026, 7, 1, 12, 0)+ await agent.compress_context()++ self.assertListEqual(+ [+ msgs[-1].get_text_content()+ for msgs in model.recorded_structured_messages+ ],+ [+ "Now is 2026-07-01 12:00 (Asia/Shanghai). Keep {braces}.",+ "Now is 2026-07-01 12:00 (Asia/Shanghai). Keep {braces}.",+ ],+ )+async def test_context_compression_overflow_retry_keeps_instructions(self,) -> None: