fix: use config metadata for sandbox state lookups

This commit is contained in:
aran-yogesh 2026-02-17 16:50:08 -08:00
parent 50e02c67ea
commit 616730fb1d
3 changed files with 16 additions and 27 deletions

View file

@ -136,12 +136,7 @@ I've {action} pull request to address this issue:
commit_message = pr_payload.get("commit_message", pr_title)
if not thread_id:
if linear_issue_id and last_message_content:
comment = f"""🤖 **Agent Response**
{last_message_content}"""
await comment_on_linear_issue(linear_issue_id, comment)
return None
raise ValueError("Missing thread_id in middleware config")
repo_config = configurable.get("repo", {})
repo_owner = repo_config.get("owner")

View file

@ -9,6 +9,7 @@ import warnings
logger = logging.getLogger(__name__)
from langgraph.config import get_config
from langgraph.graph.state import RunnableConfig
from langgraph.pregel import Pregel
from langgraph_sdk import get_client
@ -45,7 +46,7 @@ SANDBOX_CREATING = "__creating__"
SANDBOX_CREATION_TIMEOUT = 180
SANDBOX_POLL_INTERVAL = 1.0
from .utils.sandbox_state import SANDBOX_BACKENDS
from .utils.sandbox_state import SANDBOX_BACKENDS, get_sandbox_id_from_metadata
async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
@ -161,12 +162,6 @@ async def _clone_or_pull_repo_in_sandbox( # noqa: PLR0915
return repo_dir
async def _get_sandbox_id_from_metadata(thread_id: str) -> str | None:
"""Get sandbox_id from thread metadata."""
thread = await client.threads.get(thread_id=thread_id)
return thread.get("metadata", {}).get("sandbox_id")
async def _wait_for_sandbox_id(thread_id: str) -> str:
"""Wait for sandbox_id to be set in thread metadata.
@ -178,7 +173,7 @@ async def _wait_for_sandbox_id(thread_id: str) -> str:
"""
elapsed = 0.0
while elapsed < SANDBOX_CREATION_TIMEOUT:
sandbox_id = await _get_sandbox_id_from_metadata(thread_id)
sandbox_id = await get_sandbox_id_from_metadata(thread_id)
if sandbox_id is not None and sandbox_id != SANDBOX_CREATING:
return sandbox_id
await asyncio.sleep(SANDBOX_POLL_INTERVAL)
@ -224,7 +219,7 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
).with_config(config)
sandbox_backend = SANDBOX_BACKENDS.get(thread_id)
sandbox_id = await _get_sandbox_id_from_metadata(thread_id)
sandbox_id = await get_sandbox_id_from_metadata(thread_id)
if sandbox_id == SANDBOX_CREATING and not sandbox_backend:
logger.info("Sandbox creation in progress, waiting...")
@ -232,8 +227,8 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
if sandbox_backend:
logger.info("Using cached sandbox backend for thread %s", thread_id)
thread = await client.threads.get(thread_id=thread_id)
repo_dir = thread.get("metadata", {}).get("repo_dir")
metadata = get_config().get("metadata", {})
repo_dir = metadata.get("repo_dir")
if repo_owner and repo_name:
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)
@ -308,8 +303,8 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
await client.threads.update(thread_id=thread_id, metadata={"sandbox_id": None})
raise
thread = await client.threads.get(thread_id=thread_id)
repo_dir = thread.get("metadata", {}).get("repo_dir")
metadata = get_config().get("metadata", {})
repo_dir = metadata.get("repo_dir")
if repo_owner and repo_name:
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)

View file

@ -6,25 +6,24 @@ import asyncio
import logging
from typing import Any
from langgraph_sdk import get_client
from langgraph.config import get_config
from ..integrations.langsmith import _create_langsmith_sandbox
logger = logging.getLogger(__name__)
client = get_client()
# Thread ID -> SandboxBackend mapping, shared between server.py and middleware
SANDBOX_BACKENDS: dict[str, Any] = {}
async def _get_sandbox_id_from_metadata(thread_id: str) -> str | None:
async def get_sandbox_id_from_metadata(thread_id: str) -> str | None:
"""Fetch sandbox_id from thread metadata."""
try:
thread = await client.threads.get(thread_id=thread_id)
config = get_config()
except Exception:
logger.exception("Failed to fetch thread metadata for sandbox")
logger.exception("Failed to read thread metadata for sandbox")
return None
return thread.get("metadata", {}).get("sandbox_id")
return config.get("metadata", {}).get("sandbox_id")
async def get_sandbox_backend(thread_id: str) -> Any | None:
@ -33,9 +32,9 @@ async def get_sandbox_backend(thread_id: str) -> Any | None:
if sandbox_backend:
return sandbox_backend
sandbox_id = await _get_sandbox_id_from_metadata(thread_id)
sandbox_id = await get_sandbox_id_from_metadata(thread_id)
if not sandbox_id:
return None
raise ValueError(f"Missing sandbox_id in thread metadata for {thread_id}")
sandbox_backend = await asyncio.to_thread(_create_langsmith_sandbox, sandbox_id)
SANDBOX_BACKENDS[thread_id] = sandbox_backend