Recover transient sandbox connections before executing tools

This commit is contained in:
WebUp Cloud committed 2026-11-07 19:07:16 +08:00
1 parent 4f2cf2fc87
commit cb896bb0fa
4 files changed
+156 -52

No files matched your search

+29
View File
@@ -86,6 +86,35 @@ class GatewayTests(unittest.TestCase):
self.assertEqual([], policy["ingress"])
self.assertIn("169.254.0.0/16", policy["egress"][1]["to"][0]["ipBlock"]["except"])
def test_transient_exec_handshake_recovers_before_tool_input(self):
project = "00000000-0000-0000-0000-000000000002"
request = gateway.Tool(name="write_file", arguments={"path": "index.html", "content": "test"})
connection = MagicMock()
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", side_effect=[gateway.ApiException(status=503), connection]) as stream,
patch.object(gateway.time, "sleep"),
patch.object(gateway, "drain_exec_output", return_value='{"written":4}')):
self.assertEqual({"written": 4}, gateway.execute_tool(project, request, "Bearer " + "x" * 32, False))
self.assertEqual(2, stream.call_count)
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
def test_interrupted_tool_input_is_not_replayed(self):
project = "00000000-0000-0000-0000-000000000003"
request = gateway.Tool(name="run_command", arguments={"command": "npm install"})
connection = MagicMock()
connection.write_stdin.side_effect = gateway.WebSocketException("disconnected")
with (patch.object(gateway, "TOKEN", "x" * 32),
patch.object(gateway, "ensure_workspace"),
patch.object(gateway, "stream", return_value=connection) as stream):
with self.assertRaises(gateway.HTTPException) as error:
gateway.execute_tool(project, request, "Bearer " + "x" * 32, False)
self.assertEqual((502, "WORKSPACE_RESULT_UNKNOWN"), (error.exception.status_code, error.exception.detail))
stream.assert_called_once()
connection.write_stdin.assert_called_once()
connection.close.assert_called_once()
if __name__ == "__main__":
unittest.main()