mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-01 09:43:14 +00:00
Add SANDBOX_CREATE_EXTRA_JSON so operators can merge extra fields (e.g.
{"_internal_runtime":"v2"}) into the LangSmith sandbox-create request body.
The SDK's create_sandbox builds a fixed payload with no passthrough, so we
wrap the HTTP client's post to inject the fields on the POST /boxes request
only. Malformed JSON fails at startup validation.
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
(cherry picked from commit 2238303306493ae6fcd0c2d4ab4236adf283a896)
Co-authored-by: Ramon Nogueira <ramon.nogueira@langchain.dev>
165 lines
5.1 KiB
Python
165 lines
5.1 KiB
Python
"""Tests for LangSmith sandbox env-var configuration parsing."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from agent.integrations.langsmith import (
|
|
DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS,
|
|
DEFAULT_SANDBOX_IDLE_TTL_SECONDS,
|
|
DEFAULT_SANDBOX_MEM_BYTES,
|
|
DEFAULT_SANDBOX_VCPUS,
|
|
DEFAULT_SNAPSHOT_FS_CAPACITY_BYTES,
|
|
LangSmithProvider,
|
|
_get_sandbox_create_extra_fields,
|
|
_get_sandbox_snapshot_config,
|
|
_install_create_extra_fields,
|
|
)
|
|
|
|
|
|
def test_defaults_when_env_unset() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-1"},
|
|
clear=True,
|
|
):
|
|
snapshot_id, fs, vcpus, mem, idle, delete_after = _get_sandbox_snapshot_config()
|
|
assert snapshot_id == "snap-1"
|
|
assert fs == DEFAULT_SNAPSHOT_FS_CAPACITY_BYTES
|
|
assert vcpus == DEFAULT_SANDBOX_VCPUS
|
|
assert mem == DEFAULT_SANDBOX_MEM_BYTES
|
|
assert idle == DEFAULT_SANDBOX_IDLE_TTL_SECONDS
|
|
assert delete_after == DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS
|
|
|
|
|
|
def test_overrides_from_env() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-2",
|
|
"DEFAULT_SANDBOX_IDLE_TTL_SECONDS": "120",
|
|
"DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS": "3600",
|
|
},
|
|
clear=True,
|
|
):
|
|
_, _, _, _, idle, delete_after = _get_sandbox_snapshot_config()
|
|
assert idle == 120
|
|
assert delete_after == 3600
|
|
|
|
|
|
def test_zero_disables_ttls() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-3",
|
|
"DEFAULT_SANDBOX_IDLE_TTL_SECONDS": "0",
|
|
"DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS": "0",
|
|
},
|
|
clear=True,
|
|
):
|
|
_, _, _, _, idle, delete_after = _get_sandbox_snapshot_config()
|
|
assert idle == 0
|
|
assert delete_after == 0
|
|
|
|
|
|
def test_validate_startup_rejects_non_integer_ttl() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-4",
|
|
"DEFAULT_SANDBOX_IDLE_TTL_SECONDS": "not-a-number",
|
|
},
|
|
clear=True,
|
|
):
|
|
with pytest.raises(ValueError, match="DEFAULT_SANDBOX_IDLE_TTL_SECONDS"):
|
|
LangSmithProvider.validate_startup_config()
|
|
|
|
|
|
def test_validate_startup_rejects_negative_ttl() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-5",
|
|
"DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS": "-1",
|
|
},
|
|
clear=True,
|
|
):
|
|
with pytest.raises(ValueError, match=">= 0"):
|
|
LangSmithProvider.validate_startup_config()
|
|
|
|
|
|
def test_validate_startup_accepts_valid_config() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DEFAULT_SANDBOX_SNAPSHOT_ID": "snap-6",
|
|
"DEFAULT_SANDBOX_IDLE_TTL_SECONDS": "1800",
|
|
"DEFAULT_SANDBOX_DELETE_AFTER_STOP_SECONDS": "86400",
|
|
},
|
|
clear=True,
|
|
):
|
|
LangSmithProvider.validate_startup_config()
|
|
|
|
|
|
def test_extra_fields_unset_is_empty() -> None:
|
|
with patch.dict("os.environ", {}, clear=True):
|
|
assert _get_sandbox_create_extra_fields() == {}
|
|
with patch.dict("os.environ", {"SANDBOX_CREATE_EXTRA_JSON": " "}, clear=True):
|
|
assert _get_sandbox_create_extra_fields() == {}
|
|
|
|
|
|
def test_extra_fields_parsed() -> None:
|
|
with patch.dict(
|
|
"os.environ",
|
|
{"SANDBOX_CREATE_EXTRA_JSON": '{"_internal_runtime": "v2"}'},
|
|
clear=True,
|
|
):
|
|
assert _get_sandbox_create_extra_fields() == {"_internal_runtime": "v2"}
|
|
|
|
|
|
def test_extra_fields_rejects_invalid_json() -> None:
|
|
with patch.dict("os.environ", {"SANDBOX_CREATE_EXTRA_JSON": "{not json"}, clear=True):
|
|
with pytest.raises(ValueError, match="valid JSON"):
|
|
_get_sandbox_create_extra_fields()
|
|
|
|
|
|
def test_extra_fields_rejects_non_object() -> None:
|
|
with patch.dict("os.environ", {"SANDBOX_CREATE_EXTRA_JSON": "[1, 2]"}, clear=True):
|
|
with pytest.raises(ValueError, match="JSON object"):
|
|
_get_sandbox_create_extra_fields()
|
|
|
|
|
|
def test_install_create_extra_fields_merges_only_boxes_post() -> None:
|
|
calls: list[tuple[str, dict]] = []
|
|
|
|
class _FakeHttp:
|
|
def post(self, url, **kwargs): # noqa: ANN001, ANN003
|
|
calls.append((url, kwargs.get("json")))
|
|
return "ok"
|
|
|
|
class _FakeClient:
|
|
def __init__(self) -> None:
|
|
self._http = _FakeHttp()
|
|
|
|
client = _FakeClient()
|
|
_install_create_extra_fields(client, {"_internal_runtime": "v2"})
|
|
|
|
client._http.post("https://api/v2/sandboxes/boxes", json={"snapshot_id": "s"})
|
|
client._http.post("https://api/v2/sandboxes/boxes/abc/start", json={"foo": "bar"})
|
|
|
|
assert calls[0][1] == {"snapshot_id": "s", "_internal_runtime": "v2"}
|
|
assert calls[1][1] == {"foo": "bar"}
|
|
|
|
|
|
def test_install_create_extra_fields_noop_when_empty() -> None:
|
|
class _FakeHttp:
|
|
def __init__(self) -> None:
|
|
self.post = "sentinel"
|
|
|
|
class _FakeClient:
|
|
def __init__(self) -> None:
|
|
self._http = _FakeHttp()
|
|
|
|
client = _FakeClient()
|
|
_install_create_extra_fields(client, {})
|
|
assert client._http.post == "sentinel"
|