Persist active run event history

This commit is contained in:
武阳
2026-05-07 17:40:46 +08:00
parent 847b74ffb8
commit d57570e527
5 changed files with 316 additions and 24 deletions
+46
View File
@@ -285,6 +285,52 @@ class AgentRuntimeTests(unittest.TestCase):
self.assertGreaterEqual(len(result.transcript), 5)
self.assertGreaterEqual(len(result.file_history), 0)
def test_agent_persists_max_turns_explanation_after_tool_result(self) -> None:
responses = [
{
'choices': [
{
'message': {
'role': 'assistant',
'content': '我先读取文件。',
'tool_calls': [
{
'id': 'call_1',
'type': 'function',
'function': {
'name': 'read_file',
'arguments': '{"path": "hello.txt"}',
},
}
],
},
'finish_reason': 'tool_calls',
}
]
}
]
with tempfile.TemporaryDirectory() as tmp_dir:
workspace = Path(tmp_dir)
(workspace / 'hello.txt').write_text('hello world\n', encoding='utf-8')
with patch(
'src.openai_compat.request.urlopen',
side_effect=make_urlopen_side_effect(responses),
):
agent = LocalCodingAgent(
model_config=ModelConfig(
model='Qwen/Qwen3-Coder-30B-A3B-Instruct',
base_url='http://127.0.0.1:8000/v1',
),
runtime_config=AgentRuntimeConfig(cwd=workspace, max_turns=1),
)
result = agent.run('Inspect hello.txt')
self.assertEqual(result.stop_reason, 'max_turns')
self.assertIn('最大步骤上限', result.final_output)
self.assertEqual(result.transcript[-1]['role'], 'assistant')
self.assertEqual(result.transcript[-1]['stop_reason'], 'max_turns')
self.assertIn('最大步骤上限', result.transcript[-1]['content'])
def test_agent_stops_after_data_agent_goal_requires_review(self) -> None:
responses = [
{
+37
View File
@@ -262,6 +262,43 @@ class GuiServerTests(unittest.TestCase):
self.assertEqual(payload['status'], 'running')
self.assertEqual(payload['current_stage'], 'first stage')
def test_latest_run_includes_runtime_event_history(self) -> None:
with tempfile.TemporaryDirectory() as d:
client, state = _build_client(Path(d))
record = state.run_manager.start('alice', 'thread-1')
state.run_manager.update(record.run_id, status='running')
state.run_manager.record_event(
record.run_id,
{
'type': 'tool_start',
'tool_name': 'python_exec',
'tool_call_id': 'call-1',
'arguments': {'code': 'print(1)'},
'assistant_content': '我用 Python 分析一下。',
},
)
state.run_manager.record_event(
record.run_id,
{
'type': 'tool_result',
'tool_name': 'python_exec',
'tool_call_id': 'call-1',
'ok': True,
'metadata': {'output_preview': 'exit_code=0'},
},
)
latest = client.get(
'/api/runs/latest',
params={'account_id': 'alice', 'session_id': 'thread-1'},
)
self.assertEqual(latest.status_code, 200)
events = latest.json()['events']
self.assertEqual([event['type'] for event in events], ['tool_start', 'tool_result'])
self.assertEqual(events[0]['tool_name'], 'python_exec')
self.assertEqual(events[1]['metadata']['output_preview'], 'exit_code=0')
def test_cancel_run_marks_streaming_session_tail_cancelled(self) -> None:
with tempfile.TemporaryDirectory() as d:
root = Path(d)