harbor-rayrun 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,17 @@
1
+ """harbor-rayrun —— 把 Rayrun 沙箱云接成 Harbor 的评测环境。
2
+
3
+ Harbor 的 `BaseEnvironment.type()` 显式允许返回任意字符串(它的注释写明是为了让
4
+ 仓库之外的第三方环境不必改 `EnvironmentType` 枚举),所以本 provider **不需要**
5
+ Harbor 上游改动即可用 `--env` 入口加载:
6
+
7
+ uv pip install ./integrations/harbor-rayrun
8
+ export RAYRUN_API_URL=https://api.rayrunos.com/v1 # 自托管:http://<daemon>:8080/v1
9
+ export RAYRUN_TOKEN=rayrun_<token_id>_<secret>
10
+ harbor run -t <task> -a oracle -e harbor_rayrun:RayrunEnvironment
11
+
12
+ 环境变量沿用 CLI 的同名约定(`07` §2.5),不新造一套 key 体系。
13
+ """
14
+
15
+ from harbor_rayrun.environment import RayrunEnvironment
16
+
17
+ __all__ = ["RayrunEnvironment"]
@@ -0,0 +1,260 @@
1
+ """Rayrun HTTP API 的薄客户端(provider 专用)。
2
+
3
+ 只覆盖 provider 真正用到的端点:沙箱生命周期(create/get/stop/delete)、
4
+ `exec`、文件读写与列目录、镜像列表。刻意不做成通用 SDK —— `openapi.yaml`
5
+ 生成的 SDK 是独立立项(`docs/15` §10-Q7),provider 只要一层能对上的薄客户端。
6
+
7
+ 契约来源:`docs/06` §4.3/§4.4.1/§4.5/§4.6;认证与错误 envelope 见 §1.5/§2。
8
+ 所有非 2xx 一律抛 `RayrunAPIError`(带 code + message),provider 侧不再做二次
9
+ 翻译 —— 错误码是全产品统一的(`06` §2)。
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import io
15
+ from dataclasses import dataclass
16
+ from typing import AsyncIterator, BinaryIO
17
+ from urllib.parse import quote
18
+
19
+ import httpx
20
+
21
+
22
+ class RayrunAPIError(RuntimeError):
23
+ """Rayrun 返回的错误响应(`{"error": code, "message": ...}`,`06` §1.5)。"""
24
+
25
+ def __init__(self, status: int, code: str, message: str = "") -> None:
26
+ self.status = status
27
+ self.code = code
28
+ self.message = message
29
+ detail = f"{code}: {message}" if message else code
30
+ super().__init__(f"rayrun API {status} {detail}")
31
+
32
+
33
+ @dataclass(frozen=True)
34
+ class Sandbox:
35
+ id: str
36
+ status: str
37
+ ip: str | None
38
+ name: str | None
39
+
40
+
41
+ @dataclass(frozen=True)
42
+ class CommandResult:
43
+ """`POST /sandboxes/{id}/exec` 的 200 响应(`06` §4.4.1)。
44
+
45
+ `exit_code` 为 None 当且仅当 `timed_out` —— 契约明确不伪造 128+9。
46
+ """
47
+
48
+ stdout: str
49
+ stderr: str
50
+ exit_code: int | None
51
+ duration_ms: int
52
+ timed_out: bool
53
+ truncated: bool
54
+
55
+
56
+ class RayrunClient:
57
+ """Rayrun daemon 的异步客户端。`base_url` 需含 `/v1`(与 CLI 约定一致)。"""
58
+
59
+ def __init__(
60
+ self,
61
+ base_url: str,
62
+ token: str,
63
+ *,
64
+ transport: httpx.AsyncBaseTransport | None = None,
65
+ ) -> None:
66
+ """`transport` 是**测试缝隙**(与 guestd 的 runCmd / netOps 注入同款范式):
67
+ 单测传 `httpx.MockTransport` 打桩,生产传 None 走真实网络。"""
68
+ if not base_url:
69
+ raise ValueError("RAYRUN_API_URL 为空")
70
+ if not token:
71
+ raise ValueError("RAYRUN_TOKEN 为空")
72
+ # read 超时关掉:exec 同步阻塞到命令结束,可能是分钟级(`06` §4.4.1 的
73
+ # timeout_seconds 最长 3600s,权威超时在 daemon 侧)。connect/write 仍要
74
+ # 有界,否则网络不通时会挂死。
75
+ timeout = httpx.Timeout(connect=10.0, read=None, write=60.0, pool=10.0)
76
+ self._client = httpx.AsyncClient(
77
+ base_url=base_url.rstrip("/"),
78
+ headers={"Authorization": f"Bearer {token}"},
79
+ timeout=timeout,
80
+ transport=transport,
81
+ )
82
+
83
+ async def aclose(self) -> None:
84
+ await self._client.aclose()
85
+
86
+ # ---------------------------------------------------------------- 内部
87
+ @staticmethod
88
+ def _encode_path(path: str) -> str:
89
+ """把沙箱内的相对路径编进 URL(保留 `/`,其余按段转义)。"""
90
+ return quote(path.lstrip("/"), safe="/")
91
+
92
+ def _raise_for_status(self, resp: httpx.Response) -> None:
93
+ if resp.status_code < 400:
94
+ return
95
+ code, message = "unknown", ""
96
+ try:
97
+ body = resp.json()
98
+ code = str(body.get("error") or code)
99
+ message = str(body.get("message") or "")
100
+ except Exception: # noqa: BLE001 —— 非 JSON 响应(如 nginx 的 HTML 413)
101
+ message = resp.text[:200]
102
+ raise RayrunAPIError(resp.status_code, code, message)
103
+
104
+ # ---------------------------------------------------------------- 只读
105
+ async def agents(self) -> list[dict]:
106
+ """`GET /v1/agents`(读宿主机镜像目录,非数据库)。"""
107
+ resp = await self._client.get("/agents")
108
+ self._raise_for_status(resp)
109
+ return resp.json().get("agents", [])
110
+
111
+ async def get_sandbox(self, sandbox_id: str) -> Sandbox:
112
+ resp = await self._client.get(f"/sandboxes/{sandbox_id}")
113
+ self._raise_for_status(resp)
114
+ body = resp.json()
115
+ return Sandbox(
116
+ id=body["id"], status=body["status"], ip=body.get("ip"), name=body.get("name")
117
+ )
118
+
119
+ async def list_files(self, sandbox_id: str, path: str = "") -> list[dict]:
120
+ """`GET /v1/sandboxes/{id}/files?path=` —— **单层**列目录(`12` §4.2:无递归语义)。"""
121
+ resp = await self._client.get(
122
+ f"/sandboxes/{sandbox_id}/files", params={"path": path} if path else None
123
+ )
124
+ self._raise_for_status(resp)
125
+ return resp.json().get("entries", [])
126
+
127
+ async def read_file(self, sandbox_id: str, path: str) -> bytes:
128
+ resp = await self._client.get(
129
+ f"/sandboxes/{sandbox_id}/files/{self._encode_path(path)}"
130
+ )
131
+ self._raise_for_status(resp)
132
+ return resp.content
133
+
134
+ # ---------------------------------------------------------------- 写
135
+ async def create_sandbox(
136
+ self,
137
+ *,
138
+ name: str,
139
+ agent: str,
140
+ cpu_cores: int,
141
+ memory_mb: int,
142
+ egress_allow: list[str] | None = None,
143
+ egress_allow_all: bool = False,
144
+ workspace_mb: int | None = None,
145
+ max_lifetime_seconds: int | None = None,
146
+ ) -> Sandbox:
147
+ """`POST /v1/sandboxes`(201 = 已 Running,同步端点,`06` §4.3)。
148
+
149
+ `egress_allow=[]` 即**完全断网**;`egress_allow_all=True` 是显式的「允许
150
+ 全部出网」(2026-09-27 起,`06` §4.3)—— 两者**互斥**,同时给会被 daemon
151
+ 以 400 拒绝(这里也不构造那种请求)。
152
+ """
153
+ if egress_allow_all and egress_allow:
154
+ raise ValueError("egress_allow_all 与 egress_allow 互斥(daemon 会 400)")
155
+ egress: dict = {"allow": list(egress_allow or [])}
156
+ if egress_allow_all:
157
+ egress["allow_all"] = True
158
+ body: dict = {
159
+ "name": name,
160
+ "agent": agent,
161
+ "cpu_cores": cpu_cores,
162
+ "memory_mb": memory_mb,
163
+ "egress": egress,
164
+ }
165
+ if workspace_mb is not None:
166
+ body["workspace_mb"] = workspace_mb
167
+ if max_lifetime_seconds is not None:
168
+ body["max_lifetime_seconds"] = max_lifetime_seconds
169
+ resp = await self._client.post("/sandboxes", json=body)
170
+ self._raise_for_status(resp)
171
+ data = resp.json()
172
+ return Sandbox(
173
+ id=data["id"],
174
+ status=data["status"],
175
+ ip=data.get("ip"),
176
+ name=data.get("name"),
177
+ )
178
+
179
+ async def write_file(self, sandbox_id: str, path: str, data: bytes) -> int:
180
+ """`PUT /v1/sandboxes/{id}/files/{path}`(不设大小上限,`06` §4.5)。"""
181
+ return await self.write_file_stream(sandbox_id, path, io.BytesIO(data))
182
+
183
+ async def write_file_stream(self, sandbox_id: str, path: str, stream: BinaryIO) -> int:
184
+ """同上,但**流式**发送(逐 1 MiB 块)。
185
+
186
+ 两个理由:大文件不整份进 provider 的内存;daemon 本就是边读 body 边分块
187
+ 发往 guest(`12` D19),先攒全再发没有意义。
188
+ """
189
+
190
+ # 必须是**异步**生成器:httpx 的 AsyncClient 拒收同步 request stream
191
+ # ("Attempted to send an sync request with an AsyncClient instance")。
192
+ async def _chunks() -> AsyncIterator[bytes]:
193
+ while True:
194
+ chunk = stream.read(1 << 20)
195
+ if not chunk:
196
+ return
197
+ yield chunk
198
+
199
+ resp = await self._client.put(
200
+ f"/sandboxes/{sandbox_id}/files/{self._encode_path(path)}",
201
+ content=_chunks(),
202
+ headers={"Content-Type": "application/octet-stream"},
203
+ )
204
+ self._raise_for_status(resp)
205
+ return int(resp.json().get("size", 0))
206
+
207
+ async def exec(
208
+ self,
209
+ sandbox_id: str,
210
+ command: str | None = None,
211
+ *,
212
+ argv: list[str] | None = None,
213
+ cwd: str | None = None,
214
+ env: dict[str, str] | None = None,
215
+ timeout_seconds: int | None = None,
216
+ user: str | None = None,
217
+ ) -> CommandResult:
218
+ """`POST /v1/sandboxes/{id}/exec` —— **同步**:响应即命令结束(`06` §4.4.1)。
219
+
220
+ `command` 与 `argv` **恰好二选一**(`06` §4.4.1):给 `command` 由服务端
221
+ 归一为 `["/bin/sh","-c",command]`,给 `argv` 则原样 exec、不经 shell。
222
+ provider 走后者 —— 理由见 `environment.exec`。
223
+ """
224
+ if (command is None) == (argv is None):
225
+ raise ValueError("exec 需要 command 与 argv 恰好其一")
226
+ body: dict = {"argv": argv} if argv is not None else {"command": command}
227
+ if cwd:
228
+ body["cwd"] = cwd
229
+ if env:
230
+ body["env"] = env
231
+ if timeout_seconds:
232
+ body["timeout_seconds"] = timeout_seconds
233
+ if user:
234
+ body["user"] = user
235
+ resp = await self._client.post(f"/sandboxes/{sandbox_id}/exec", json=body)
236
+ self._raise_for_status(resp)
237
+ data = resp.json()
238
+ return CommandResult(
239
+ stdout=data.get("stdout") or "",
240
+ stderr=data.get("stderr") or "",
241
+ exit_code=data.get("exit_code"),
242
+ duration_ms=int(data.get("duration_ms") or 0),
243
+ timed_out=bool(data.get("timed_out")),
244
+ truncated=bool(data.get("truncated")),
245
+ )
246
+
247
+ # ---------------------------------------------------------------- 生命周期
248
+ async def stop_sandbox(self, sandbox_id: str) -> None:
249
+ """`POST /v1/sandboxes/{id}/stop`(ADR-005 冷停:杀进程、留盘、停表)。"""
250
+ resp = await self._client.post(f"/sandboxes/{sandbox_id}/stop")
251
+ if resp.status_code == 404:
252
+ return # 幂等:没了就是停好了
253
+ self._raise_for_status(resp)
254
+
255
+ async def delete_sandbox(self, sandbox_id: str) -> None:
256
+ """`DELETE /v1/sandboxes/{id}`(204;Terminated 幂等,从未存在 404)。"""
257
+ resp = await self._client.delete(f"/sandboxes/{sandbox_id}")
258
+ if resp.status_code == 404:
259
+ return
260
+ self._raise_for_status(resp)
@@ -0,0 +1,554 @@
1
+ """Rayrun 沙箱云的 Harbor provider(`docs/plans/harbor-integration.md` M3)。
2
+
3
+ Harbor 把每个 trial 跑在一个隔离环境里,环境由 provider 实现。本文件把那个环境
4
+ 接到 **Rayrun 的 HTTP API**(控制面)—— 与 E2B/Daytona 那类 provider 不同,这里
5
+ **不走沙箱的入向**(不拨端口、不需要公网域名),所以不依赖 `docs/13` 的服务发布。
6
+
7
+ 契约面(`15` §2.1)与 Rayrun 端点(`06` §4)的映射:
8
+
9
+ start(force_build) → 校验镜像存在 + POST /sandboxes(201 即 Running)
10
+ stop(delete=True) → DELETE /sandboxes/{id}
11
+ stop(delete=False) → POST /sandboxes/{id}/stop ← ADR-005 冷停:留盘停表
12
+ exec(...) → POST /sandboxes/{id}/exec ← M1 新落的原语(ADR-007)
13
+ upload_file / upload_dir → PUT /sandboxes/{id}/files/{path}
14
+ download_file / download_dir → GET …/files/{path} + GET …/files(单层列目录)
15
+ is_dir / is_file → 基类默认实现(走 exec 的 test -d/-f),不覆写
16
+
17
+ **已知边界(诚实声明,不是缺陷;`15` §6.3「声明即契约」)**:
18
+
19
+ 1. **不支持 PUBLIC 出网**:Rayrun 的 egress 是「精确白名单」模型,**没有「允许全部」
20
+ 档位**(`06` §4.3)。Harbor 的 `NetworkPolicy` 默认是 `PUBLIC`,这类任务会在建沙箱
21
+ 之前**明确报错** —— 绝不静默降级成断网(那会让任务以为能联网却莫名失败),也绝不
22
+ 假装白名单能覆盖「全部」。要跑这类任务:任务声明 `network_mode = "allowlist"`
23
+ (列出所需域名)或 `"no-network"`。⚠️ Harbor 对 `PUBLIC` 没有能力位可拦,
24
+ 所以这道闸必须在本 provider 里。
25
+ 2. **不支持 docker-compose 任务**(`capabilities.docker_compose=False`,Harbor 会在
26
+ 开跑前按声明裁剪任务)。
27
+ 3. **无 GPU / TPU / Windows**;**不支持运行期改网络策略**(`dynamic_network_policy=False`)。
28
+ 4. **镜像不由 Harbor 构建**(也刻意不给 provider 加构建能力:那等于把「构建任意
29
+ Dockerfile」的 RCE 面开在产品面上,`15` §5.1 方案 A 已否决)。镜像来自离线 builder
30
+ (`images/build-task-image.sh`);缺失时 `start()` 报错并附一条可直接复制的构建命令。
31
+ 5. 单次 exec 的输出上限 4 MiB(超出置 `truncated`,会打 warn);命令超时按
32
+ `timeout(1)` 惯例给 Harbor 返回 124(Rayrun 契约里超时的 `exit_code` 是 null)。
33
+ """
34
+
35
+ from __future__ import annotations
36
+
37
+ import os
38
+ import shlex
39
+ import uuid
40
+ from pathlib import Path, PurePosixPath
41
+ from typing import override
42
+
43
+ from harbor.environments.base import BaseEnvironment, ExecResult
44
+ from harbor.environments.capabilities import (
45
+ EnvironmentCapabilities,
46
+ EnvironmentResourceCapabilities,
47
+ )
48
+ from harbor.environments.definition import (
49
+ COMPOSE_FILE_NAME,
50
+ require_agent_environment_definition,
51
+ )
52
+ from harbor.models.task.config import EnvironmentConfig, NetworkMode, NetworkPolicy
53
+ from harbor.models.trial.paths import EnvironmentPaths, TrialPaths
54
+
55
+ from harbor_rayrun.client import RayrunClient
56
+
57
+ # env 名沿用 CLI 的既有约定(`07` §2.5),不新造一套 key 体系。
58
+ ENV_API_URL = "RAYRUN_API_URL"
59
+ ENV_TOKEN = "RAYRUN_TOKEN"
60
+ # 镜像名约定:`<prefix><environment_name>@<version>`。builder 的 `--name` 由使用者
61
+ # 决定、未必等于 Harbor 的任务名,所以两者都可覆盖。
62
+ ENV_IMAGE_PREFIX = "RAYRUN_IMAGE_PREFIX"
63
+ ENV_IMAGE_VERSION = "RAYRUN_IMAGE_VERSION"
64
+
65
+ # 任务没声明资源时的缺省(Rayrun 的下限是 1 核 / 512 MB,这里取更贴近评测的档位)。
66
+ DEFAULT_CPUS = 2
67
+ DEFAULT_MEMORY_MB = 2048
68
+
69
+ # 命令超时给 Harbor 的退出码:照 timeout(1) 惯例,与 Rayrun CLI 同口径(`07` §2.4)。
70
+ EXIT_CODE_TIMEOUT = 124
71
+
72
+ # 调用方没给 timeout_sec 时用的命令超时 = Rayrun exec 的上限(`06` §4.4.1)。
73
+ #
74
+ # 为什么不是「不传,让服务端用缺省」:Rayrun 的 exec **有**服务端缺省 300s,而
75
+ # Harbor 对 verifier 的时限是在**外层**用 asyncio.wait_for 兜的(docker provider
76
+ # 里 docker exec 本身无界)。照缺省走会让「外层给 900s、内层 300s 就杀」的截断
77
+ # 静默发生 —— 表现是 verifier 跑到第 5 分钟被打断、Harbor 只报 reward 文件不存在
78
+ #(2026-09-27 M4 实测踩到,`docs/plans/harbor-integration.md` §13.6)。
79
+ # 取上限 ⇒ **外层成为唯一时限权威**,语义与 docker provider 对齐。
80
+ DEFAULT_EXEC_TIMEOUT_SEC = 3600
81
+
82
+ # 沙箱名上限(`06` §4.3:同客户内活跃沙箱名唯一,≤ 64 字符)。
83
+ MAX_SANDBOX_NAME = 64
84
+
85
+ # 文件传输的中转目录(相对 workdir)。文件 API 只认 workdir 相对路径,Harbor
86
+ # 的路径全在 workdir 之外,所以每笔传输都要在此落一次脚。点号开头 + 每笔一个
87
+ # 随机子目录:与任务自己的文件不会撞名,也不会被任务的 `ls` 当成它的产物。
88
+ STAGING_DIR = ".rayrun-harbor-xfer"
89
+
90
+ # Harbor 约定的落盘路径(**从它的 EnvironmentPaths 派生**,不在这里抄一份 ——
91
+ # Harbor 改目录时这里跟着走)。它们在 workdir 之外,而沙箱里清单外的目录一律
92
+ # 只读(ADR-006 的 overlay 只覆盖固定的 7 个子树)⇒ 任务镜像必须把这三个目录
93
+ # 声明为可写,否则 verifier 连 `/logs/verifier/reward.txt` 都写不出来。
94
+ _HARBOR_PATHS = EnvironmentPaths()
95
+ HONORED_WRITABLE_DIRS: tuple[str, ...] = (
96
+ str(_HARBOR_PATHS.logs_dir),
97
+ str(_HARBOR_PATHS.tests_dir),
98
+ str(_HARBOR_PATHS.solution_dir),
99
+ )
100
+ WRITABLE_HINT = (
101
+ " ⚠ --writable-dir 是**必需**的(不是可选优化):这几个目录在沙箱里默认只读,"
102
+ "详见 docs/plans/harbor-integration.md §13.3"
103
+ )
104
+
105
+
106
+ class RayrunEnvironment(BaseEnvironment):
107
+ """把 Rayrun 沙箱接成 Harbor 的评测环境。"""
108
+
109
+ @classmethod
110
+ @override
111
+ def preflight(cls) -> None:
112
+ """开跑前校验凭证(Harbor 在 job 启动时调用;失败即 SystemExit)。"""
113
+ missing = [k for k in (ENV_API_URL, ENV_TOKEN) if not os.environ.get(k)]
114
+ if missing:
115
+ raise SystemExit(
116
+ f"Rayrun provider 需要 {' 与 '.join(missing)}。\n"
117
+ " 托管:export RAYRUN_API_URL=https://api.rayrunos.com/v1\n"
118
+ " 自托管:export RAYRUN_API_URL=http://<daemon 主机>:8080/v1\n"
119
+ " export RAYRUN_TOKEN=rayrun_<token_id>_<secret>\n"
120
+ "(与 CLI 同名,见 docs/07 §2.5)"
121
+ )
122
+
123
+ def __init__(
124
+ self,
125
+ environment_dir: Path,
126
+ environment_name: str,
127
+ session_id: str,
128
+ trial_paths: TrialPaths,
129
+ task_env_config: EnvironmentConfig,
130
+ *args,
131
+ **kwargs,
132
+ ) -> None:
133
+ super().__init__(
134
+ environment_dir=environment_dir,
135
+ environment_name=environment_name,
136
+ session_id=session_id,
137
+ trial_paths=trial_paths,
138
+ task_env_config=task_env_config,
139
+ *args,
140
+ **kwargs,
141
+ )
142
+ self._client = RayrunClient(
143
+ base_url=self._require_env(ENV_API_URL),
144
+ token=self._require_env(ENV_TOKEN),
145
+ )
146
+ self._sandbox_id: str | None = None
147
+ # 文件 API 的根(= manifest workdir),首次用到时问 guest 一次。
148
+ self._root: str | None = None
149
+
150
+ # ------------------------------------------------------------ 契约面
151
+ @staticmethod
152
+ @override
153
+ def type() -> str:
154
+ # 返回普通字符串而非 EnvironmentType 枚举:Harbor 的 type() 显式允许任意
155
+ # 字符串,正是为了让仓库外的第三方 provider 不必改上游枚举(`15` §2.1)。
156
+ return "rayrun"
157
+
158
+ @classmethod
159
+ @override
160
+ def resource_capabilities(cls) -> EnvironmentResourceCapabilities:
161
+ # 创建期硬分配(`06` §4.3 的 cpu_cores / memory_mb 即上限),不做 request 档。
162
+ return EnvironmentResourceCapabilities(
163
+ cpu_limit=True,
164
+ memory_limit=True,
165
+ cpu_request=False,
166
+ memory_request=False,
167
+ )
168
+
169
+ @property
170
+ @override
171
+ def capabilities(self) -> EnvironmentCapabilities:
172
+ # 字段名**逐条对着装好的 harbor 版本核过**(0.23.0 里没有 `stream` —— 那
173
+ # 是 main 分支新加的;pydantic 会静默忽略未知字段,写错了不会报错)。
174
+ return EnvironmentCapabilities(
175
+ gpus=False,
176
+ tpus=False,
177
+ windows=False,
178
+ mounted=False,
179
+ docker_compose=False,
180
+ disable_internet=True, # egress.allow=[] = 完全断网
181
+ network_allowlist=True, # 域名精确匹配 + IPv4 CIDR
182
+ network_allowlist_hostnames=True,
183
+ network_allowlist_wildcard_hostnames=False, # 精确匹配,刻意不做通配(`01` §9.3)
184
+ network_allowlist_ipv4_addresses=False, # 只收域名或 CIDR,不收裸 IP
185
+ network_allowlist_ipv6_addresses=False,
186
+ network_allowlist_ipv4_cidrs=True,
187
+ network_allowlist_ipv6_cidrs=False,
188
+ dynamic_network_policy=False, # egress 只在创建时提交一次
189
+ )
190
+
191
+ @override
192
+ def _validate_definition(self) -> None:
193
+ """开跑前的任务校验(纯查文件,不联网)。"""
194
+ if (self.environment_dir / COMPOSE_FILE_NAME).exists():
195
+ raise ValueError(
196
+ f"Rayrun 不支持 docker-compose 任务({self.environment_dir}/{COMPOSE_FILE_NAME})。\n"
197
+ " 可行路径:把该任务的环境改写成单个 Dockerfile,再用离线 builder 产成 Rayrun 镜像。"
198
+ )
199
+ require_agent_environment_definition(
200
+ self.environment_dir,
201
+ docker_image=self.task_env_config.docker_image,
202
+ )
203
+
204
+ # ------------------------------------------------------------ 生命周期
205
+ @override
206
+ async def start(self, force_build: bool) -> None:
207
+ """建沙箱。
208
+
209
+ `force_build` 对 Rayrun 无意义(镜像不由 provider 构建,见类文档第 4 条)——
210
+ 刻意不静默忽略它:镜像不存在时直接报错并给出构建命令。
211
+
212
+ 顺序有讲究:**先算 egress(PUBLIC 会在这里抛错)→ 再校验镜像 → 最后建沙箱**,
213
+ 免得为一个注定要拒绝的任务白建一台 VM。
214
+ """
215
+ if self._sandbox_id is not None:
216
+ raise RuntimeError(f"环境已启动(sandbox={self._sandbox_id}),不要重复 start")
217
+
218
+ egress_allow, egress_allow_all = self._egress()
219
+ image_ref = self._image_ref()
220
+ await self._require_image(image_ref)
221
+
222
+ cpus = self._effective_cpus or DEFAULT_CPUS
223
+ memory_mb = self._effective_memory_mb or DEFAULT_MEMORY_MB
224
+ if not self._effective_cpus or not self._effective_memory_mb:
225
+ self.logger.debug(
226
+ "任务未声明资源,使用缺省 cpu=%s memory_mb=%s", cpus, memory_mb
227
+ )
228
+
229
+ sandbox = await self._client.create_sandbox(
230
+ name=self._sandbox_name(),
231
+ agent=image_ref,
232
+ cpu_cores=int(cpus),
233
+ memory_mb=int(memory_mb),
234
+ egress_allow=egress_allow,
235
+ egress_allow_all=egress_allow_all,
236
+ )
237
+ self._sandbox_id = sandbox.id
238
+ self.logger.debug(
239
+ "rayrun sandbox %s(image=%s ip=%s egress=%s)",
240
+ sandbox.id,
241
+ image_ref,
242
+ sandbox.ip,
243
+ "allow_all" if egress_allow_all else (egress_allow or "[](断网)"),
244
+ )
245
+
246
+ # 基类助手:建挂载点目录(走我们实现的 exec);「预构建镜像」类任务还会把
247
+ # environment/ 传进 workdir(与 E2B provider 同款调用)。
248
+ await self.ensure_dirs(self._mount_targets(writable_only=True))
249
+ await self._upload_environment_dir_after_start()
250
+
251
+ @override
252
+ async def stop(self, delete: bool) -> None:
253
+ """`delete=True` → 销毁;`delete=False` → 冷停(留盘、停表、IP 不变)。"""
254
+ if self._sandbox_id is None:
255
+ return
256
+ sandbox_id, self._sandbox_id = self._sandbox_id, None
257
+ try:
258
+ if delete:
259
+ await self._client.delete_sandbox(sandbox_id)
260
+ else:
261
+ await self._client.stop_sandbox(sandbox_id)
262
+ finally:
263
+ await self._client.aclose()
264
+
265
+ @override
266
+ async def exec(
267
+ self,
268
+ command: str,
269
+ cwd: str | None = None,
270
+ env: dict[str, str] | None = None,
271
+ timeout_sec: int | None = None,
272
+ user: str | int | None = None,
273
+ ) -> ExecResult:
274
+ sandbox_id = self._require_sandbox()
275
+ resolved_user = self._resolve_user(user)
276
+ # guest 内只有 root(guestd 是 PID 1):明确拒绝其它 user,好过静默降级。
277
+ if resolved_user not in (None, "root", 0, "0"):
278
+ raise ValueError(f"Rayrun 只支持以 root 执行命令(收到 user={resolved_user!r})")
279
+
280
+ # **shell 语义必须对齐 docker provider**(`["bash","-c",command]`,
281
+ # `environments/docker/docker_unix.py:281`):installed agent 全都写
282
+ # `set -euo pipefail` 之类的 bash 特性,而 Rayrun exec 的 `command` 形式
283
+ # 归一为 `["/bin/sh","-c",…]`(`daemon/internal/app/exec.go:193`)——
284
+ # Debian/Ubuntu 的 `/bin/sh` 是 **dash**,当场 `set: Illegal option -o
285
+ # pipefail`,agent 连装都装不上。走 `argv` 而不是 `command` 是为了少一层
286
+ # `/bin/sh -c` 与随之而来的引号转义(exec 端点原生支持 argv,`06` §4.4.1)。
287
+ result = await self._client.exec(
288
+ sandbox_id,
289
+ argv=["bash", "-c", command],
290
+ cwd=cwd,
291
+ env=self._merge_env(env),
292
+ timeout_seconds=timeout_sec or DEFAULT_EXEC_TIMEOUT_SEC,
293
+ user="root",
294
+ )
295
+ # 超时:Rayrun 契约里 exit_code 为 null(不伪造 128+9),这里给 Harbor 一个
296
+ # 可判定的失败码(与 CLI 的 124 同口径)。
297
+ return_code = result.exit_code if result.exit_code is not None else EXIT_CODE_TIMEOUT
298
+ if result.truncated:
299
+ self.logger.warning(
300
+ "exec 输出触顶被截断(4 MiB 上限,stdout+stderr 合计):%s", command[:80]
301
+ )
302
+ return ExecResult(stdout=result.stdout, stderr=result.stderr, return_code=return_code)
303
+
304
+ # ------------------------------------------------------------ 文件
305
+ #
306
+ # **路径根**:Rayrun 的文件 API 是 **workdir 相对**的(`06` §4.5 —— 绝对路径
307
+ # 与 `..` 逃逸一律 400 `invalid_path` 并落审计,那是有意的安全边界),而
308
+ # Harbor 用的是**容器绝对路径**(`EnvironmentPaths` 把 `/logs` `/tests`
309
+ # `/solution` 写死在里面)。两者之间必须翻译,否则上传/下载全部 400。
310
+ #
311
+ # 翻译以「中转目录」收口:先把目标用 `cp -a` 挪进 workdir 下的中转目录,
312
+ # 再用文件 API 读写,完事删掉。**刻意不做「本来就在 workdir 里就直通」的
313
+ # 优化** —— Harbor 传给文件 API 的路径全都落在 workdir 之外(它自己的三个
314
+ # 目录),优化出来的分支在真实调用上一次都不会跑,却要多养一条路径。
315
+ # 代价是每次传输多一次 guest 内 `cp`(本地盘,与跨网传输不是一个量级)。
316
+ @override
317
+ async def upload_file(self, source_path: Path | str, target_path: str) -> None:
318
+ sandbox_id = self._require_sandbox()
319
+ rel = await self._stage_out()
320
+ try:
321
+ with open(source_path, "rb") as fh:
322
+ # guestd 写文件不建父目录(file_ops.go 只 O_CREATE)—— 中转目录
323
+ # 已由 _stage_out 建好,文件名是扁平的,不需要再建父目录。
324
+ await self._client.write_file_stream(sandbox_id, rel, fh)
325
+ await self._move(rel, target_path)
326
+ finally:
327
+ await self._unstage(rel)
328
+
329
+ @override
330
+ async def upload_dir(self, source_dir: Path | str, target_dir: str) -> None:
331
+ sandbox_id = self._require_sandbox()
332
+ root = Path(source_dir)
333
+ files = [p for p in sorted(root.rglob("*")) if p.is_file()]
334
+ if not files:
335
+ return
336
+ rel = await self._stage_out()
337
+ try:
338
+ # 一次性把所有需要的目录建出来:guestd 的写文件不建父目录,逐个文件
339
+ # 一次 exec 会慢得多(一个 environment/ 目录可能有几十上百个文件)。
340
+ # 这些是**相对路径** —— exec 的缺省 cwd 就是 workdir,正好对上。
341
+ dirs = sorted({str(PurePosixPath(rel) / p.relative_to(root).parent) for p in files})
342
+ await self._mkdirs(sandbox_id, dirs)
343
+ for path in files:
344
+ sub = path.relative_to(root)
345
+ with open(path, "rb") as fh:
346
+ await self._client.write_file_stream(sandbox_id, f"{rel}/{sub}", fh)
347
+ # 内容**并进**目标目录(不是把中转目录本身搬过去):Harbor 对
348
+ # upload_dir 的语义是「把 source 的内容放到 target」。
349
+ guest_root = await self._file_root()
350
+ await self._run(
351
+ f"mkdir -p {shlex.quote(target_dir)} && "
352
+ f"cp -a {guest_root}/{rel}/. {shlex.quote(target_dir)}/"
353
+ )
354
+ finally:
355
+ await self._unstage(rel)
356
+
357
+ @override
358
+ async def download_file(self, source_path: str, target_path: Path | str) -> None:
359
+ sandbox_id = self._require_sandbox()
360
+ rel = await self._stage_in(source_path)
361
+ try:
362
+ data = await self._client.read_file(sandbox_id, rel)
363
+ finally:
364
+ await self._unstage(rel)
365
+ local = Path(target_path)
366
+ local.parent.mkdir(parents=True, exist_ok=True)
367
+ local.write_bytes(data)
368
+
369
+ @override
370
+ async def download_dir(self, source_dir: str, target_dir: Path | str) -> None:
371
+ sandbox_id = self._require_sandbox()
372
+ rel = await self._stage_in(source_dir)
373
+ try:
374
+ local_root = Path(target_dir)
375
+ src_root = PurePosixPath(rel)
376
+ # 列目录是**单层**的(`12` §4.2:无递归语义)⇒ 自己走一遍广度优先,
377
+ # 并保留相对目录结构(轨迹里同名文件多,拍平会互相覆盖)。
378
+ queue = [src_root]
379
+ while queue:
380
+ current = queue.pop(0)
381
+ for entry in await self._client.list_files(sandbox_id, str(current)):
382
+ remote = current / entry["name"]
383
+ if entry.get("type") == "dir":
384
+ queue.append(remote)
385
+ continue
386
+ local_path = local_root / remote.relative_to(src_root)
387
+ local_path.parent.mkdir(parents=True, exist_ok=True)
388
+ local_path.write_bytes(await self._client.read_file(sandbox_id, str(remote)))
389
+ finally:
390
+ await self._unstage(rel)
391
+
392
+ # ---- 路径翻译(中转目录) ----------------------------------------
393
+ async def _file_root(self) -> str:
394
+ """文件 API 的根 = manifest `[run].workdir`(`06` §4.5)。
395
+
396
+ 用 `pwd` 问 guest 而不是读 manifest:manifest 不过 HTTP,provider 侧拿
397
+ 不到;而 exec 的缺省 cwd 恰好就是 workdir(`10` §3.9),问一次即可,
398
+ 不需要为 provider 新增接口。顺带在同一趟里验证**它可写** —— 写不了的话
399
+ 每次传输都会以 `io_error` 失败,而那个报错完全指不出「镜像的 workdir
400
+ 只读,要用 --writable-dir 重建」(`docs/plans/harbor-integration.md` §13.3)。
401
+ """
402
+ if self._root is None:
403
+ # 刻意不经 _run:它的通用报错指不出这里该给的那条修法。
404
+ result = await self._client.exec(
405
+ self._require_sandbox(),
406
+ "touch .rayrun-writecheck && rm -f .rayrun-writecheck && pwd",
407
+ timeout_seconds=60,
408
+ user="root",
409
+ )
410
+ root = result.stdout.strip()
411
+ if result.exit_code != 0 or not root.startswith("/"):
412
+ raise RuntimeError(
413
+ f"沙箱 workdir 不可写或不可用(exit={result.exit_code}):{result.stderr.strip()[:200]}\n"
414
+ " 任务镜像的 workdir 若不在默认可写的子树里(/usr /etc /var /opt /root /home /srv),"
415
+ "重建镜像时要声明:\n"
416
+ " images/build-task-image.sh … --writable-dir <workdir>\n"
417
+ " 见 docs/plans/harbor-integration.md §13.3 与 ADR-006 修订。"
418
+ )
419
+ self._root = root
420
+ return self._root
421
+
422
+ async def _stage_in(self, source: str) -> str:
423
+ """把 guest 内的绝对路径(文件或目录)复制进 workdir 下的中转目录,
424
+ 返回**相对路径**(可直接喂文件 API)。调用方负责 `_unstage`。
425
+
426
+ 复制失败(比如源目录不存在)时**在这里**收摊再抛:调用方的 finally 拿不到
427
+ 返回值,谁都不会替它清 —— 而这**不是**异常路径上的边角情况,
428
+ `download_dir("/logs/agent")`(agent 一个字都没写时)每次都会走到。
429
+ """
430
+ rel = await self._stage_out()
431
+ root = await self._file_root()
432
+ try:
433
+ await self._run(f"rm -rf {root}/{rel} && cp -a {shlex.quote(source)} {root}/{rel}")
434
+ except Exception:
435
+ try:
436
+ await self._unstage(rel)
437
+ except Exception: # noqa: BLE001 —— 清理失败不能盖掉原始错误
438
+ pass
439
+ raise
440
+ return rel
441
+
442
+ async def _stage_out(self) -> str:
443
+ """分配一个空的、位于 workdir 下的中转相对路径。
444
+
445
+ **只建父目录**(`STAGING_DIR` 本身),返回的路径**刻意保持不存在** ——
446
+ 中转路径是什么形状由调用方决定:`upload_file` 要在它上面放一个**文件**,
447
+ 而 guestd 的写文件是 `O_CREATE|O_TRUNC|O_WRONLY`
448
+ (`guestd/internal/guest/file_ops.go:43`),往一个已存在的**目录**上写会
449
+ `EISDIR` ⇒ 500 `file op io_error`。`upload_dir` / `_stage_in` 要的是目录,
450
+ 它们自己会 `mkdir -p`(前者经 `_mkdirs` 一次性建齐,后者靠 `cp -a` 顺带建)。
451
+
452
+ 名字带 uuid ⇒ 不会撞上上一轮遗留的中转路径,不需要先 `rm -rf`。
453
+ """
454
+ root = await self._file_root()
455
+ await self._run(f"mkdir -p {root}/{STAGING_DIR}")
456
+ return f"{STAGING_DIR}/{uuid.uuid4().hex[:12]}"
457
+
458
+ async def _unstage(self, rel: str) -> None:
459
+ await self._run(f"rm -rf {await self._file_root()}/{rel}")
460
+
461
+ async def _move(self, rel: str, target: str) -> None:
462
+ """把中转路径搬到 guest 内的绝对目标(含父目录)。"""
463
+ root = await self._file_root()
464
+ parent = str(PurePosixPath(target).parent)
465
+ mkdir = f"mkdir -p {shlex.quote(parent)} && " if parent not in ("", ".") else ""
466
+ await self._run(f"{mkdir}cp -a {root}/{rel} {shlex.quote(target)}")
467
+
468
+ # ------------------------------------------------------------ 内部
469
+ @staticmethod
470
+ def _require_env(name: str) -> str:
471
+ value = os.environ.get(name, "")
472
+ if not value:
473
+ raise ValueError(f"{name} 未设置(preflight 应已拦下;这里防直接构造)")
474
+ return value
475
+
476
+ def _require_sandbox(self) -> str:
477
+ if self._sandbox_id is None:
478
+ raise RuntimeError("沙箱尚未启动(先 start())")
479
+ return self._sandbox_id
480
+
481
+ async def _run(self, command: str, timeout_sec: int = 120):
482
+ """跑一条 guest 内命令,非 0 退出即抛(中转目录的搬运全部经这里)。"""
483
+ result = await self._client.exec(
484
+ self._require_sandbox(), command, timeout_seconds=timeout_sec, user="root"
485
+ )
486
+ if result.exit_code != 0:
487
+ raise RuntimeError(
488
+ f"guest 命令失败(exit={result.exit_code}):{command[:120]}\n {result.stderr.strip()[:200]}"
489
+ )
490
+ return result
491
+
492
+ def _image_ref(self) -> str:
493
+ prefix = os.environ.get(ENV_IMAGE_PREFIX, "")
494
+ version = os.environ.get(ENV_IMAGE_VERSION, "1")
495
+ return f"{prefix}{self.environment_name}@{version}"
496
+
497
+ def _sandbox_name(self) -> str:
498
+ """沙箱名:Rayrun 要求同客户内活跃名唯一且 ≤64 字符。
499
+
500
+ 由 Harbor 的 `session_id`(`{trial_name}__{role}__env`)派生 —— 它本身含
501
+ trial 的随机后缀,天然唯一;非 [A-Za-z0-9._-] 的字符换成 `-`。
502
+ """
503
+ safe = "".join(c if (c.isalnum() or c in "._-") else "-" for c in self.session_id)
504
+ return safe[:MAX_SANDBOX_NAME] or "harbor-trial"
505
+
506
+ def _egress(self) -> tuple[list[str], bool]:
507
+ """把 Harbor 的 NetworkPolicy 映射成 Rayrun 的 `egress`(`06` §4.3)。
508
+
509
+ - `no-network` → `allow=[]`(完全断网)
510
+ - `allowlist` → `allow=[...]`
511
+ - `public` → **`allow_all=true`**(显式档位,2026-09-27 起)。
512
+
513
+ 三种模式都**精确执行**,不做任何静默近似 —— 早先 Rayrun 没有「允许全部」
514
+ 档位时,PUBLIC 只能被拒绝;补上那一档之后,Harbor 的默认策略(PUBLIC)
515
+ 才真正跑得起来,且「这台沙箱开了全出网」在 Rayrun 的审计里看得见。
516
+ """
517
+ policy: NetworkPolicy = self.network_policy
518
+ if policy.network_mode == NetworkMode.NO_NETWORK:
519
+ return [], False
520
+ if policy.network_mode == NetworkMode.PUBLIC:
521
+ return [], True
522
+ return sorted(set(policy.allowed_hosts)), False
523
+
524
+ async def _require_image(self, image_ref: str) -> None:
525
+ """镜像必须已在 Rayrun 宿主机上(Rayrun 不做镜像构建,见类文档第 4 条)。"""
526
+ agents = await self._client.agents()
527
+ existing = {f"{a.get('name')}@{a.get('version')}" for a in agents}
528
+ if image_ref in existing:
529
+ return
530
+ name, _, version = image_ref.partition("@")
531
+ available = "、".join(sorted(existing)) or "(宿主机镜像目录为空)"
532
+ # 镜像里的 source 两种形态都要能照做:Dockerfile(本地可构建)与
533
+ # docker_image(预构建,任务只给了引用)。
534
+ source = (
535
+ f"--image {shlex.quote(self.task_env_config.docker_image)}"
536
+ if self.task_env_config.docker_image
537
+ else f"--dockerfile {self.environment_dir}/Dockerfile"
538
+ )
539
+ raise ValueError(
540
+ f"Rayrun 宿主机上没有镜像 {image_ref}。\n"
541
+ " 在**有 docker 的构建机**上产一张(离线 builder,见 docs/plans/harbor-integration.md §10):\n"
542
+ f" images/build-task-image.sh {source} \\\n"
543
+ f" --name {name} --version {version or '1'} --out /tmp/task-images \\\n"
544
+ f" {' '.join(f'--writable-dir {d}' for d in HONORED_WRITABLE_DIRS)}\n"
545
+ f" rsync -a --sparse /tmp/task-images/{image_ref}/ <宿主机>:$RAYRUN_IMAGES_DIR/\n"
546
+ f" {WRITABLE_HINT}\n"
547
+ f" 宿主机现有镜像:{available}"
548
+ )
549
+
550
+ async def _mkdirs(self, sandbox_id: str, dirs: list[str]) -> None:
551
+ quoted = " ".join(shlex.quote(d) for d in dirs)
552
+ result = await self._client.exec(sandbox_id, f"mkdir -p {quoted}", timeout_seconds=60)
553
+ if result.exit_code != 0:
554
+ raise RuntimeError(f"mkdir -p 失败(exit={result.exit_code}):{result.stderr[:200]}")
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.5
2
+ Name: harbor-rayrun
3
+ Version: 0.1.0
4
+ Summary: Harbor provider:把 Rayrun 沙箱云接成 Harbor 的评测环境
5
+ Project-URL: Homepage, https://github.com/RayrunOS/harbor-rayrun
6
+ Project-URL: Repository, https://github.com/RayrunOS/harbor-rayrun
7
+ License: Apache-2.0
8
+ License-File: LICENSE
9
+ Requires-Python: >=3.12
10
+ Requires-Dist: harbor<0.24,>=0.23.0
11
+ Requires-Dist: httpx>=0.27
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest-asyncio>=0.24; extra == 'test'
14
+ Requires-Dist: pytest>=8; extra == 'test'
15
+ Description-Content-Type: text/markdown
16
+
17
+ # harbor-rayrun —— Rayrun 的 Harbor provider
18
+
19
+ 把 **Rayrun 沙箱云**接成 [Harbor](https://docs.harborframework.com) 的评测环境:Harbor 每个
20
+ trial 的隔离环境由 provider 实现,本包把它落到 Rayrun 的 HTTP API 上。
21
+
22
+ 本仓库是 **独立仓库**(2026-11-15 从 Rayrun 主仓库的 `integrations/harbor-rayrun/` 拆出)。
23
+ 下文出现的 `docs/...`、`images/...`、`CLAUDE.md` 都指
24
+ [Rayrun 主仓库](https://github.com/RayrunOS/RayrunOS) 内的路径:设计真相源
25
+ `docs/plans/harbor-integration.md`(M3),外部契约取证见 `docs/15` §2.1。
26
+
27
+ ## 它负责什么
28
+
29
+ | Harbor 契约 | 落到 Rayrun |
30
+ |---|---|
31
+ | `start(force_build)` | 校验镜像在宿主机上 → `POST /v1/sandboxes`(201 即 Running) |
32
+ | `stop(delete=True/False)` | `DELETE /sandboxes/{id}` / `POST …/stop`(**ADR-005 冷停**:留盘、停表、IP 不变) |
33
+ | `exec(command, cwd, env, timeout_sec, user)` | `POST /sandboxes/{id}/exec`(**同步**;M1 新落的原语,ADR-007) |
34
+ | `upload_file` / `upload_dir` | `PUT /sandboxes/{id}/files/{path}`(先 `mkdir -p` —— guestd 写文件不建父目录) |
35
+ | `download_file` / `download_dir` | `GET …/files/{path}`;列目录是**单层**的,provider 自己走递归 |
36
+ | `is_dir` / `is_file` | 用基类默认实现(走 `exec` 的 `test -d/-f`),不覆写 |
37
+
38
+ **它不负责什么**:不构建镜像(镜像由平台 builder `POST /v1/builds` 或离线 builder 产出,见下)、
39
+ 不做 SSH/流式(`stream` 档位未开)、不支持 GPU / TPU / Windows / docker-compose。
40
+
41
+ ## 用法
42
+
43
+ ```bash
44
+ # 1. 装(Harbor 与 provider 在同一个 Python 环境里)
45
+ uv pip install harbor-rayrun
46
+ # 或从源码:uv pip install git+ssh://git@github.com/RayrunOS/harbor-rayrun.git
47
+ # 或在本仓库根目录:uv pip install .
48
+
49
+ # 2. 凭证(沿用 CLI 的 env 名,docs/07 §2.5)
50
+ # 托管环境(2026-10-28 起):https://api.rayrunos.com/v1
51
+ # 自托管 / dev: http://<daemon 主机>:8080/v1
52
+ # ⚠️ dashboard.rayrunos.com/v1 已 410,不是 API 入口
53
+ export RAYRUN_API_URL=https://api.rayrunos.com/v1
54
+ export RAYRUN_TOKEN=rayrun_<token_id>_<secret>
55
+
56
+ # 3. 跑(provider 用 --env 的 module:Class 入口,不需要改 Harbor 上游)
57
+ harbor run -t <task> -a oracle -e harbor_rayrun:RayrunEnvironment
58
+ ```
59
+
60
+ 镜像名默认取 `<environment_name>@1`,可用两个环境变量覆盖:
61
+ `RAYRUN_IMAGE_PREFIX`(前缀,例如 `tb-`)与 `RAYRUN_IMAGE_VERSION`。
62
+
63
+ ## 两条必须先知道的前提
64
+
65
+ **1. 镜像要先在目标 Rayrun 环境里备好。** 两条路:
66
+
67
+ - **线上/托管**:用平台 builder 现场构建(`POST /v1/builds`,ADR-024),产物是
68
+ `<name>@<version>`,跑完删除。**不要把本地 `rootfs.ext4` / 镜像目录传到线上**
69
+ (`docs/howto/harbor-on-rayrun.md` §1.2;CLAUDE.md §6)。
70
+ - **本地 dev**:用离线 builder `images/build-task-image.sh` 产出到本机
71
+ `RAYRUN_IMAGES_DIR`,见 `docs/plans/harbor-integration.md` §10。
72
+
73
+ 镜像缺失时 `start()` 会直接报错并附一条**可直接复制**的构建命令,不猜、不静默。
74
+
75
+ **构建时带 `--root-writable`**(旧的「三个 `--writable-dir`」方案已被它取代):
76
+
77
+ ```bash
78
+ images/build-task-image.sh --dockerfile <任务的 Dockerfile> \
79
+ --name <name> --version 1 --out /tmp/task-images --root-writable
80
+ ```
81
+
82
+ 这不是可选优化。沙箱里**清单外的目录一律只读**(ADR-006:可写面是固定 7 个子树
83
+ `/usr /etc /var /opt /root /home /srv` + `/workspace` 盘),而 Harbor 的
84
+ `/logs`(reward 写在这里)`/tests` `/solution` 全在清单外 ⇒ 少了可写面,verifier 连
85
+ `/logs/verifier/reward.txt` 都写不出来,症状只是「reward 文件不存在」。`--root-writable`
86
+ 用 overlay + pivot_root 让 `/` 与 docker 语义一致;任务镜像自己的 `WORKDIR`(典型 `/app`)
87
+ 也由 guestd 按 manifest 的 `[run].workdir` 自动补挂。见 `docs/plans/harbor-integration.md`
88
+ §13.3 / §13.11 与 ADR-006 修订。
89
+
90
+ **2. 网络策略的映射是精确的**(2026-09-27 起):
91
+
92
+ | Harbor `NetworkPolicy` | Rayrun `egress` |
93
+ |---|---|
94
+ | `no-network` | `allow: []`(完全断网) |
95
+ | `allowlist` | `allow: [...]`(域名精确匹配 + IPv4 CIDR) |
96
+ | `public`(**Harbor 的默认**) | `allow_all: true`(显式档位,Rayrun 侧会进审计) |
97
+
98
+ 三者都**精确执行**、不做静默近似。`public` 那一档曾经只能被拒绝(Rayrun 当时没有「允许
99
+ 全部」),为此产品侧补了 `egress.allow_all`(`06` §4.3)—— 于是默认任务也能跑,而「这台
100
+ 沙箱开了全出网」在 Rayrun 的 journal(`sandbox.created.payload.egress.allow_all`)里看得见。
101
+
102
+ ## 依赖
103
+
104
+ - 运行时:`harbor>=0.23,<0.24`(提供 `BaseEnvironment` 抽象面)+ `httpx`(打 Rayrun API)。
105
+ **刻意不引 Rayrun 的生成式 SDK** —— provider 只用 8 个端点,薄客户端足够;SDK 生成是独立
106
+ 立项(`docs/15` §10-Q7),将来若落地,换掉 `client.py` 即可、provider 契约不变。
107
+ - 开发/测试:`pytest` + `pytest-asyncio`(见 `[project.optional-dependencies]`)。
108
+
109
+ ## 测试
110
+
111
+ ```bash
112
+ make test # 首次会建 .venv 并从 PyPI 镜像装依赖(需要网络)
113
+ ```
114
+
115
+ 单测用 `httpx.MockTransport` 打桩,覆盖的都是**会静默出错**的地方:能力虚报、PUBLIC 被悄悄
116
+ 降级、镜像缺失没有可操作提示、超时没有可判定退出码、写文件不建父目录。基线用**真**
117
+ `BaseEnvironment`(不是 stub),所以能力声明的字段名是**对着装好的 harbor 版本**核过的 ——
118
+ `main` 分支与 0.23.0 的 `EnvironmentCapabilities` 字段并不相同(0.23.0 没有 `stream`),
119
+ pydantic 会静默忽略未知字段,写错不会报错。
120
+
121
+ ## 与真机验收的分工
122
+
123
+ 单测钉 provider 的判断;**真沙箱链路**(建沙箱 → exec → 文件 → 销毁)归
124
+ `docs/plans/harbor-integration.md` 的 **M4**(Harbor 端到端 reward 比对)。
@@ -0,0 +1,7 @@
1
+ harbor_rayrun/__init__.py,sha256=K9qxA_iWmkSR0WK5jdiUzGwm5LvI6O6cpzuGcB4OqC4,783
2
+ harbor_rayrun/client.py,sha256=20tcXU9-7vmLZY8TznoldU3zVJhDXHxHdXUt2I75NkM,10314
3
+ harbor_rayrun/environment.py,sha256=WMQveYOlq64TQMMz0m-VMIUOOUiMr9kr-6ay-aRpOSM,28020
4
+ harbor_rayrun-0.1.0.dist-info/METADATA,sha256=e7r6B4C_9geSO85Yx80FW3Nbpm4Bbjoos4-4nF6Yjng,6544
5
+ harbor_rayrun-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
6
+ harbor_rayrun-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
7
+ harbor_rayrun-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.