mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-06 15:52:11 +00:00
fix: use config metadata for sandbox state lookups
This commit is contained in:
parent
50e02c67ea
commit
616730fb1d
3 changed files with 16 additions and 27 deletions
|
|
@ -136,12 +136,7 @@ I've {action} pull request to address this issue:
|
||||||
commit_message = pr_payload.get("commit_message", pr_title)
|
commit_message = pr_payload.get("commit_message", pr_title)
|
||||||
|
|
||||||
if not thread_id:
|
if not thread_id:
|
||||||
if linear_issue_id and last_message_content:
|
raise ValueError("Missing thread_id in middleware config")
|
||||||
comment = f"""🤖 **Agent Response**
|
|
||||||
|
|
||||||
{last_message_content}"""
|
|
||||||
await comment_on_linear_issue(linear_issue_id, comment)
|
|
||||||
return None
|
|
||||||
|
|
||||||
repo_config = configurable.get("repo", {})
|
repo_config = configurable.get("repo", {})
|
||||||
repo_owner = repo_config.get("owner")
|
repo_owner = repo_config.get("owner")
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import warnings
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
from langgraph.config import get_config
|
||||||
from langgraph.graph.state import RunnableConfig
|
from langgraph.graph.state import RunnableConfig
|
||||||
from langgraph.pregel import Pregel
|
from langgraph.pregel import Pregel
|
||||||
from langgraph_sdk import get_client
|
from langgraph_sdk import get_client
|
||||||
|
|
@ -45,7 +46,7 @@ SANDBOX_CREATING = "__creating__"
|
||||||
SANDBOX_CREATION_TIMEOUT = 180
|
SANDBOX_CREATION_TIMEOUT = 180
|
||||||
SANDBOX_POLL_INTERVAL = 1.0
|
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
|
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
|
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:
|
async def _wait_for_sandbox_id(thread_id: str) -> str:
|
||||||
"""Wait for sandbox_id to be set in thread metadata.
|
"""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
|
elapsed = 0.0
|
||||||
while elapsed < SANDBOX_CREATION_TIMEOUT:
|
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:
|
if sandbox_id is not None and sandbox_id != SANDBOX_CREATING:
|
||||||
return sandbox_id
|
return sandbox_id
|
||||||
await asyncio.sleep(SANDBOX_POLL_INTERVAL)
|
await asyncio.sleep(SANDBOX_POLL_INTERVAL)
|
||||||
|
|
@ -224,7 +219,7 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
|
||||||
).with_config(config)
|
).with_config(config)
|
||||||
|
|
||||||
sandbox_backend = SANDBOX_BACKENDS.get(thread_id)
|
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:
|
if sandbox_id == SANDBOX_CREATING and not sandbox_backend:
|
||||||
logger.info("Sandbox creation in progress, waiting...")
|
logger.info("Sandbox creation in progress, waiting...")
|
||||||
|
|
@ -232,8 +227,8 @@ async def get_agent(config: RunnableConfig) -> Pregel: # noqa: PLR0915
|
||||||
|
|
||||||
if sandbox_backend:
|
if sandbox_backend:
|
||||||
logger.info("Using cached sandbox backend for thread %s", thread_id)
|
logger.info("Using cached sandbox backend for thread %s", thread_id)
|
||||||
thread = await client.threads.get(thread_id=thread_id)
|
metadata = get_config().get("metadata", {})
|
||||||
repo_dir = thread.get("metadata", {}).get("repo_dir")
|
repo_dir = metadata.get("repo_dir")
|
||||||
|
|
||||||
if repo_owner and repo_name:
|
if repo_owner and repo_name:
|
||||||
logger.info("Pulling latest changes for repo %s/%s", repo_owner, 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})
|
await client.threads.update(thread_id=thread_id, metadata={"sandbox_id": None})
|
||||||
raise
|
raise
|
||||||
|
|
||||||
thread = await client.threads.get(thread_id=thread_id)
|
metadata = get_config().get("metadata", {})
|
||||||
repo_dir = thread.get("metadata", {}).get("repo_dir")
|
repo_dir = metadata.get("repo_dir")
|
||||||
|
|
||||||
if repo_owner and repo_name:
|
if repo_owner and repo_name:
|
||||||
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)
|
logger.info("Pulling latest changes for repo %s/%s", repo_owner, repo_name)
|
||||||
|
|
|
||||||
|
|
@ -6,25 +6,24 @@ import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from langgraph_sdk import get_client
|
from langgraph.config import get_config
|
||||||
|
|
||||||
from ..integrations.langsmith import _create_langsmith_sandbox
|
from ..integrations.langsmith import _create_langsmith_sandbox
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
client = get_client()
|
|
||||||
|
|
||||||
# Thread ID -> SandboxBackend mapping, shared between server.py and middleware
|
# Thread ID -> SandboxBackend mapping, shared between server.py and middleware
|
||||||
SANDBOX_BACKENDS: dict[str, Any] = {}
|
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."""
|
"""Fetch sandbox_id from thread metadata."""
|
||||||
try:
|
try:
|
||||||
thread = await client.threads.get(thread_id=thread_id)
|
config = get_config()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to fetch thread metadata for sandbox")
|
logger.exception("Failed to read thread metadata for sandbox")
|
||||||
return None
|
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:
|
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:
|
if sandbox_backend:
|
||||||
return 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:
|
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_backend = await asyncio.to_thread(_create_langsmith_sandbox, sandbox_id)
|
||||||
SANDBOX_BACKENDS[thread_id] = sandbox_backend
|
SANDBOX_BACKENDS[thread_id] = sandbox_backend
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue