Skip to content

Commit cf0443a

Browse files
authored
update docs for new feature (#342)
* docs: update release notes for version 1.1.0 * docs: update Python SDK references for handling large files and long outputs
1 parent cf08a5d commit cf0443a

File tree

7 files changed

+442
-23
lines changed

7 files changed

+442
-23
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Error Codes
2+
3+
错误码定义和分类,用于错误处理和重试策略。
4+
5+
## 使用示例
6+
7+
```python
8+
import rock
9+
10+
def test_codes_values():
11+
"""测试基本状态码值"""
12+
assert rock.codes.OK == 2000
13+
assert rock.codes.BAD_REQUEST == 4000
14+
assert rock.codes.INTERNAL_SERVER_ERROR == 5000
15+
assert rock.codes.COMMAND_ERROR == 6000
16+
```
17+
18+
## Codes 分类
19+
20+
```python
21+
OK = 2000, "OK"
22+
"""
23+
成功状态码 (2xxx)
24+
"""
25+
26+
BAD_REQUEST = 4000, "Bad Request"
27+
"""
28+
客户端错误码 (4xxx):
29+
30+
这些错误表示客户端请求有问题,
31+
SDK 会抛出异常。
32+
"""
33+
34+
INTERNAL_SERVER_ERROR = 5000, "Internal Server Error"
35+
"""
36+
服务端错误码 (5xxx):
37+
38+
这些错误表示服务端出现问题,
39+
SDK 会抛出异常。
40+
"""
41+
42+
COMMAND_ERROR = 6000, "Command Error"
43+
"""
44+
命令/执行错误码 (6xxx):
45+
46+
这些错误与命令执行相关,由模型处理,
47+
SDK 不会抛出异常。
48+
"""
49+
```
50+
51+
## 重试策略建议
52+
53+
- **重试触发条件**: 只有当 `INTERNAL_SERVER_ERROR` 时才需要重试
54+
- **其他情况的处理策略**:
55+
- `BAD_REQUEST`: 需要检查 arun 调用逻辑是否有异常
56+
- `COMMAND_ERROR`: stdout 输出到 `observation.output`,stderr 输出到 `observation.failure_reason`
57+
- `COMMAND_ERROR` 说明: 由于 bash 执行失败时,stdout/stderr 可能全部非空,建议将 observation 中 output 和 failure_reason 全部 prompt 给模型进行推理
58+
59+
## 重试示例
60+
61+
```python
62+
# Background execution with nohup
63+
while retry_times < retry_limit:
64+
try:
65+
observation: Observation = await sandbox.arun(
66+
"python long_running_script.py",
67+
mode="nohup"
68+
)
69+
if observation.exit_code != 0:
70+
logging.warning(
71+
f"Command failed with exit code {observation.exit_code}, "
72+
f"output: {observation.output}, failure_reason: {observation.failure_reason}"
73+
)
74+
return observation
75+
except RockException as e:
76+
if rock.codes.is_server_error(e.code):
77+
if retry_times >= retry_limit:
78+
logging.error(f"All {retry_limit} attempts failed")
79+
raise e
80+
else:
81+
retry_times += 1
82+
logging.error(
83+
f"Server error occurred, code: {e.code}, message: {e.code.get_reason_phrase()}, "
84+
f"exception: {str(e)}, will retry, times: {retry_times}."
85+
)
86+
await asyncio.sleep(2)
87+
continue
88+
else:
89+
logging.error(
90+
f"Non-retriable error occurred, code: {e.code}, message: {e.code.get_reason_phrase()}, exception: {str(e)}."
91+
)
92+
raise e
93+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Remote User
2+
3+
远程用户管理,用于在沙箱中创建和管理用户。
4+
5+
## 使用示例
6+
7+
```python
8+
import asyncio
9+
from rock.sdk.sandbox.config import SandboxConfig
10+
from rock.sdk.sandbox.client import Sandbox
11+
12+
from rock.actions import Action, CreateBashSessionRequest, Observation
13+
14+
async def test_remote_user():
15+
config = SandboxConfig(
16+
image='hub.docker.alibaba-inc.com/chatos/python:3.11',
17+
xrl_authorization='xxx',
18+
cluster='nt-c'
19+
)
20+
sandbox = Sandbox(config)
21+
await sandbox.start()
22+
23+
await sandbox.remote_user.create_remote_user('rock')
24+
assert await sandbox.remote_user.is_user_exist('rock')
25+
print('test remote user success')
26+
27+
async def test_create_session_with_remote_user():
28+
config = SandboxConfig(
29+
image='hub.docker.alibaba-inc.com/chatos/python:3.11',
30+
xrl_authorization='xxx',
31+
cluster='nt-c'
32+
)
33+
sandbox = Sandbox(config)
34+
await sandbox.start()
35+
36+
await sandbox.remote_user.create_remote_user('rock')
37+
assert await sandbox.remote_user.is_user_exist('rock')
38+
39+
await sandbox.create_session(CreateBashSessionRequest(remote_user="rock", session="bash"))
40+
41+
observation: Observation = await sandbox.run_in_session(
42+
action=Action(session="bash", command="whoami")
43+
)
44+
print(observation)
45+
assert observation.output.strip() == "rock"
46+
print('test create session with remote user success')
47+
48+
if __name__ == '__main__':
49+
asyncio.run(test_remote_user())
50+
asyncio.run(test_create_session_with_remote_user())
51+
```
52+
53+
## API
54+
55+
### create_remote_user(username)
56+
57+
创建远程用户。
58+
59+
```python
60+
await sandbox.remote_user.create_remote_user('username')
61+
```
62+
63+
### is_user_exist(username)
64+
65+
检查用户是否存在。
66+
67+
```python
68+
exists = await sandbox.remote_user.is_user_exist('username')
69+
```

docs/i18n/zh-Hans/docusaurus-plugin-content-docs/version-1.1.x/References/Python SDK References/sandbox.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Sandbox SDK 参考
1+
# 处理大文件和长命令输出
22

33
## `arun`
4-
`arun()``nohup` 模式下提供了两个关键参数,帮助 Agent / 调用方在“执行”与“查看”之间按需解耦:
4+
`arun()``nohup` 模式下提供了两个关键参数,帮助 Agent / 调用方在"执行"与"查看"之间按需解耦:
55

6-
1. **`response_limited_bytes_in_nohup`**(int 型)
6+
1. **`response_limited_bytes_in_nohup`**(int 型)
77
限制返回内容的最大字符数(例如 `64 * 1024`),适合仍需立刻查看部分日志、但必须控制带宽的场景。默认值 `None` 表示不加限制。
88

9-
2. **`ignore_output`**(bool,默认 `False`
9+
2. **`ignore_output`**(bool,默认 `False`
1010
当设为 `True` 时,`arun()` 不再读取 nohup 输出文件,而是在命令执行完毕后立即返回一段提示信息(包含输出文件路径、**文件大小**及查看方式)。日志仍写入 `/tmp/tmp_<timestamp>.out`,后续可通过 `read_file`、下载接口或自定义命令按需读取,实现"执行"与"查看"彻底解耦。返回的文件大小信息可帮助用户决定是直接下载还是分块读取。
1111

1212
```python
@@ -52,11 +52,62 @@ print(resp_detached.output)
5252
```
5353

5454
## `read_file_by_line_range`
55-
功能说明: 按行范围异步读取文件内容,支持自动分块读取和会话管理。主要特性包括大文件自动分块读取、自动统计文件总行数、内置重试机制(3次重试)、参数验证。以下是使用示例:
55+
56+
按行范围异步读取文件内容,支持自动分块读取和会话管理,支持大文件读取。
57+
58+
### 重要特性
59+
- **大文件分块读取**: 自动将大文件分成多个小块进行读取
60+
- **自动统计行数**: 未指定结束行时,自动计算文件总行数
61+
- **内置重试机制**: 关键操作支持最多 3 次重试,提高可靠性
62+
- **参数验证**: 自动验证输入参数的合法性
63+
- **会话管理**: 支持指定会话或自动创建临时会话
64+
65+
### 参数说明
66+
| 参数 | 类型 | 默认值 | 说明 |
67+
|------|------|--------|------|
68+
| `file_path` | str | - | 要读取的文件路径(沙箱中的绝对路径或相对路径) |
69+
| `start_line` | int \| None | 1 | 起始行号(从 1 开始) |
70+
| `end_line` | int \| None | None | 结束行号(包含),默认为文件末尾 |
71+
| `lines_per_request` | int | 1000 | 每次请求读取的行数,范围 1-10000 |
72+
73+
### 返回值
74+
- `ReadFileResponse`: 包含文件内容的响应对象
75+
- `content` (str): 读取的文件内容
76+
77+
### 异常说明
78+
- `Exception`: 当 `start_line < 1` 时抛出
79+
- `Exception`: 当 `end_line < start_line` 时抛出
80+
- `Exception`: 当 `lines_per_request` 不在 1-10000 范围内时抛出
81+
- `Exception`: 当文件读取失败时抛出
82+
83+
### 使用示例
84+
5685
```python
5786
# 读取整个文件
58-
response = await read_file_by_line_range("example.txt")
87+
response = await sandbox.read_file_by_line_range("/path/to/file.txt")
88+
89+
# 读取指定行范围(第 100 到 500 行)
90+
response = await sandbox.read_file_by_line_range(
91+
"/path/to/file.txt",
92+
start_line=100,
93+
end_line=500
94+
)
95+
96+
# 从第 1990 行读取到文件末尾
97+
response = await sandbox.read_file_by_line_range(
98+
"/path/to/file.txt",
99+
start_line=1990
100+
)
101+
102+
# 使用自定义分块大小
103+
response = await sandbox.read_file_by_line_range(
104+
"/path/to/file.txt",
105+
lines_per_request=5000
106+
)
107+
```
59108

60-
# 读取指定行范围
61-
response = await read_file_by_line_range("example.txt", start_line=1, end_line=2000)
62-
```
109+
### 注意事项
110+
- 行号从 1 开始计数,而非 0
111+
- 对于大文件建议适当增加 `lines_per_request` 以提高效率
112+
- 文件路径必须是沙箱内的有效路径
113+
- 使用 `sed` 命令进行文件读取,确保沙箱镜像支持该命令
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Error Codes
2+
3+
Error code definitions and categories for error handling and retry strategies.
4+
5+
## Usage Example
6+
7+
```python
8+
import rock
9+
10+
def test_codes_values():
11+
"""Test basic status code values"""
12+
assert rock.codes.OK == 2000
13+
assert rock.codes.BAD_REQUEST == 4000
14+
assert rock.codes.INTERNAL_SERVER_ERROR == 5000
15+
assert rock.codes.COMMAND_ERROR == 6000
16+
```
17+
18+
## Codes Categories
19+
20+
```python
21+
OK = 2000, "OK"
22+
"""
23+
Success codes (2xxx)
24+
"""
25+
26+
BAD_REQUEST = 4000, "Bad Request"
27+
"""
28+
Client error codes (4xxx):
29+
30+
These errors indicate issues with the client request,
31+
SDK will raise Exceptions for these errors.
32+
"""
33+
34+
INTERNAL_SERVER_ERROR = 5000, "Internal Server Error"
35+
"""
36+
Server error codes (5xxx):
37+
38+
These errors indicate issues on the server side,
39+
SDK will raise Exceptions for these errors.
40+
"""
41+
42+
COMMAND_ERROR = 6000, "Command Error"
43+
"""
44+
Command/execution error codes (6xxx):
45+
46+
These errors are related to command execution and should be handled by the model,
47+
SDK will NOT raise Exceptions for these errors.
48+
"""
49+
```
50+
51+
## Retry Strategy Recommendations
52+
53+
- **Retry trigger**: Only retry when `INTERNAL_SERVER_ERROR` occurs
54+
- **Other error handling**:
55+
- `BAD_REQUEST`: Check if there are issues with the arun call logic
56+
- `COMMAND_ERROR`: stdout goes to `observation.output`, stderr goes to `observation.failure_reason`
57+
- `COMMAND_ERROR` note: When bash execution fails, both stdout and stderr may be non-empty. It is recommended to prompt the model with both output and failure_reason from the observation.
58+
59+
## Retry Example
60+
61+
```python
62+
# Background execution with nohup
63+
while retry_times < retry_limit:
64+
try:
65+
observation: Observation = await sandbox.arun(
66+
"python long_running_script.py",
67+
mode="nohup"
68+
)
69+
if observation.exit_code != 0:
70+
logging.warning(
71+
f"Command failed with exit code {observation.exit_code}, "
72+
f"output: {observation.output}, failure_reason: {observation.failure_reason}"
73+
)
74+
return observation
75+
except RockException as e:
76+
if rock.codes.is_server_error(e.code):
77+
if retry_times >= retry_limit:
78+
logging.error(f"All {retry_limit} attempts failed")
79+
raise e
80+
else:
81+
retry_times += 1
82+
logging.error(
83+
f"Server error occurred, code: {e.code}, message: {e.code.get_reason_phrase()}, "
84+
f"exception: {str(e)}, will retry, times: {retry_times}."
85+
)
86+
await asyncio.sleep(2)
87+
continue
88+
else:
89+
logging.error(
90+
f"Non-retriable error occurred, code: {e.code}, message: {e.code.get_reason_phrase()}, exception: {str(e)}."
91+
)
92+
raise e
93+
```

0 commit comments

Comments
 (0)