API 约定
API 约定
统一包络(仅 HTTP)
所有 HTTP 业务接口(含鉴权失败、参数错误、404/405)均走 { code, message, data } 包络;成功与业务失败 HTTP 状态多为 200,以包络里的 code 为准(路由不存在/方法不允许仍为 404/405 状态码 + 同结构 JSON)。
| 字段 | 类型 | 说明 |
|---|---|---|
code |
int | 0 成功;400 参数错误;401 未授权;404 任务/路由不存在;429 繁忙;-1 未映射内部错误等 |
message |
string | 提示文案 |
data |
object | null | 成功时的业务体;失败时通常省略 |
{ "code": 0, "message": "成功", "data": { } }
实现:common/response.Setup() + handler 侧 OkJsonCtx / ErrorCtx;解析失败经 response.Parse 固定为 code=400。
gRPC 无该包络,直接返回 protobuf 消息;错误用 gRPC status(如 Unauthenticated / NotFound / ResourceExhausted)。
接口一览
| 协议 | 方法 | 路径 / RPC | 鉴权 | 说明 |
|---|---|---|---|---|
| HTTP | GET | /api/v1/health |
否 | 健康与池状态 |
| HTTP | POST | /api/v1/run |
Auth 开启时 | 同步执行 |
| HTTP | POST | /api/v1/tasks |
Auth 开启时 | 异步提交 |
| HTTP | GET | /api/v1/tasks/:id |
Auth 开启时 | 查询任务 |
| gRPC | — | sandbox.Sandbox/Health |
否 | 同健康检查 |
| gRPC | — | sandbox.Sandbox/Run |
Auth 开启时 | 同步执行 |
| gRPC | — | sandbox.Sandbox/Submit |
Auth 开启时 | 异步提交 |
| gRPC | — | sandbox.Sandbox/GetTask |
Auth 开启时 | 查询任务 |
HTTP JSON 字段名与下表一致;gRPC 为同义 snake_case(见 pb/sandbox.proto)。FileContent.content:HTTP 为字符串,gRPC 为 bytes。
公共类型
Limits(运行步资源限额;0 / 省略表示用服务端 DefaultLimits)
编译步(steps 除最后一步外)不使用请求里的 limits,而使用 yaml Sandbox.CompileLimits(默认更宽:60s CPU / 120s 墙钟 / 1GiB / 256 进程等),避免过紧的运行题限把 gcc/go build 卡死。编译仍走命名空间 + seccomp + 默认禁网。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
cpu_ms |
int64 | 否 | CPU 时间上限(毫秒) |
clock_ms |
int64 | 否 | 墙钟时间上限(毫秒) |
memory_kb |
int64 | 否 | 内存上限(KiB) |
stack_kb |
int64 | 否 | 栈上限(KiB) |
output_kb |
int64 | 否 | stdout+stderr 捕获上限(KiB) |
proc |
int64 | 否 | 进程数上限 |
network |
bool | 否 | 是否允许网络(仅运行步;编译步始终禁网) |
file_size_kb |
int64 | 否 | 单文件大小上限(KiB) |
FileContent
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
path |
string | 是 | 相对工作目录的路径 |
content |
string / bytes | 是 | 文件内容 |
多测点(test_case_dir)
运维将测点同步到节点 Sandbox.TestCaseRoot(默认 WorkDir/cases),请求只传相对路径目录,不传上千条清单:
TestCaseRoot/contest-a/p100/
info.json # 可选
1.in
2.in
- 有
info.json:按cases[].id+input顺序跑。 - 无则:扫
{数字}.in与其余*.in,按数字序再名字序;id为去后缀文件名。 - 上限:
Sandbox.MaxTestCases(默认 2048)。 - 明细写入
Sandbox.OutputRoot(默认WorkDir/outputs):{output_dir}/cases.jsonl与各点*.stdout/*.stderr。
响应结构(统一 steps[],无顶层计量)
RunResult:流水线结论 + 逐步结果;多测另带汇总字段。计量在 steps[i]。
沙箱不做判题:对错由业务根据落盘 stdout / status 自行判定。
RunResult: status, message, steps[],
case_count?, case_finished?, cases_file?, output_dir? # 仅多测
StepResult: index, name, cached, + ExecResult 同名字段平铺, cases[]
ExecResult: id, status, exit_code, signal, time_ms, wall_time_ms,
memory_kb, stdout, stderr, message, oom_killed
| 场景 | step 平铺计量 | cases |
其它 |
|---|---|---|---|
| 编译 / 非最后一步 | 该次执行实测 | 恒为 [] |
|
| 编译缓存命中 | 全 0,status=Accepted,cached=true |
[] |
|
单测(仅 stdin) |
该次运行实测 | 固定 1 条 | |
多测(test_case_dir) |
各点汇总(CPU/内存峰值、墙钟求和) | 恒 [] |
读 cases_file / output_dir |
ExecResult 字段
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 测点 ID;step 平铺处恒为空串 |
status |
string | 见下方状态枚举 |
exit_code |
int | 进程退出码 |
signal |
int | 终止信号(若有) |
time_ms |
int64 | CPU 时间(ms);多测汇总为各点峰值 |
wall_time_ms |
int64 | 墙钟时间(ms);多测汇总为各点之和 |
memory_kb |
int64 | 峰值内存(KiB);多测汇总为各点峰值 |
stdout |
string | 标准输出 |
stderr |
string | 标准错误 |
message |
string | 补充说明 |
oom_killed |
bool | 是否因 cgroup OOM 被杀 |
status 枚举
| 值 | 含义 |
|---|---|
Accepted |
正常结束(exit 0,未超限) |
TimeLimitExceeded |
CPU 或墙钟超时 |
MemoryLimitExceeded |
内存超限 / OOM |
OutputLimitExceeded |
输出超限 |
NonZeroExit |
非零退出 |
Signalled |
被信号终止 |
DangerousSyscall |
seccomp 拦截危险调用 |
FileError |
文件/路径类错误 |
InternalError |
内部错误 |
GET /api/v1/health
请求:无 query / body。
响应 data:
| 字段 | 类型 | 说明 |
|---|---|---|
ok |
bool | 服务是否可用 |
cgroup |
bool | cgroup 是否可用 |
cgroup_version |
string | 可选,cgroup 版本信息 |
cgroup_root |
string | 可选,cgroup 根路径 |
pool_size |
int | 工作池大小 |
max_concurrent |
int | 最大并发 |
in_flight |
int64 | 当前进行中任务数 |
seccomp |
bool | 是否启用 seccomp |
cache_enabled |
bool | 编译缓存是否开启 |
cache_hits |
int64 | 编译缓存命中次数 |
cache_misses |
int64 | 编译缓存未命中次数 |
POST /api/v1/run(同步)
请求 body:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
steps |
Step[] |
是 | 执行步骤;最后一步为运行,前面为编译(可为空) |
files |
FileContent[] |
否 | 写入工作目录的文件(源码等) |
language |
string | 否 | 仅作编译缓存命名空间,不解析工具链 |
artifacts |
string[] | 否 | 编译成功后写入缓存的相对路径;空则不缓存 |
stdin |
string | 否 | 单测标准输入(无 test_case_dir 时) |
test_case_dir |
string | 否 | 多测目录,相对 TestCaseRoot;与清单式 test_cases 已移除 |
stop_on_fail |
bool | 否 | 多测时遇非 Accepted 是否停止后续测点 |
env |
string[] | 否 | 请求级环境变量,与步骤 env 合并 |
limits |
Limits |
否 | 仅运行步资源限额;编译步使用服务端 Sandbox.CompileLimits |
Step
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
command |
string[] | 是 | argv;command[0] 会 LookPath(./ 或含 / 不解析) |
name |
string | 否 | 缺省时编译步为 compile,运行步为 run |
env |
string[] | 否 | 该步附加环境变量 |
响应 data:status + message + steps[];多测另含 case_count / case_finished / cases_file / output_dir。计量只在各 steps[i] 内。
示例(多测;需先把测点放到 TestCaseRoot/contest-a/p100/):
curl -s localhost:8888/api/v1/run -H 'Content-Type: application/json' -d '{
"language": "cpp",
"files": [{"path": "main.cpp", "content": "#include <bits/stdc++.h>\nusing namespace std;int main(){long long a,b;cin>>a>>b;cout<<a+b<<endl;}\n"}],
"steps": [
{"name": "compile", "command": ["g++", "-O2", "-std=c++17", "main.cpp", "-o", "main"]},
{"name": "run", "command": ["./main"]}
],
"artifacts": ["main"],
"test_case_dir": "contest-a/p100",
"stop_on_fail": true,
"limits": {"cpu_ms": 2000, "clock_ms": 5000, "memory_kb": 262144}
}'
响应 data 形状(多测时运行步 cases=[],明细在 cases_file):
{
"status": "Accepted",
"message": "",
"case_count": 2,
"case_finished": 2,
"cases_file": "run-7f3a2c/cases.jsonl",
"output_dir": "run-7f3a2c",
"steps": [
{
"index": 0, "name": "compile", "cached": false,
"status": "Accepted", "exit_code": 0, "signal": 0,
"time_ms": 180, "wall_time_ms": 210, "memory_kb": 120000,
"stdout": "", "stderr": "", "message": "", "oom_killed": false,
"cases": []
},
{
"index": 1, "name": "run", "cached": false,
"status": "Accepted", "exit_code": 0, "signal": 0,
"time_ms": 12, "wall_time_ms": 25, "memory_kb": 9000,
"stdout": "", "stderr": "", "message": "", "oom_killed": false,
"cases": []
}
]
}
OutputRoot/run-7f3a2c/cases.jsonl 每行一条测点计量,并含 stdout_file / stderr_file 相对路径。
POST /api/v1/tasks(异步提交)
请求 body:与 POST /run 相同(不再携带回调地址;出站目标仅在 yaml Callbacks 配置)。
响应 data:
| 字段 | 类型 | 说明 |
|---|---|---|
task_id |
string | 任务 ID |
state |
string | 初始一般为 pending |
GET /api/v1/tasks/:id(查询任务)
路径参数:
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 即 task_id |
响应 data:
| 字段 | 类型 | 说明 |
|---|---|---|
task_id |
string | 任务 ID |
state |
string | pending | running | done | failed |
error |
string | 失败时的错误信息(可选) |
created_at |
int64 | 创建时间 Unix 秒 |
updated_at |
int64 | 更新时间 Unix 秒 |
result |
object | 终态且成功执行时的运行结果,结构同 POST /run 的 data |
状态机:pending → running → done | failed。轮询与回调可并存。终态任务默认保留 7 天(Sandbox.TaskTTLSeconds,默认 604800),到期后才 GC,期间 GET /tasks/:id 仍可查到结果。
异步回调(出站)
任务进入 done / failed 且 yaml Callbacks 中配置了有效目标时,沙箱向全部目标并行 POST(各目标独立密钥/重试,互不影响)。签名算法与入站相同,密钥与入站 Auth.AccessKeys 分离。
全局限制在途投递数(默认 64),避免「任务洪峰 × 多 URL」打满出站连接。
请求头
| Header | 说明 |
|---|---|
Content-Type |
application/json |
Idempotency-Key |
等于 task_id(业务侧幂等键) |
X-Sandbox-Task-Id |
任务 ID |
X-Access-Key / X-Timestamp / X-Nonce / X-Sign |
该目标的出站 HMAC |
Body
| 字段 | 类型 | 说明 |
|---|---|---|
task_id |
string | 任务 ID |
state |
string | done 或 failed |
error |
string | 可选 |
result |
object | 可选,结构同运行结果 |
业务侧返回 2xx 视为成功;否则按该目标的 MaxRetries / RetryBackoffMs 退避重试。默认拒私网;本地联调设 AllowPrivateIP: true。回调是通知通道,需要强一致时可再 GET /tasks/:id 复核。
gRPC
# 需 grpcurl;Auth 开启时还须带 metadata 签名
grpcurl -plaintext localhost:8889 list
grpcurl -plaintext -d '{"files":[{"path":"main.c","content":"#include <stdio.h>\nint main(){puts(\"hi\");}\n"}],"steps":[{"name":"compile","command":["gcc","-O2","main.c","-o","main"]},{"name":"run","command":["./main"]}],"artifacts":["main"]}' \
localhost:8889 sandbox.Sandbox/Run
字段与 HTTP 一致(GetTask 返回 created_at / updated_at)。完整定义见 pb/sandbox.proto。