Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,102 @@ config = SandboxConfig(
)
```

### 2.4 沙箱加速配置

ROCK 提供沙箱网络加速功能,支持配置 APT、PIP 和 GitHub 镜像源,提升受限网络环境下的包下载速度。

#### 支持的加速类型

**APT 镜像配置**

配置 APT 包管理器镜像源,加速 Debian/Ubuntu 软件包下载。

```python
from rock.sdk.sandbox.speedup import SpeedupType

# 配置 APT 镜像
await sandbox.network.speedup(
speedup_type=SpeedupType.APT,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)
```

**PIP 镜像配置**

配置 Python 包索引镜像,加速 pip 安装。

```python
# HTTP 镜像
await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)

# HTTPS 镜像
await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="https://mirrors.aliyun.com"
)
```

**GitHub 加速**

通过添加自定义 DNS 解析条目加速 GitHub 访问。

```python
await sandbox.network.speedup(
speedup_type=SpeedupType.GITHUB,
speedup_value="11.11.11.11"
)
```

#### 完整示例

```python
from rock.sdk.sandbox.speedup import SpeedupType
from rock.actions import RunMode

async def setup_sandbox_with_speedup():
"""创建沙箱并配置加速"""
config = SandboxConfig(image="python:3.11")
sandbox = Sandbox(config)

await sandbox.start()

# 配置加速(在安装包之前配置)
await sandbox.network.speedup(
speedup_type=SpeedupType.APT,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)

await sandbox.arun(cmd="apt-get update && apt-get install -y git", mode=RunMode.NOHUP)

await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="https://mirrors.aliyun.com"
)

# speedup 不会主动安装 PIP,仅配置镜像源进行加速
await sandbox.arun(cmd="pip install numpy", mode=RunMode.NOHUP)

# 可以通过镜像 IP 加速 GitHub 访问
await sandbox.network.speedup(
speedup_type=SpeedupType.GITHUB,
speedup_value="11.11.11.11"
)

return sandbox
```

#### 注意事项

1. **配置顺序**: 在安装包之前配置加速
2. **HTTPS vs HTTP**: HTTPS 镜像不需要为 PIP 配置 trusted-host
3. **GitHub IP**: 不同区域可能需要不同的 IP 以获得最佳性能
4. **持久性**: 配置在沙箱生命周期内持久有效
5. **多次调用**: 后续的加速调用会覆盖之前的配置
6. **PIP 安装**: speedup 功能仅配置镜像源,不会自动安装 PIP

## 3. GEM SDK

### 3.1 Python SDK 方式
Expand Down Expand Up @@ -163,7 +259,6 @@ if __name__ == "__main__":
```

## 相关文档

- [快速开始指南](../../Getting%20Started/quickstart.md) - 了解如何快速开始使用 ROCK SDK
- [API 文档](../api.md) - 查看 SDK 封装的底层 API 接口
- [配置指南](../../User%20Guides/configuration.md) - 了解 SDK 相关的配置选项
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
- 简易的 Agent 安装接口
- 与Model-Service快速集成的能力
- 支持多种类型的Agent在隔离的沙箱环境中本地运行


#### 沙箱加速 (Sandbox Speedup)
**[沙箱加速参考文档](../References/Python SDK References/python_sdk.md)**

提供针对沙箱环境的网络优化与镜像配置能力:
- 支持一键配置 APT 公共镜像源
- 支持 PIP 镜像源加速(自动处理 HTTP/HTTPS 协议及信任主机配置)
- 通过 Hosts 映射实现 GitHub 访问加速
- 采用非侵入式配置,仅优化源地址而不强制执行安装
- 基于策略(Strategy)的 URL 自动解析与标准化逻辑
---

### 增强
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,102 @@ config = SandboxConfig(
)
```

### 2.4 Sandbox Speedup Configuration

ROCK provides sandbox network acceleration capabilities, supporting configuration of APT, PIP, and GitHub mirror sources to improve package download speeds in restricted network environments.

#### Supported Speedup Types

**APT Mirror Configuration**

Configure APT package manager mirror sources for faster Debian/Ubuntu package downloads.

```python
from rock.sdk.sandbox.speedup import SpeedupType

# Configure APT mirror
await sandbox.network.speedup(
speedup_type=SpeedupType.APT,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)
```

**PIP Mirror Configuration**

Configure Python package index mirrors for faster pip installations.

```python
# HTTP mirror
await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)

# HTTPS mirror
await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="https://mirrors.aliyun.com"
)
```

**GitHub Acceleration**

Configure GitHub IP acceleration by adding custom DNS resolution entries.

```python
await sandbox.network.speedup(
speedup_type=SpeedupType.GITHUB,
speedup_value="11.11.11.11"
)
```

#### Complete Example

```python
from rock.sdk.sandbox.speedup import SpeedupType
from rock.actions import RunMode

async def setup_sandbox_with_speedup():
"""Create sandbox and configure acceleration"""
config = SandboxConfig(image="python:3.11")
sandbox = Sandbox(config)

await sandbox.start()

# Configure acceleration (before installing packages)
await sandbox.network.speedup(
speedup_type=SpeedupType.APT,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)

await sandbox.arun(cmd="apt-get update && apt-get install -y git", mode=RunMode.NOHUP)

await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="https://mirrors.aliyun.com"
)

# Speedup does not automatically install PIP, it only configures mirror sources for acceleration
await sandbox.arun(cmd="pip install numpy", mode=RunMode.NOHUP)

# GitHub can be accelerated through mirror IP
await sandbox.network.speedup(
speedup_type=SpeedupType.GITHUB,
speedup_value="11.11.11.11"
)

return sandbox
```

#### Important Notes

1. **Configuration Order**: Configure speedup before installing packages
2. **HTTPS vs HTTP**: HTTPS mirrors don't require trusted-host configuration for PIP
3. **GitHub IP**: Different regions may require different IPs for optimal performance
4. **Persistence**: Configurations persist within the sandbox lifecycle
5. **Multiple Calls**: Subsequent speedup calls will override previous configurations
6. **PIP Installation**: The speedup feature only configures mirror sources and does not automatically install PIP

## 3. GEM SDK

### 3.1 Python SDK Approach
Expand Down Expand Up @@ -163,7 +259,6 @@ if __name__ == "__main__":
```

## Related Documents

- [Quick Start Guide](../../Getting%20Started/quickstart.md) - Learn how to quickly get started with the ROCK SDK
- [API Documentation](../api.md) - View the underlying API interfaces encapsulated by the SDK
- [Configuration Guide](../../User%20Guides/configuration.md) - Learn about SDK-related configuration options
Expand Down
10 changes: 10 additions & 0 deletions docs/versioned_docs/version-1.0.x/Release Notes/v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ Features for quickly installing Agents in ROCK's Sandbox:
- Quick integration capability with Model-Service
- Support for running multiple Agents in isolated sandbox environments

#### Sandbox Speedup
**[Sandbox Speedup References Documentation](../References/Python SDK References/python_sdk.md)**

Provides network optimization and mirror configuration capabilities for sandbox environments:
- Support for one-click APT public mirror configuration
- PIP mirror acceleration with automatic HTTP/HTTPS and trusted-host handling
- GitHub access acceleration via automated Hosts mapping
- Non-intrusive configuration focusing on source optimization without forced installation
- Strategy-based URL parsing and normalization logic

---

### Enhancements
Expand Down
6 changes: 6 additions & 0 deletions rock/sdk/sandbox/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from rock.sdk.sandbox.agent.base import Agent
from rock.sdk.sandbox.config import SandboxConfig, SandboxGroupConfig
from rock.sdk.sandbox.model_service.base import ModelService
from rock.sdk.sandbox.network import Network
from rock.sdk.sandbox.process import Process
from rock.sdk.sandbox.remote_user import LinuxRemoteUser, RemoteUser
from rock.utils import HttpUtils, extract_nohup_pid, retry_async

Expand All @@ -65,6 +67,8 @@ class Sandbox(AbstractSandbox):
agent: Agent | None = None
model_service: ModelService | None = None
remote_user: RemoteUser | None = None
process: Process | None = None
network: Network | None = None

def __init__(self, config: SandboxConfig):
self._pod_name = None
Expand All @@ -80,6 +84,8 @@ def __init__(self, config: SandboxConfig):
self._oss_token_expire_time = self._generate_utc_iso_time()
self._cluster = self.config.cluster
self.remote_user = LinuxRemoteUser(self)
self.process = Process(self)
self.network = Network(self)

@property
def sandbox_id(self) -> str:
Expand Down
60 changes: 60 additions & 0 deletions rock/sdk/sandbox/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from __future__ import annotations # Postpone annotation evaluation to avoid circular imports.

from typing import TYPE_CHECKING

from rock.actions import Observation
from rock.logger import init_logger
from rock.sdk.sandbox.speedup.executor import SpeedupExecutor
from rock.sdk.sandbox.speedup.types import SpeedupType

if TYPE_CHECKING:
from rock.sdk.sandbox.client import Sandbox

logger = init_logger(__name__)


class Network:
"""Network management for sandbox"""

def __init__(self, sandbox: Sandbox):
self.sandbox = sandbox
self._speedup_executor = SpeedupExecutor(sandbox)

async def speedup(self, speedup_type: SpeedupType, speedup_value: str, timeout: int = 300) -> Observation:
"""
Configure acceleration for package managers or network resources

Args:
speedup_type: Type of speedup configuration (SpeedupType.APT, SpeedupType.PIP, SpeedupType.GITHUB)
speedup_value: Speedup value, format depends on speedup_type:
- APT: Mirror URL with protocol
Examples: "http://mirrors.cloud.aliyuncs.com", "https://mirrors.aliyun.com"
- PIP: Mirror URL with protocol
Examples: "http://mirrors.cloud.aliyuncs.com", "https://mirrors.aliyun.com"
- GITHUB: IP address for github.com
Examples: "11.11.11.11"
timeout: Execution timeout in seconds, default 300

Returns:
Observation: Execution result containing output and exit code

Examples:
# Configure APT mirror
result = await sandbox.network.speedup(
speedup_type=SpeedupType.APT,
speedup_value="http://mirrors.cloud.aliyuncs.com"
)

# Configure PIP mirror with custom path
result = await sandbox.network.speedup(
speedup_type=SpeedupType.PIP,
speedup_value="https://mirrors.aliyun.com"
)

# Configure GitHub acceleration
result = await sandbox.network.speedup(
speedup_type=SpeedupType.GITHUB,
speedup_value="11.11.11.11"
)
"""
return await self._speedup_executor.execute(speedup_type, speedup_value, timeout)
Loading
Loading