Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ai/agents/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from . import runtime as runtime_

if TYPE_CHECKING:
from collections.abc import AsyncIterator
from collections.abc import AsyncIterator, Callable


class HookRegistry:
Expand Down Expand Up @@ -141,6 +141,15 @@ def __init__(
super().__init__(hook.hook_id)
self.hook = hook

def __reduce__(
self,
) -> tuple[
Callable[..., HookDeferredException],
tuple[messages_.HookPart[Any]],
dict[str, Any],
]:
return type(self), (self.hook,), self.__dict__


def _label(target: str | messages_.HookPart[Any]) -> str:
"""Normalize a hook label or a HookPart down to its label string."""
Expand Down
18 changes: 18 additions & 0 deletions tests/agents/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import asyncio
import copy
import pickle
from collections.abc import AsyncGenerator
from typing import Any

Expand All @@ -20,6 +22,22 @@ class Confirmation(pydantic.BaseModel):
reason: str = ""


def test_hook_deferred_exception_survives_pickle_and_copy() -> None:
hook: ai.messages.HookPart[Any] = ai.messages.HookPart(
hook_id="approve_tc-1", hook_type="ToolApproval", status="pending"
)
exc = ai.HookDeferredException(hook)
exc.add_note("keep exception state")

restored = pickle.loads(pickle.dumps(exc))
assert restored.hook == hook
assert copy.copy(exc).hook == hook
assert copy.deepcopy(exc).hook == hook
assert restored.args == exc.args == (hook.hook_id,)
assert str(restored) == str(exc) == hook.hook_id
assert restored.__notes__ == ["keep exception state"]


# -- resolve_hook() with live future (long-running mode) -------------------


Expand Down
Loading