Python
OpenShell 0.1.2: pin the version and name the sandbox and workspace in the runner stream
Rohit C Prasad0571ee4ac645
diff --git a/coworker/sandbox/providers/openshell.py b/coworker/sandbox/providers/openshell.pyindex d8f49376..51dcc525 100644--- a/coworker/sandbox/providers/openshell.py+++ b/coworker/sandbox/providers/openshell.py@@ -45,7 +45,7 @@ from . import openshell_wire as wirelog = logging.getLogger(__name__)-PINNED_VERSION = "0.0.116" # ruling 24: one pinned release until NVIDIA ships GA+PINNED_VERSION = "0.1.2" # one pinned release (ruling 24). 0.0.x and 0.1.x cannot talk to each otherDEFAULT_IMAGE = "ghcr.io/nvidia/openshell-community/sandboxes/base@sha256:aeef1c63f00e2913ea002ccb3aaf925f338b5c5d70e63576f0d95c16a138044e"LABEL = "openworker"_SOCKET = f"{policy.RUNTIME_DIR}/owr/r.sock"@@ -401,7 +401,7 @@ class OpenShellProvider:if not self.sandbox_id:raise RuntimeError("the sandbox has not been created")command = [policy.PYTHON, "-S", f"{policy.RUNNER_MOUNT}/{self._runner.name}", "attach", "--socket", _SOCKET]- return GrpcExecTransport(self.sandbox_id, command)+ return GrpcExecTransport(self.sandbox_name, command)def verify(self, client: Any) -> None:"""After connecting: every folder must really be reachable inside (spike finding F:@@ -505,7 +505,7 @@ def list_our_sandboxes(registry: str) -> list[dict[str, Any]]:class GrpcExecTransport:"""The runner's pipe: OpenShell's `ExecSandboxInteractive` stream, no terminal."""- def __init__(self, sandbox_id: str, command: list[str]) -> None:+ def __init__(self, sandbox: str, command: list[str]) -> None:try:import grpcexcept ImportError as exc: # an optional extra, needed for this provider only@@ -524,7 +524,9 @@ class GrpcExecTransport:self._buffer = b""self._closed = Falsecall = self._channel.stream_stream(wire.METHOD, request_serializer=lambda b: b, response_deserializer=lambda b: b)- self._call = call(self._requests(wire.encode_start(sandbox_id, command)))+ # The same workspace the CLI calls above used (its `--workspace`, from the environment).+ workspace = os.environ.get("OPENSHELL_WORKSPACE", "").strip() or wire.DEFAULT_WORKSPACE+ self._call = call(self._requests(wire.encode_start(sandbox, command, workspace)))threading.Thread(target=self._pump, daemon=True).start()def _requests(self, start: bytes):diff --git a/coworker/sandbox/providers/openshell_wire.py b/coworker/sandbox/providers/openshell_wire.pyindex 42d4786f..14aea808 100644--- a/coworker/sandbox/providers/openshell_wire.py+++ b/coworker/sandbox/providers/openshell_wire.py@@ -3,11 +3,14 @@Only this one streaming call goes over gRPC (the CLI cannot stream input without aterminal; spike finding B). Its messages are tiny, so they are encoded and decoded herewith the protobuf wire format directly, which keeps generated code and the protobuf-runtime out of OpenWorker. Field numbers are those of OpenShell 0.0.116 (`proto/-openshell.proto`); the provider refuses any other version.+runtime out of OpenWorker. Field numbers are those of OpenShell 0.1.2 (`proto/+openshell.proto`); the provider refuses any other version. Since 0.1.0 the request names+the sandbox (it carried an internal id before) and must say which workspace it is in.message ExecSandboxInput { oneof payload { ExecSandboxRequest start = 1; bytes stdin = 2; } }- message ExecSandboxRequest { string sandbox_id = 1; repeated string command = 2; ... bool tty = 7; }+ message ExecSandboxRequest { string sandbox = 1; repeated string command = 2; ... bool tty = 7;+ bool no_login_shell = 10; WorkspaceSelector workspace_scope = 12; }+ message WorkspaceSelector { oneof selection { string workspace = 1; ... } }message ExecSandboxEvent { oneof payload { Stdout stdout = 1; Stderr stderr = 2; Exit exit = 3; } }message ExecSandboxStdout { bytes data = 1; } (Stderr alike)message ExecSandboxExit { int32 exit_code = 1; }@@ -37,11 +40,20 @@ def _field(number: int, payload: bytes) -> bytes:return _varint((number << 3) | _LEN) + _varint(len(payload)) + payload-def encode_start(sandbox_id: str, command: list[str]) -> bytes:- request = _field(1, sandbox_id.encode("utf-8"))+DEFAULT_WORKSPACE = "default"+++def encode_start(sandbox: str, command: list[str], workspace: str = DEFAULT_WORKSPACE) -> bytes:+ """`sandbox`: the sandbox's name. `workspace`: the OpenShell workspace it is in; an+ omitted one no longer means the default, so it is always sent."""+ request = _field(1, sandbox.encode("utf-8"))for part in command:request += _field(2, part.encode("utf-8"))# tty (field 7) is left at its default, false: no terminal on the stream.+ # no_login_shell (field 10) = true: the command is run as given. The default wraps it+ # in a login shell, whose startup files could print into the runner's stream.+ request += _varint((10 << 3) | _VARINT) + _varint(1)+ request += _field(12, _field(1, (workspace or DEFAULT_WORKSPACE).encode("utf-8")))return _field(1, request)diff --git a/tests/test_sandbox_openshell.py b/tests/test_sandbox_openshell.pyindex 04dd232c..a8143f79 100644--- a/tests/test_sandbox_openshell.py+++ b/tests/test_sandbox_openshell.py@@ -63,8 +63,15 @@ def test_folders_are_mounted_at_the_same_absolute_path():def test_stream_messages_match_the_protobuf_wire_format():- # ExecSandboxInput{start: ExecSandboxRequest{sandbox_id:"abc", command:["bash","-c"]}}- assert wire.encode_start("abc", ["bash", "-c"]).hex() == "0a0f0a0361626312046261736812022d63"+ # ExecSandboxInput{start: ExecSandboxRequest{sandbox:"abc", command:["bash","-c"],+ # no_login_shell: true, workspace_scope: {workspace: "default"}}} (OpenShell 0.1.2)+ start = wire.encode_start("abc", ["bash", "-c"])+ assert start.hex() == "0a1c" + "0a03616263" + "120462617368" + "12022d63" + "5001" + "62090a0764656661756c74"+ (number, _kind, request), = list(wire._fields(start))+ fields = [(n, v) for n, _k, v in wire._fields(request)]+ assert number == 1 and fields[0] == (1, b"abc") and (10, 1) in fields+ assert [(n, v) for n, _k, v in wire._fields(dict(fields)[12])] == [(1, b"default")]+ assert b"team-a" in wire.encode_start("abc", ["true"], "team-a")assert wire.encode_stdin(b"hi\n").hex() == "1203" + b"hi\n".hex()assert wire.decode_event(bytes.fromhex("0a070a0568656c6c6f")) == ("stdout", b"hello", None)assert wire.decode_event(bytes.fromhex("12050a03657272")) == ("stderr", b"err", None)