fix: harden run-level hook lifecycle

maintainer edit: keep cancellation out of on_error so shutdown paths do not look like run failures, and let the SDK capture hook use the authoritative after_run snapshot.
This commit is contained in:
chengyongru
2026-06-05 01:09:45 +08:00
committed by Xubin Ren
parent 2ea226055e
commit 8933da1ec5
4 changed files with 93 additions and 2 deletions
+7 -1
View File
@@ -166,7 +166,9 @@ class SDKCaptureHook(AgentHook):
The runner mutates ``context.messages`` in place across iterations, so the
snapshot is refreshed on every ``after_iteration`` call; the last call
reflects the end-of-turn state the SDK caller cares about.
reflects the end-of-turn state the SDK caller cares about. The run-level
snapshot is authoritative when available and covers paths without a final
per-iteration callback.
"""
def __init__(self) -> None:
@@ -178,3 +180,7 @@ class SDKCaptureHook(AgentHook):
for call in context.tool_calls:
self.tools_used.append(call.name)
self.messages = list(context.messages)
async def after_run(self, context: AgentRunHookContext) -> None:
self.tools_used = list(context.tools_used)
self.messages = list(context.messages)