genesis-sandbox-client-python 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- genesis_sandbox_client_python-0.1.0/LICENSE +201 -0
- genesis_sandbox_client_python-0.1.0/PKG-INFO +197 -0
- genesis_sandbox_client_python-0.1.0/README.md +174 -0
- genesis_sandbox_client_python-0.1.0/pyproject.toml +66 -0
- genesis_sandbox_client_python-0.1.0/pyproject.toml.orig +52 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/__init__.py +57 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/async_session.py +663 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/client.py +993 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/control_probe.py +119 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/deadline_http.py +96 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/errors.py +133 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/execution_governance.py +270 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/invocation_output.py +11 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/json_codec.py +31 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/py.typed +0 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/session.py +536 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/session_exec_client.py +76 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/types.py +150 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/view_preparation.py +64 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/workspace_client.py +180 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/workspace_lifecycle.py +154 -0
- genesis_sandbox_client_python-0.1.0/src/genesis_sandbox_client/workspace_purge.py +26 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: genesis-sandbox-client-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Dependency-free Python client SDK for Genesis Sandbox (sessions, jobs, WorkspaceFS, artifacts)
|
|
5
|
+
Keywords: genesis,sandbox,sdk,code-execution,client
|
|
6
|
+
Author: caoshouling
|
|
7
|
+
Author-email: caoshouling <1046757614@qq.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Project-URL: Homepage, https://github.com/capemeta/genesis-sandbox-client-python
|
|
20
|
+
Project-URL: Repository, https://github.com/capemeta/genesis-sandbox-client-python
|
|
21
|
+
Project-URL: Issues, https://github.com/capemeta/genesis-sandbox-client-python/issues
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Genesis Sandbox Client — Python SDK
|
|
25
|
+
|
|
26
|
+
Genesis Sandbox 的官方 Python 客户端 SDK。**零第三方依赖**(仅标准库),提供同步与异步两套会话层、类型化错误分类、幂等恢复语义与预算受控的日志/文件读取。
|
|
27
|
+
|
|
28
|
+
- 服务端协议与实现见 [genesis-sandbox](https://github.com/capemeta/genesis-sandbox) 仓库
|
|
29
|
+
- 兄弟 SDK:[genesis-sandbox-client-go](https://github.com/capemeta/genesis-sandbox-client-go)、[genesis-sandbox-client-java](https://github.com/capemeta/genesis-sandbox-client-java)、[genesis-sandbox-client-typescript](https://github.com/capemeta/genesis-sandbox-client-typescript)
|
|
30
|
+
|
|
31
|
+
## 安装
|
|
32
|
+
|
|
33
|
+
要求 Python 3.12+。
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install genesis-sandbox-client-python==0.1.0
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
首次公开发布版本为 `0.1.0`,不沿用单仓时期的内部版本编号。发布与 Platform 接入步骤见 [发布指南](https://github.com/capemeta/genesis-sandbox-client-python/blob/main/docs/publishing.md)。
|
|
40
|
+
|
|
41
|
+
> 首次发布完成前请从源码安装,例如
|
|
42
|
+
> `uv pip install git+https://github.com/capemeta/genesis-sandbox-client-python.git`,
|
|
43
|
+
> 或本地 `uv pip install -e /path/to/genesis-sandbox-client-python`。
|
|
44
|
+
|
|
45
|
+
本地开发(uv):
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
uv sync # 创建 .venv 并可编辑安装本包 + dev 依赖
|
|
49
|
+
uv run pytest # 运行测试
|
|
50
|
+
uv run ruff check . # lint
|
|
51
|
+
uv run mypy src/genesis_sandbox_client # 类型检查
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 快速开始
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from genesis_sandbox_client import APIError, Client, TransportError, new_sandbox
|
|
58
|
+
|
|
59
|
+
client = Client(
|
|
60
|
+
"http://127.0.0.1:18010", # 非 HTTPS 仅允许回环地址
|
|
61
|
+
token="test-token-1",
|
|
62
|
+
timeout=30,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
with new_sandbox(client, profile="code-polyglot-basic") as sb:
|
|
66
|
+
sb.write_file("input/name.txt", "Genesis")
|
|
67
|
+
result = sb.run_python("""
|
|
68
|
+
from pathlib import Path
|
|
69
|
+
print("hello", Path("/workspace/input/name.txt").read_text())
|
|
70
|
+
""")
|
|
71
|
+
print(result.stdout)
|
|
72
|
+
|
|
73
|
+
exec_id = sb.run_async('print("async")', lang="python") # 异步提交
|
|
74
|
+
sb.wait_exec(exec_id, max_wait=60) # 有界等待
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
异步:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
async with new_sandbox_async(client, profile="python") as sb:
|
|
81
|
+
result = await sb.run_python('print("hello")')
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
无状态一次性 Job:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from genesis_sandbox_client import quick_python
|
|
88
|
+
|
|
89
|
+
result = quick_python(client, "print(42)")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 包结构
|
|
93
|
+
|
|
94
|
+
| 模块 | 说明 |
|
|
95
|
+
| --- | --- |
|
|
96
|
+
| `genesis_sandbox_client/client.py` | 低层 Client,覆盖 jobs / workspaces / sessions / session WorkspaceFS / artifacts / 环境解析 / 依赖构建 / GUI;含 `lookup_session`、`read_exec_logs` 预算读取、`token_provider` 凭据轮转 |
|
|
97
|
+
| `genesis_sandbox_client/session.py` | 同步 `SandboxSession` helper(休眠创建、懒启 Runtime、Suspend、内置心跳、WorkspaceFS、`cancel_exec_receipt`) |
|
|
98
|
+
| `genesis_sandbox_client/async_session.py` | 异步 `AsyncSandboxSession` helper(与同步对齐:lookup / 取消回执 / 日志流与预算读取;阻塞 I/O 经 `asyncio.to_thread` 下沉) |
|
|
99
|
+
| `genesis_sandbox_client/errors.py` | `APIError` / `TransportError` / `ProtocolError` / `ExecRecoveryError` 类型化错误与 `classify_error` 稳定分类 |
|
|
100
|
+
| `genesis_sandbox_client/types.py` | `ExecResult` / `SandboxOptions` / `CancelReceipt` / `SSEEvent` / `EffectiveEnvironment` 与心跳常量 |
|
|
101
|
+
|
|
102
|
+
包含 `py.typed`(PEP 561),全公开 API 均带类型注解。
|
|
103
|
+
|
|
104
|
+
## 运行示例
|
|
105
|
+
|
|
106
|
+
先在 genesis-sandbox 仓库启动本地开发环境(`scripts\start-dev-container.bat`),然后:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
export GENESIS_SANDBOX_BASE_URL=http://127.0.0.1:18010
|
|
110
|
+
export GENESIS_SANDBOX_API_KEY=test-token-1
|
|
111
|
+
|
|
112
|
+
uv run python examples/quickstart.py # 推荐应用入口
|
|
113
|
+
uv run python examples/production_sync.py # Session + WorkspaceFS 主路径(含 Suspend)
|
|
114
|
+
uv run python examples/job_artifact_sync.py # 单次 Job + Artifact 无状态链路
|
|
115
|
+
uv run python examples/production_async.py # 异步并发(fan-out / 信号量限流)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 错误处理
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from genesis_sandbox_client import APIError, ExecRecoveryError, TransportError, classify_error
|
|
122
|
+
|
|
123
|
+
try:
|
|
124
|
+
result = sb.run_python(code)
|
|
125
|
+
except APIError as e: # 结构化服务端错误
|
|
126
|
+
print(e.status_code, e.error_code, e.request_id, e.retryable, e.retry_after)
|
|
127
|
+
except TransportError as e: # 网络/协议层失败(ProtocolError 是其子类)
|
|
128
|
+
...
|
|
129
|
+
except ExecRecoveryError as e: # 结果未知,按身份恢复
|
|
130
|
+
record = sb.get_exec_by_operation(e.operation_id)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`classify_error(exc)` 返回稳定枚举式字符串(`invalid_request` / `unauthorized` / `not_found` / `conflict` / `rate_limited` / `transient` / `transport` / `protocol` / `unknown`),供上层 Adapter 映射通用契约错误码。
|
|
134
|
+
|
|
135
|
+
## 关键语义(生产必读)
|
|
136
|
+
|
|
137
|
+
### 写重试与 operation_id
|
|
138
|
+
|
|
139
|
+
全部写操作只发送一次,即使携带原生幂等身份或传入 `max_retries`。幂等身份用于查询原结果,不作为未知副作用自动重放的许可;仅 GET/HEAD/OPTIONS 可以有界重试。
|
|
140
|
+
|
|
141
|
+
同步/异步自动心跳在续租异常后暂停,可通过 `heartbeat_error` 查看原异常;先查询原会话期限和状态,不自动续写、重建计算或切换环境。
|
|
142
|
+
|
|
143
|
+
`exec_named_session` / `exec_session_async` 未显式传 `operation_id` 时本地生成 uuid4——进程在拿到回执前崩溃则该身份丢失。**生产必须显式传入并持久保存 operation_id**,响应丢失时用 `get_exec_by_operation` / `wait_exec_by_operation` 按同一身份查回执。
|
|
144
|
+
|
|
145
|
+
### 恢复(ExecRecoveryError)
|
|
146
|
+
|
|
147
|
+
`client.list_session_execs(session_id, limit=50, cursor=None)` 与同步/异步会话 `list_execs` 只读分页查询原执行,返回服务端的 `items`、`total` 和可选 `next_cursor`,不会创建或恢复运行环境。
|
|
148
|
+
|
|
149
|
+
提交或观察发生不确定的网络错误时抛 `ExecRecoveryError`,携带 `session_id`、`operation_id`、可选 `exec_id` 与 `phase`(`submit` / `observe` / `lookup` / `cancel`)。会话创建响应丢失用 `client.lookup_session(idempotency_key)`(404 返回 None)。
|
|
150
|
+
|
|
151
|
+
### 取消语义(接受 vs 停止确认)
|
|
152
|
+
|
|
153
|
+
服务端 `:cancel` 确认停止后才返回终态 ExecRecord。`cancel_exec_receipt(exec_id)` 返回 `CancelReceipt`,outcome 为 `accepted` / `already_terminal` / `stop_confirmed`;**`stop_confirmed` 是资源回收安全门禁**。停止未知抛 `ExecRecoveryError(phase="cancel")`——未确认前不得回收资源或重跑替代执行。
|
|
154
|
+
|
|
155
|
+
### 日志预算分页
|
|
156
|
+
|
|
157
|
+
`read_exec_logs(exec_id, *, cursor, byte_budget, max_events)` 返回 `events` / `next_cursor` / `exhausted` / `gap_detected`。游标对外为不透明字符串,原样回传 `next_cursor` 继续读取;事件游标不连续时 `gap_detected=True` 显式标记(缺失不可补齐)。流式消费用 `stream_exec_logs`。
|
|
158
|
+
|
|
159
|
+
### 凭据与端点安全
|
|
160
|
+
|
|
161
|
+
- `Client(..., token_provider=lambda: get_short_lived_token())`:每次请求发出前取值(含重试),优先于固定 token。
|
|
162
|
+
- Bearer 只发往配置的 `base_url`,不随重定向转发(3xx 按错误返回)。
|
|
163
|
+
- `base_url` 必须是无内嵌凭据、无路径/查询/片段的 HTTP(S) 绝对地址;非 HTTPS 仅允许回环地址。
|
|
164
|
+
- token 拒绝空白边缘与 CR/LF(防头注入)。
|
|
165
|
+
|
|
166
|
+
### 依赖构建
|
|
167
|
+
|
|
168
|
+
依赖构建必须绑定已解析环境:
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
resolution = client.resolve_environment(hints=["runtime.python"], strict=True, ttl_seconds=1800)
|
|
172
|
+
build = client.build_dependencies(
|
|
173
|
+
resolution_id=resolution["resolution_id"],
|
|
174
|
+
language="python",
|
|
175
|
+
lockfile=requirements_lock,
|
|
176
|
+
)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
从 Catalog 选择 Card 时,用 `resolve_environment(profile=card["name"], profile_revision=card["profile_revision"])` 精确绑定后再构建。
|
|
180
|
+
|
|
181
|
+
## 从单仓开发版本迁移
|
|
182
|
+
|
|
183
|
+
SDK 已从 `genesis-sandbox` 仓库迁至本仓库,导入名从 `sandbox` 改为 `genesis_sandbox_client`:
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
# 旧
|
|
187
|
+
from sandbox import Client, new_sandbox
|
|
188
|
+
|
|
189
|
+
# 新
|
|
190
|
+
from genesis_sandbox_client import Client, new_sandbox
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
单仓时期的 `0.1.x`、`0.2.x`、`0.3.x` 为内部开发编号;独立 Python 包从 `0.1.0` 开始公开发布。版本号独立于 Sandbox 服务和其他语言 SDK。
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
Apache-2.0
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Genesis Sandbox Client — Python SDK
|
|
2
|
+
|
|
3
|
+
Genesis Sandbox 的官方 Python 客户端 SDK。**零第三方依赖**(仅标准库),提供同步与异步两套会话层、类型化错误分类、幂等恢复语义与预算受控的日志/文件读取。
|
|
4
|
+
|
|
5
|
+
- 服务端协议与实现见 [genesis-sandbox](https://github.com/capemeta/genesis-sandbox) 仓库
|
|
6
|
+
- 兄弟 SDK:[genesis-sandbox-client-go](https://github.com/capemeta/genesis-sandbox-client-go)、[genesis-sandbox-client-java](https://github.com/capemeta/genesis-sandbox-client-java)、[genesis-sandbox-client-typescript](https://github.com/capemeta/genesis-sandbox-client-typescript)
|
|
7
|
+
|
|
8
|
+
## 安装
|
|
9
|
+
|
|
10
|
+
要求 Python 3.12+。
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install genesis-sandbox-client-python==0.1.0
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
首次公开发布版本为 `0.1.0`,不沿用单仓时期的内部版本编号。发布与 Platform 接入步骤见 [发布指南](https://github.com/capemeta/genesis-sandbox-client-python/blob/main/docs/publishing.md)。
|
|
17
|
+
|
|
18
|
+
> 首次发布完成前请从源码安装,例如
|
|
19
|
+
> `uv pip install git+https://github.com/capemeta/genesis-sandbox-client-python.git`,
|
|
20
|
+
> 或本地 `uv pip install -e /path/to/genesis-sandbox-client-python`。
|
|
21
|
+
|
|
22
|
+
本地开发(uv):
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uv sync # 创建 .venv 并可编辑安装本包 + dev 依赖
|
|
26
|
+
uv run pytest # 运行测试
|
|
27
|
+
uv run ruff check . # lint
|
|
28
|
+
uv run mypy src/genesis_sandbox_client # 类型检查
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 快速开始
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from genesis_sandbox_client import APIError, Client, TransportError, new_sandbox
|
|
35
|
+
|
|
36
|
+
client = Client(
|
|
37
|
+
"http://127.0.0.1:18010", # 非 HTTPS 仅允许回环地址
|
|
38
|
+
token="test-token-1",
|
|
39
|
+
timeout=30,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
with new_sandbox(client, profile="code-polyglot-basic") as sb:
|
|
43
|
+
sb.write_file("input/name.txt", "Genesis")
|
|
44
|
+
result = sb.run_python("""
|
|
45
|
+
from pathlib import Path
|
|
46
|
+
print("hello", Path("/workspace/input/name.txt").read_text())
|
|
47
|
+
""")
|
|
48
|
+
print(result.stdout)
|
|
49
|
+
|
|
50
|
+
exec_id = sb.run_async('print("async")', lang="python") # 异步提交
|
|
51
|
+
sb.wait_exec(exec_id, max_wait=60) # 有界等待
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
异步:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
async with new_sandbox_async(client, profile="python") as sb:
|
|
58
|
+
result = await sb.run_python('print("hello")')
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
无状态一次性 Job:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from genesis_sandbox_client import quick_python
|
|
65
|
+
|
|
66
|
+
result = quick_python(client, "print(42)")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 包结构
|
|
70
|
+
|
|
71
|
+
| 模块 | 说明 |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| `genesis_sandbox_client/client.py` | 低层 Client,覆盖 jobs / workspaces / sessions / session WorkspaceFS / artifacts / 环境解析 / 依赖构建 / GUI;含 `lookup_session`、`read_exec_logs` 预算读取、`token_provider` 凭据轮转 |
|
|
74
|
+
| `genesis_sandbox_client/session.py` | 同步 `SandboxSession` helper(休眠创建、懒启 Runtime、Suspend、内置心跳、WorkspaceFS、`cancel_exec_receipt`) |
|
|
75
|
+
| `genesis_sandbox_client/async_session.py` | 异步 `AsyncSandboxSession` helper(与同步对齐:lookup / 取消回执 / 日志流与预算读取;阻塞 I/O 经 `asyncio.to_thread` 下沉) |
|
|
76
|
+
| `genesis_sandbox_client/errors.py` | `APIError` / `TransportError` / `ProtocolError` / `ExecRecoveryError` 类型化错误与 `classify_error` 稳定分类 |
|
|
77
|
+
| `genesis_sandbox_client/types.py` | `ExecResult` / `SandboxOptions` / `CancelReceipt` / `SSEEvent` / `EffectiveEnvironment` 与心跳常量 |
|
|
78
|
+
|
|
79
|
+
包含 `py.typed`(PEP 561),全公开 API 均带类型注解。
|
|
80
|
+
|
|
81
|
+
## 运行示例
|
|
82
|
+
|
|
83
|
+
先在 genesis-sandbox 仓库启动本地开发环境(`scripts\start-dev-container.bat`),然后:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
export GENESIS_SANDBOX_BASE_URL=http://127.0.0.1:18010
|
|
87
|
+
export GENESIS_SANDBOX_API_KEY=test-token-1
|
|
88
|
+
|
|
89
|
+
uv run python examples/quickstart.py # 推荐应用入口
|
|
90
|
+
uv run python examples/production_sync.py # Session + WorkspaceFS 主路径(含 Suspend)
|
|
91
|
+
uv run python examples/job_artifact_sync.py # 单次 Job + Artifact 无状态链路
|
|
92
|
+
uv run python examples/production_async.py # 异步并发(fan-out / 信号量限流)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 错误处理
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from genesis_sandbox_client import APIError, ExecRecoveryError, TransportError, classify_error
|
|
99
|
+
|
|
100
|
+
try:
|
|
101
|
+
result = sb.run_python(code)
|
|
102
|
+
except APIError as e: # 结构化服务端错误
|
|
103
|
+
print(e.status_code, e.error_code, e.request_id, e.retryable, e.retry_after)
|
|
104
|
+
except TransportError as e: # 网络/协议层失败(ProtocolError 是其子类)
|
|
105
|
+
...
|
|
106
|
+
except ExecRecoveryError as e: # 结果未知,按身份恢复
|
|
107
|
+
record = sb.get_exec_by_operation(e.operation_id)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`classify_error(exc)` 返回稳定枚举式字符串(`invalid_request` / `unauthorized` / `not_found` / `conflict` / `rate_limited` / `transient` / `transport` / `protocol` / `unknown`),供上层 Adapter 映射通用契约错误码。
|
|
111
|
+
|
|
112
|
+
## 关键语义(生产必读)
|
|
113
|
+
|
|
114
|
+
### 写重试与 operation_id
|
|
115
|
+
|
|
116
|
+
全部写操作只发送一次,即使携带原生幂等身份或传入 `max_retries`。幂等身份用于查询原结果,不作为未知副作用自动重放的许可;仅 GET/HEAD/OPTIONS 可以有界重试。
|
|
117
|
+
|
|
118
|
+
同步/异步自动心跳在续租异常后暂停,可通过 `heartbeat_error` 查看原异常;先查询原会话期限和状态,不自动续写、重建计算或切换环境。
|
|
119
|
+
|
|
120
|
+
`exec_named_session` / `exec_session_async` 未显式传 `operation_id` 时本地生成 uuid4——进程在拿到回执前崩溃则该身份丢失。**生产必须显式传入并持久保存 operation_id**,响应丢失时用 `get_exec_by_operation` / `wait_exec_by_operation` 按同一身份查回执。
|
|
121
|
+
|
|
122
|
+
### 恢复(ExecRecoveryError)
|
|
123
|
+
|
|
124
|
+
`client.list_session_execs(session_id, limit=50, cursor=None)` 与同步/异步会话 `list_execs` 只读分页查询原执行,返回服务端的 `items`、`total` 和可选 `next_cursor`,不会创建或恢复运行环境。
|
|
125
|
+
|
|
126
|
+
提交或观察发生不确定的网络错误时抛 `ExecRecoveryError`,携带 `session_id`、`operation_id`、可选 `exec_id` 与 `phase`(`submit` / `observe` / `lookup` / `cancel`)。会话创建响应丢失用 `client.lookup_session(idempotency_key)`(404 返回 None)。
|
|
127
|
+
|
|
128
|
+
### 取消语义(接受 vs 停止确认)
|
|
129
|
+
|
|
130
|
+
服务端 `:cancel` 确认停止后才返回终态 ExecRecord。`cancel_exec_receipt(exec_id)` 返回 `CancelReceipt`,outcome 为 `accepted` / `already_terminal` / `stop_confirmed`;**`stop_confirmed` 是资源回收安全门禁**。停止未知抛 `ExecRecoveryError(phase="cancel")`——未确认前不得回收资源或重跑替代执行。
|
|
131
|
+
|
|
132
|
+
### 日志预算分页
|
|
133
|
+
|
|
134
|
+
`read_exec_logs(exec_id, *, cursor, byte_budget, max_events)` 返回 `events` / `next_cursor` / `exhausted` / `gap_detected`。游标对外为不透明字符串,原样回传 `next_cursor` 继续读取;事件游标不连续时 `gap_detected=True` 显式标记(缺失不可补齐)。流式消费用 `stream_exec_logs`。
|
|
135
|
+
|
|
136
|
+
### 凭据与端点安全
|
|
137
|
+
|
|
138
|
+
- `Client(..., token_provider=lambda: get_short_lived_token())`:每次请求发出前取值(含重试),优先于固定 token。
|
|
139
|
+
- Bearer 只发往配置的 `base_url`,不随重定向转发(3xx 按错误返回)。
|
|
140
|
+
- `base_url` 必须是无内嵌凭据、无路径/查询/片段的 HTTP(S) 绝对地址;非 HTTPS 仅允许回环地址。
|
|
141
|
+
- token 拒绝空白边缘与 CR/LF(防头注入)。
|
|
142
|
+
|
|
143
|
+
### 依赖构建
|
|
144
|
+
|
|
145
|
+
依赖构建必须绑定已解析环境:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
resolution = client.resolve_environment(hints=["runtime.python"], strict=True, ttl_seconds=1800)
|
|
149
|
+
build = client.build_dependencies(
|
|
150
|
+
resolution_id=resolution["resolution_id"],
|
|
151
|
+
language="python",
|
|
152
|
+
lockfile=requirements_lock,
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
从 Catalog 选择 Card 时,用 `resolve_environment(profile=card["name"], profile_revision=card["profile_revision"])` 精确绑定后再构建。
|
|
157
|
+
|
|
158
|
+
## 从单仓开发版本迁移
|
|
159
|
+
|
|
160
|
+
SDK 已从 `genesis-sandbox` 仓库迁至本仓库,导入名从 `sandbox` 改为 `genesis_sandbox_client`:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
# 旧
|
|
164
|
+
from sandbox import Client, new_sandbox
|
|
165
|
+
|
|
166
|
+
# 新
|
|
167
|
+
from genesis_sandbox_client import Client, new_sandbox
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
单仓时期的 `0.1.x`、`0.2.x`、`0.3.x` 为内部开发编号;独立 Python 包从 `0.1.0` 开始公开发布。版本号独立于 Sandbox 服务和其他语言 SDK。
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
Apache-2.0
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "genesis-sandbox-client-python"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Dependency-free Python client SDK for Genesis Sandbox (sessions, jobs, WorkspaceFS, artifacts)"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = [
|
|
10
|
+
"genesis",
|
|
11
|
+
"sandbox",
|
|
12
|
+
"sdk",
|
|
13
|
+
"code-execution",
|
|
14
|
+
"client",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[[project.authors]]
|
|
29
|
+
name = "caoshouling"
|
|
30
|
+
email = "1046757614@qq.com"
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/capemeta/genesis-sandbox-client-python"
|
|
34
|
+
Repository = "https://github.com/capemeta/genesis-sandbox-client-python"
|
|
35
|
+
Issues = "https://github.com/capemeta/genesis-sandbox-client-python/issues"
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest>=8.3",
|
|
40
|
+
"ruff>=0.6",
|
|
41
|
+
"mypy>=1.13",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[build-system]
|
|
45
|
+
requires = ["uv_build>=0.12.5,<0.13.0"]
|
|
46
|
+
build-backend = "uv_build"
|
|
47
|
+
|
|
48
|
+
[tool.uv.build-backend]
|
|
49
|
+
module-name = "genesis_sandbox_client"
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 120
|
|
56
|
+
target-version = "py312"
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = [
|
|
60
|
+
"E",
|
|
61
|
+
"F",
|
|
62
|
+
"W",
|
|
63
|
+
"I",
|
|
64
|
+
"UP",
|
|
65
|
+
"B",
|
|
66
|
+
]
|