From 0c59811ee44c1dd34af423b08564fca42c601af1 Mon Sep 17 00:00:00 2001 From: aran-yogesh Date: Tue, 24 Feb 2026 16:41:33 -0800 Subject: [PATCH] feat: persist sandbox_id to thread metadata via client.threads.update --- apps/agent/agent/integrations/langsmith.py | 35 +++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/apps/agent/agent/integrations/langsmith.py b/apps/agent/agent/integrations/langsmith.py index 2c7c63d7..f84c7768 100644 --- a/apps/agent/agent/integrations/langsmith.py +++ b/apps/agent/agent/integrations/langsmith.py @@ -63,11 +63,44 @@ def create_langsmith_sandbox( template_name, template_image = _get_sandbox_template_config() provider = LangSmithProvider(api_key=api_key) - return provider.get_or_create( + backend = provider.get_or_create( sandbox_id=sandbox_id, template=template_name, template_image=template_image, ) + _update_thread_sandbox_metadata(backend.id) + return backend + + +def _update_thread_sandbox_metadata(sandbox_id: str) -> None: + """Update thread metadata with sandbox_id.""" + try: + import asyncio + + from langgraph.config import get_config + from langgraph_sdk import get_client + + config = get_config() + thread_id = config.get("configurable", {}).get("thread_id") + if not thread_id: + return + client = get_client() + + async def _update() -> None: + await client.threads.update( + thread_id=thread_id, + metadata={"sandbox_id": sandbox_id}, + ) + + try: + loop = asyncio.get_running_loop() + except RuntimeError: + asyncio.run(_update()) + else: + loop.create_task(_update()) + except Exception: + # Best-effort: ignore failures (no config context, client unavailable, etc.) + pass class SandboxProvider(ABC):