fix(agent-runner): package context for plugin execution

This commit is contained in:
huanghuoguoguo
2026-05-21 13:56:17 +08:00
parent 26923c66c0
commit 094b87e578
10 changed files with 393 additions and 47 deletions

View File

@@ -309,6 +309,62 @@ async def test_orchestrator_runs_fake_plugin_with_authorized_context():
assert await get_session_registry().get(context["run_id"]) is None
@pytest.mark.asyncio
async def test_orchestrator_packages_legacy_max_round_without_mutating_query():
descriptor = make_descriptor()
plugin_connector = FakePluginConnector(
results=[
{
"type": "message.completed",
"data": {"message": {"role": "assistant", "content": "fake response"}},
}
]
)
ap = FakeApplication(plugin_connector)
orchestrator = AgentRunOrchestrator(ap, FakeRegistry(descriptor))
query = make_query()
query.pipeline_config["ai"]["runner_config"][RUNNER_ID]["max-round"] = 2
query.messages = [
provider_message.Message(role="user", content="message 1"),
provider_message.Message(role="assistant", content="response 1"),
provider_message.Message(role="user", content="message 2"),
provider_message.Message(role="assistant", content="response 2"),
provider_message.Message(role="user", content="message 3"),
provider_message.Message(role="assistant", content="response 3"),
]
messages = [message async for message in orchestrator.run_from_query(query)]
assert len(messages) == 1
context = plugin_connector.contexts[0]
assert [message["content"] for message in context["messages"]] == [
"message 2",
"response 2",
"message 3",
"response 3",
]
assert [message.content for message in query.messages] == [
"message 1",
"response 1",
"message 2",
"response 2",
"message 3",
"response 3",
]
assert context["runtime"]["metadata"]["context_packaging"] == {
"policy": {
"mode": "legacy_max_round",
"max_round": 2,
},
"history": {
"source": "query.messages",
"source_total_count": 6,
"delivered_count": 4,
"messages_complete": False,
},
}
@pytest.mark.asyncio
async def test_orchestrator_streams_fake_plugin_deltas():
descriptor = make_descriptor()

View File

@@ -48,6 +48,18 @@ def get_round_truncator_module():
def make_truncate_config(max_round: int = 5):
"""Create a pipeline config with max-round setting."""
return {
'msg-truncate': {
'method': 'round',
'round': {
'max-round': max_round,
},
},
}
def make_agent_runner_config(max_round: int = 5):
"""Create an AgentRunner pipeline config with max-round binding config."""
return {
'ai': {
'runner': {'id': RUNNER_ID},
@@ -137,6 +149,36 @@ class TestRoundTruncatorProcess:
# All messages should be preserved
assert len(result.new_query.messages) == 5
@pytest.mark.asyncio
async def test_agent_runner_path_skips_pipeline_truncation(self):
"""AgentRunner path should leave query.messages intact at pipeline stage."""
msgtrun = get_msgtrun_module()
entities = get_entities_module()
app = FakeApp()
stage = msgtrun.ConversationMessageTruncator(app)
pipeline_config = make_agent_runner_config(max_round=1)
await stage.initialize(pipeline_config)
query = text_query("current")
query.pipeline_config = pipeline_config
query.messages = [
provider_message.Message(role='user', content='old1'),
provider_message.Message(role='assistant', content='old1_resp'),
provider_message.Message(role='user', content='current'),
]
result = await stage.process(query, 'ConversationMessageTruncator')
assert result.result_type == entities.ResultType.CONTINUE
assert [(msg.role, msg.content) for msg in result.new_query.messages] == [
('user', 'old1'),
('assistant', 'old1_resp'),
('user', 'current'),
]
@pytest.mark.asyncio
async def test_truncate_exceeds_limit(self):
"""Messages exceeding max-round should be truncated precisely.