Skip to content

Build and run agents you can see, understand and trust.

fix(workspace): report an unset OpenSandbox exit code as a failure (#3062)

lihongyuan997b9cb38c7be8
diff --git a/src/agentscope/workspace/_opensandbox/_opensandbox_backend.py b/src/agentscope/workspace/_opensandbox/_opensandbox_backend.py
index fb3f6bed1..5289c9bff 100644
--- a/src/agentscope/workspace/_opensandbox/_opensandbox_backend.py
+++ b/src/agentscope/workspace/_opensandbox/_opensandbox_backend.py
@@ -90,14 +90,17 @@ class OpenSandboxBackend(BackendBase):
`ExecResult`:
The captured exit code, stdout, and stderr. A non-zero
command exit is returned as a normal result; transport
- errors yield an ``exit_code`` of ``-1``.
+ errors, and an exit code the provider leaves unset, yield
+ an ``exit_code`` of ``-1``.
"""
command_line = " ".join(shlex.quote(arg) for arg in command)
opts = self._make_run_opts(cwd=cwd or self._workdir, timeout=timeout)
try:
res = await self._sandbox.commands.run(command_line, opts=opts)
+ # The SDK leaves ``exit_code`` unset when the run's outcome is
+ # unknown (e.g. killed); report it as -1 rather than success.
return ExecResult(
- exit_code=int(getattr(res, "exit_code", 0) or 0),
+ exit_code=-1 if res.exit_code is None else res.exit_code,
stdout=self._execution_stream_bytes(res, "stdout"),
stderr=self._execution_stream_bytes(res, "stderr"),
)
diff --git a/tests/backend_opensandbox_test.py b/tests/backend_opensandbox_test.py
index 1be34dfbc..6f655325a 100644
--- a/tests/backend_opensandbox_test.py
+++ b/tests/backend_opensandbox_test.py
@@ -19,7 +19,9 @@ which avoids duplicating the sandbox bring-up logic here.
import os
import unittest
+from types import SimpleNamespace
from unittest.async_case import IsolatedAsyncioTestCase
+from unittest.mock import AsyncMock
from agentscope.tool import ExecResult
from agentscope.workspace import OpenSandboxWorkspace
@@ -153,3 +155,22 @@ class TestOpenSandboxBackend(IsolatedAsyncioTestCase):
# Deleting a non-existent path must not raise.
await self.backend.delete_path(f"{SANDBOX_WORKDIR}/missing")
+
+
+class TestOpenSandboxBackendExitCode(IsolatedAsyncioTestCase):
+ """Exit-code mapping, without a live sandbox."""
+
+ async def test_unset_exit_code_is_not_success(self) -> None:
+ """An execution the SDK leaves without an exit code reports -1."""
+ execution = SimpleNamespace(exit_code=None, stdout=b"", stderr=b"")
+ sandbox = SimpleNamespace(
+ commands=SimpleNamespace(run=AsyncMock(return_value=execution)),
+ )
+ backend = OpenSandboxBackend(sandbox, workdir="/workspace")
+
+ result = await backend.exec_shell(["python", "-c", "pass"])
+
+ self.assertEqual(
+ result,
+ ExecResult(exit_code=-1, stdout=b"", stderr=b""),
+ )