refactor(agent): make run usage explicit (#5546)

* refactor(agent): make run usage explicit

* fix(api): capture usage per run
This commit is contained in:
chengyongru
2026-08-26 15:18:53 +08:00
committed by GitHub
parent 0c84725b13
commit 4f6c0aedfa
20 changed files with 184 additions and 233 deletions
+7 -7
View File
@@ -457,12 +457,12 @@ async def test_agent_loop_extra_hook_receives_calls(tmp_path):
)
loop.tools.get_definitions = MagicMock(return_value=[])
content, tools_used, messages, _, _ = await loop._run_agent_loop(
result = await loop._run_agent_loop(
[{"role": "user", "content": "hi"}],
runtime=loop.llm_runtime(),
)
assert content == "done"
assert result.final_content == "done"
assert "before_run" in events
assert "before_iter:0" in events
assert "after_iter:0" in events
@@ -545,12 +545,12 @@ async def test_agent_loop_extra_hook_error_isolation(tmp_path):
)
loop.tools.get_definitions = MagicMock(return_value=[])
content, _, _, _, _ = await loop._run_agent_loop(
result = await loop._run_agent_loop(
[{"role": "user", "content": "hi"}],
runtime=loop.llm_runtime(),
)
assert content == "still works"
assert result.final_content == "still works"
@pytest.mark.asyncio
@@ -590,11 +590,11 @@ async def test_agent_loop_no_hooks_backward_compat(tmp_path):
loop.tools.execute = AsyncMock(return_value="ok")
loop.max_iterations = 2
content, tools_used, _, _, _ = await loop._run_agent_loop(
result = await loop._run_agent_loop(
[], runtime=loop.llm_runtime()
)
assert content == (
assert result.final_content == (
"I reached the maximum number of tool call iterations (2) "
"without completing the task. You can try breaking the task into smaller steps."
)
assert tools_used == ["list_dir", "list_dir"]
assert result.tools_used == ["list_dir", "list_dir"]