Merge pull request #977 from langchain-ai/yogesh/sandbox-id-persist-metadata

feat: persist sandbox_id to thread metadata
This commit is contained in:
Aran Yogesh 2026-02-25 10:15:28 -08:00 • committed by GitHub
commit 84ae60ad49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 13 deletions

View file

@ -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):

View file

@ -255,13 +255,6 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
sandbox_backend = await asyncio.to_thread(create_langsmith_sandbox)
logger.info("Sandbox created: %s", sandbox_backend.id)
# Update metadata immediately after sandbox creation so other callers
# can connect to the sandbox while we clone the repo
await client.threads.update(
thread_id=thread_id,
metadata={"sandbox_id": sandbox_backend.id},
)
repo_dir = None
if repo_owner and repo_name:
logger.info("Cloning repo %s/%s into sandbox", repo_owner, repo_name)
@ -299,11 +292,6 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
try:
sandbox_backend = await asyncio.to_thread(create_langsmith_sandbox)
logger.info("New sandbox created: %s", sandbox_backend.id)
await client.threads.update(
thread_id=thread_id,
metadata={"sandbox_id": sandbox_backend.id},
)
except Exception:
logger.exception("Failed to create replacement sandbox")
await client.threads.update(thread_id=thread_id, metadata={"sandbox_id": None})