| 256 | ) |
| 257 | |
| 258 | async def cleanup(self) -> dict[str, object] | None: |
| 259 | should_trace_cleanup = self.current_session is not None or bool(self._prepared_sessions) |
| 260 | span_cm = ( |
| 261 | custom_span("sandbox.cleanup", data={}) |
| 262 | if should_trace_cleanup and _supports_trace_spans() |
| 263 | else nullcontext(None) |
| 264 | ) |
| 265 | with span_cm: |
| 266 | try: |
| 267 | return await self._session_manager.cleanup() |
| 268 | finally: |
| 269 | self._prepared_agents.clear() |
| 270 | self._prepared_sessions.clear() |
| 271 | |
| 272 | |
| 273 | def _get_memory_capability(agent: Agent[TContext]) -> Memory | None: |