Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/openshell/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tempfile
import threading
import time
import uuid
from collections import namedtuple
from dataclasses import dataclass, field
from enum import IntEnum
Expand Down Expand Up @@ -644,7 +645,9 @@ def exec(
stdin: bytes | None = None,
timeout_seconds: int | None = None,
no_login_shell: bool = False,
request_id: str | None = None,
) -> ExecResult:
extra = {} if request_id is None else {"request_id": request_id}
return self._client.exec(
self.sandbox.name,
command,
Expand All @@ -655,6 +658,7 @@ def exec(
stdin=stdin,
timeout_seconds=timeout_seconds,
no_login_shell=no_login_shell,
**extra,
)

def exec_python(
Expand Down Expand Up @@ -1329,10 +1333,21 @@ def exec_stream(
stdin: bytes | None = None,
timeout_seconds: int | None = None,
no_login_shell: bool = False,
request_id: str | None = None,
) -> Iterator[ExecChunk | ExecResult]:
if not command:
raise SandboxError("command must not be empty")

if request_id is not None:
try:
parsed_request_id = uuid.UUID(request_id)
except (TypeError, ValueError, AttributeError) as exc:
raise SandboxError(
"request_id must be a canonical nonzero UUID"
) from exc
if parsed_request_id.int == 0 or str(parsed_request_id) != request_id:
raise SandboxError("request_id must be a canonical nonzero UUID")

request = openshell_pb2.ExecSandboxRequest(
workspace_scope=_workspace_scope(workspace),
sandbox=sandbox,
Expand All @@ -1342,6 +1357,8 @@ def exec_stream(
stdin=stdin or b"",
no_login_shell=no_login_shell,
)
if request_id is not None:
request.request_id = request_id
if timeout_seconds:
request.execution_timeout.seconds = timeout_seconds
# Use whichever is larger: the default client timeout or the command
Expand Down Expand Up @@ -1434,6 +1451,7 @@ def exec(
stdin: bytes | None = None,
timeout_seconds: int | None = None,
no_login_shell: bool = False,
request_id: str | None = None,
) -> ExecResult:
result: ExecResult | None = None
for item in self.exec_stream(
Expand All @@ -1445,6 +1463,7 @@ def exec(
stdin=stdin,
timeout_seconds=timeout_seconds,
no_login_shell=no_login_shell,
request_id=request_id,
):
if stream_output and isinstance(item, ExecChunk):
if item.stream == "stdout":
Expand Down Expand Up @@ -1924,6 +1943,7 @@ def exec(
stdin: bytes | None = None,
timeout_seconds: int | None = None,
no_login_shell: bool = False,
request_id: str | None = None,
) -> ExecResult:
if self._session is None:
raise SandboxError("sandbox context has not been entered")
Expand All @@ -1935,6 +1955,7 @@ def exec(
stdin=stdin,
timeout_seconds=timeout_seconds,
no_login_shell=no_login_shell,
**({} if request_id is None else {"request_id": request_id}),
)

def exec_python(
Expand Down
69 changes: 69 additions & 0 deletions tests/sandbox_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pickle
import threading
import time
import uuid
from concurrent import futures
from copy import deepcopy
from dataclasses import asdict
Expand Down Expand Up @@ -560,6 +561,74 @@ def test_exec_sends_stdin_payload() -> None:
assert stub.request.stdin == b"payload"


def test_exec_forwards_owned_request_id_without_generating_one() -> None:
stub = _FakeStub()
client = _client_with_fake_stub(stub)
request_id = str(uuid.uuid4())

assert (
client.exec(
"sandbox-1", ["true"], workspace="default", request_id=request_id
).exit_code
== 0
)
assert stub.request is not None
assert stub.request.request_id == request_id

ref = SandboxRef(
id="sandbox-1",
name="sandbox-1",
workspace="default",
status=SandboxStatusRef(phase=2, current_policy_version=0),
)
session = sandbox_module.SandboxSession(client, ref)
assert session.exec(["true"], request_id=request_id).exit_code == 0
assert stub.request is not None
assert stub.request.request_id == request_id

context = sandbox_module.Sandbox(workspace="default", sandbox="sandbox-1")
context._session = session
assert context.exec(["true"], request_id=request_id).exit_code == 0
assert stub.request is not None
assert stub.request.request_id == request_id

assert client.exec("sandbox-1", ["true"], workspace="default").exit_code == 0
assert stub.request is not None
assert stub.request.request_id == ""


def test_exec_lost_stream_does_not_retry_with_a_new_request_id() -> None:
class LostStreamStub(_FakeStub):
calls = 0

def ExecSandbox(self, request, timeout=None):
_ = timeout
self.calls += 1
self.request = request
raise grpc.RpcError("stream lost")

stub = LostStreamStub()
client = _client_with_fake_stub(stub)
request_id = str(uuid.uuid4())

with pytest.raises(grpc.RpcError, match="stream lost"):
client.exec("sandbox-1", ["true"], workspace="default", request_id=request_id)
assert stub.calls == 1
assert stub.request is not None
assert stub.request.request_id == request_id


@pytest.mark.parametrize(
"request_id", ["", "not-a-uuid", str(uuid.UUID(int=0)), "A" * 36]
)
def test_exec_rejects_invalid_request_id_before_rpc(request_id: str) -> None:
stub = _FakeStub()
client = _client_with_fake_stub(stub)
with pytest.raises(SandboxError, match="canonical nonzero UUID"):
client.exec("sandbox-1", ["true"], workspace="default", request_id=request_id)
assert stub.request is None


def test_exec_python_serializes_callable_payload() -> None:
stub = _FakeStub()
client = _client_with_fake_stub(stub)
Expand Down