|
3 | 3 | import os
|
4 | 4 | import tempfile
|
5 | 5 | from collections.abc import Callable
|
| 6 | +from copy import deepcopy |
6 | 7 | from dataclasses import dataclass
|
7 | 8 | from pathlib import Path
|
8 | 9 | from typing import Union
|
@@ -361,6 +362,50 @@ async def test_kernel_invoke_deep_copy_preserves_previous_state():
|
361 | 362 | assert snapshot2[-1] == {"id": 4, "name": "Desk lamp", "is_on": True}
|
362 | 363 |
|
363 | 364 |
|
| 365 | +# region Clone safety with unpickleable async context |
| 366 | + |
| 367 | + |
| 368 | +class _AsyncGenPlugin: |
| 369 | + """Plugin holding an async generator to emulate MCP-style async internals. |
| 370 | +
|
| 371 | + Deep-copying objects that reference async generators typically fails with |
| 372 | + `TypeError: cannot pickle 'async_generator' object`. |
| 373 | + """ |
| 374 | + |
| 375 | + def __init__(self): |
| 376 | + async def _agen(): |
| 377 | + yield "tick" |
| 378 | + |
| 379 | + # Store an async generator object on the instance to make it unpickleable. |
| 380 | + self._unpickleable_async_gen = _agen() |
| 381 | + |
| 382 | + @kernel_function(name="do", description="Return OK to validate plugin wiring") |
| 383 | + async def do(self) -> str: |
| 384 | + return "ok" |
| 385 | + |
| 386 | + |
| 387 | +@pytest.mark.asyncio |
| 388 | +async def test_kernel_clone_with_unpickleable_plugin_does_not_raise(): |
| 389 | + kernel = Kernel() |
| 390 | + plugin_instance = _AsyncGenPlugin() |
| 391 | + kernel.add_plugin(plugin_instance) |
| 392 | + |
| 393 | + # Sanity: naive deepcopy of plugins should raise due to async generator state |
| 394 | + with pytest.raises(TypeError): |
| 395 | + deepcopy(kernel.plugins) |
| 396 | + |
| 397 | + # Clone should succeed and preserve function usability |
| 398 | + cloned = kernel.clone() |
| 399 | + |
| 400 | + func = cloned.get_function(plugin_instance.__class__.__name__, "do") |
| 401 | + result = await func.invoke(cloned) |
| 402 | + assert result is not None |
| 403 | + assert result.value == "ok" |
| 404 | + |
| 405 | + |
| 406 | +# endregion |
| 407 | + |
| 408 | + |
364 | 409 | async def test_invoke_function_call_throws_during_invoke(kernel: Kernel, get_tool_call_mock):
|
365 | 410 | tool_call_mock = get_tool_call_mock
|
366 | 411 | result_mock = MagicMock(spec=ChatMessageContent)
|
|
0 commit comments