mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 20:12:09 +00:00
全栈安全加固、性能提速与架构演进:连接池与沙箱止血、双路混合检索、多版本归档及前端分包治理
- 第一批(安全加固与生命周期止血):隔离测试宿主机密钥与 .env 泄漏;修复 SQLite 取消查询挂死、:memory: 连接池报错与 EngineCache 世代竞态;补齐运行与评测删除时的 Checkpointer 线程、Artifact 磁盘文件与无主附件回收;加固三套沙箱路径越界、进程组清理与 Seatbelt 数据目录隔离,封堵 SSRF IPv4-mapped IPv6 元数据绕过与响应体 OOM;修复推理模型多轮 reasoning_content、结构化输出 Token 漏计与缓存折扣计费 - 第二批(数据库/集群/问数提速与首屏分包):补齐核心表索引与 SQLite WAL 调优,列表接口按需 defer 大列,收敛启动扫描并打通跨进程 slot 槽位唤醒;优化跨源全局选表、哈希桶数字绑定、流式 CSV 导入及多方言分析算子;剥离首屏静态引入的 @xyflow/react 与全局样式(首屏主包降至 461.59 kB),补充流式 rAF 攒批、Store 精细订阅与表单无障碍绑定 - 第三批(架构演进与工程自动化闭环):记录库超挂接阈值历史版本自动增量归档至单库多表(解除 SQLite 10 库 ATTACH 硬上限);重构知识库为词面 BM25 与全集合归一化向量矩阵真·双路独立召回并支持游标分批重建;升级多方言感知 SQL 模板词法分析(E'...' / $$...$$ / q'[...]' / # / 嵌套注释)与 Decimal / bool / NaN 参数校验,消除 ledger 层对 api 路由层的反向依赖;拆分 3101 行 CaliberDrafts 页面为 7 个领域子模块,消除检查脚本本机路径硬编码并新增 GitHub Actions CI 门禁与三批回归测试套件
This commit is contained in:
@@ -184,58 +184,82 @@ class BubblewrapSandbox(Sandbox):
|
||||
cmd_prefix = cmd_prefix if cmd_prefix is not None else interpreter.python_argv()
|
||||
entry = entry_name(ext)
|
||||
cmd = [*cmd_prefix, f"/work/{entry}"]
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time() * 1000)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not str(target).startswith(str(workdir.resolve())):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
argv = self._argv(workdir, limits, cmd, env)
|
||||
|
||||
started = time.perf_counter()
|
||||
ephemeral = not session_id
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time() * 1000)}-{os.urandom(4).hex()}")
|
||||
work_root = workdir.resolve()
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
# bwrap 自己也只拿最小的环境:沙箱里的进程靠上面的 --setenv,不从这里继承
|
||||
env={"PATH": os.environ.get("PATH", "/usr/bin:/bin")},
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
try:
|
||||
os.killpg(os.getpgid(proc.pid), 9)
|
||||
except (ProcessLookupError, PermissionError):
|
||||
proc.kill()
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False, backend=self.name, timed_out=True, exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 起进程和收输出在同一个 try 里。首行只说原因,类名和 errno 这些留给日志
|
||||
logger.warning("%s 沙箱进程出错:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"沙箱进程出错:{describe_exception(e)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not target.is_relative_to(work_root):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
code_ = proc.returncode or 0
|
||||
return ExecResult(
|
||||
ok=code_ == 0,
|
||||
exit_code=code_,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
truncated=t1 or t2,
|
||||
timed_out=code_ in (-24, 152),
|
||||
backend=self.name,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
files_written=[p.name for p in workdir.iterdir() if p.is_file() and not p.name.startswith(ENTRY_PREFIX)][:50],
|
||||
)
|
||||
argv = self._argv(workdir, limits, cmd, env)
|
||||
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
# bwrap 自己也只拿最小的环境:沙箱里的进程靠上面的 --setenv,不从这里继承
|
||||
env={"PATH": os.environ.get("PATH", "/usr/bin:/bin")},
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.warning("%s 沙箱进程出错:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"沙箱进程出错:{describe_exception(e)}")
|
||||
|
||||
try:
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False, backend=self.name, timed_out=True, exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
if getattr(proc, "returncode", 0) is None:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
logger.warning("%s 沙箱进程出错:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"沙箱进程出错:{describe_exception(e)}")
|
||||
except BaseException:
|
||||
if getattr(proc, "returncode", 0) is None:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
raise
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
code_ = proc.returncode or 0
|
||||
return ExecResult(
|
||||
ok=code_ == 0,
|
||||
exit_code=code_,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
truncated=t1 or t2,
|
||||
timed_out=code_ in (-24, 152),
|
||||
backend=self.name,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
files_written=[p.name for p in workdir.iterdir() if p.is_file() and not p.name.startswith(ENTRY_PREFIX)][:50],
|
||||
)
|
||||
finally:
|
||||
if ephemeral:
|
||||
root_resolved = self._root.resolve()
|
||||
if work_root != root_resolved and work_root.is_relative_to(root_resolved):
|
||||
shutil.rmtree(workdir, ignore_errors=True)
|
||||
|
||||
@staticmethod
|
||||
def _kill(proc: asyncio.subprocess.Process) -> None:
|
||||
try:
|
||||
os.killpg(os.getpgid(proc.pid), 9)
|
||||
except (ProcessLookupError, PermissionError):
|
||||
proc.kill()
|
||||
|
||||
async def cleanup(self, session_id: str) -> None:
|
||||
await asyncio.to_thread(shutil.rmtree, self._session_dir(session_id), True)
|
||||
|
||||
@@ -107,73 +107,90 @@ class LocalSandbox(Sandbox):
|
||||
ok=False, backend=self.name, error=f"本机找不到 {argv[0]}"
|
||||
)
|
||||
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time()*1000)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not str(target).startswith(str(workdir.resolve())):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
# 只透传最小环境变量,避免把宿主机的 API key 泄漏给被执行的代码
|
||||
child_env = {
|
||||
"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
|
||||
"HOME": str(workdir),
|
||||
"LANG": "en_US.UTF-8",
|
||||
"PYTHONDONTWRITEBYTECODE": "1",
|
||||
**(env or {}),
|
||||
}
|
||||
|
||||
started = time.perf_counter()
|
||||
ephemeral = not session_id
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time() * 1000)}-{os.urandom(4).hex()}")
|
||||
work_root = workdir.resolve()
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
cwd=str(workdir),
|
||||
env=child_env,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 首行只说原因,类名和 errno 这些留给日志
|
||||
logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"无法启动进程:{describe_exception(e)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not target.is_relative_to(work_root):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
try:
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
# 只透传最小环境变量,避免把宿主机的 API key 泄漏给被执行的代码
|
||||
child_env = {
|
||||
"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
|
||||
"HOME": str(workdir),
|
||||
"LANG": "en_US.UTF-8",
|
||||
"PYTHONDONTWRITEBYTECODE": "1",
|
||||
**(env or {}),
|
||||
}
|
||||
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
os.killpg(os.getpgid(proc.pid), 9)
|
||||
except (ProcessLookupError, PermissionError):
|
||||
proc.kill()
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False,
|
||||
backend=self.name,
|
||||
timed_out=True,
|
||||
exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
)
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
cwd=str(workdir),
|
||||
env=child_env,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 首行只说原因,类名和 errno 这些留给日志
|
||||
logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"无法启动进程:{describe_exception(e)}")
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
written = [
|
||||
p.name
|
||||
for p in workdir.iterdir()
|
||||
if p.is_file() and not p.name.startswith(ENTRY_PREFIX)
|
||||
][:50]
|
||||
return ExecResult(
|
||||
ok=proc.returncode == 0,
|
||||
exit_code=proc.returncode or 0,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
truncated=t1 or t2,
|
||||
backend=self.name,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
files_written=written,
|
||||
)
|
||||
try:
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False,
|
||||
backend=self.name,
|
||||
timed_out=True,
|
||||
exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
)
|
||||
except BaseException:
|
||||
if proc.returncode is None:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
raise
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
written = [
|
||||
p.name
|
||||
for p in workdir.iterdir()
|
||||
if p.is_file() and not p.name.startswith(ENTRY_PREFIX)
|
||||
][:50]
|
||||
return ExecResult(
|
||||
ok=proc.returncode == 0,
|
||||
exit_code=proc.returncode or 0,
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
truncated=t1 or t2,
|
||||
backend=self.name,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
files_written=written,
|
||||
)
|
||||
finally:
|
||||
if ephemeral:
|
||||
root_resolved = self._root.resolve()
|
||||
if work_root != root_resolved and work_root.is_relative_to(root_resolved):
|
||||
shutil.rmtree(workdir, ignore_errors=True)
|
||||
|
||||
@staticmethod
|
||||
def _kill(proc: asyncio.subprocess.Process) -> None:
|
||||
try:
|
||||
os.killpg(os.getpgid(proc.pid), 9)
|
||||
except (ProcessLookupError, PermissionError):
|
||||
proc.kill()
|
||||
|
||||
async def cleanup(self, session_id: str) -> None:
|
||||
path = self._session_dir(session_id)
|
||||
|
||||
@@ -78,6 +78,7 @@ def build_policy(workdir: Path, *, language: str, network: bool) -> str:
|
||||
"""
|
||||
work = _real(workdir)
|
||||
home = _real(Path.home())
|
||||
data = _real(settings.data_dir)
|
||||
lines = [
|
||||
"(version 1)",
|
||||
"(allow default)",
|
||||
@@ -98,8 +99,9 @@ def build_policy(workdir: Path, *, language: str, network: bool) -> str:
|
||||
' (literal "/dev/stderr") (literal "/dev/dtracehelper"))',
|
||||
'(allow file-write* (subpath "/private/var/folders"))', # 临时目录,Python 要用
|
||||
"",
|
||||
";; ---- 读:家目录挡住,放行工作区和解释器 ----",
|
||||
";; ---- 读:家目录与平台数据目录挡住,放行工作区和解释器 ----",
|
||||
f'(deny file-read* (subpath "{home}"))',
|
||||
*([f'(deny file-read* (subpath "{data}"))'] if data != home else []),
|
||||
f'(allow file-read* (subpath "{work}"))',
|
||||
]
|
||||
for root in _interpreter_roots(language):
|
||||
@@ -194,83 +196,99 @@ class SeatbeltSandbox(Sandbox):
|
||||
|
||||
ext, cmd_of = _LANG[language]
|
||||
entry = entry_name(ext)
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time() * 1000)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not str(target).startswith(str(workdir.resolve())):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
self._policies.mkdir(parents=True, exist_ok=True)
|
||||
policy_file = self._policies / f"{workdir.name}-{language}.sb"
|
||||
atomic.write_text(policy_file, build_policy(workdir, language=language, network=limits.network))
|
||||
|
||||
argv = [SANDBOX_EXEC, "-f", str(policy_file), *cmd_of(), entry]
|
||||
|
||||
# HOME 指向工作区:既挡住家目录,又让解释器的缓存有地方落
|
||||
child_env = {
|
||||
"PATH": "/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin",
|
||||
"HOME": str(workdir),
|
||||
"TMPDIR": str(workdir),
|
||||
"LANG": "en_US.UTF-8",
|
||||
"PYTHONDONTWRITEBYTECODE": "1",
|
||||
"PYTHONUNBUFFERED": "1",
|
||||
**(env or {}),
|
||||
}
|
||||
|
||||
started = time.perf_counter()
|
||||
ephemeral = not session_id
|
||||
workdir = self._session_dir(session_id or f"tmp-{int(time.time() * 1000)}-{os.urandom(4).hex()}")
|
||||
work_root = workdir.resolve()
|
||||
policy_file: Path | None = None
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
cwd=str(workdir),
|
||||
env=child_env,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 首行只说原因,类名和 errno 这些留给日志
|
||||
logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"没能启动进程:{describe_exception(e)}")
|
||||
atomic.write_text(workdir / entry, code)
|
||||
|
||||
for rel, content in (files or {}).items():
|
||||
target = (workdir / rel).resolve()
|
||||
if not target.is_relative_to(work_root):
|
||||
return ExecResult(ok=False, backend=self.name, error=f"非法文件路径:{rel}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
atomic.write_text(target, content)
|
||||
|
||||
self._policies.mkdir(parents=True, exist_ok=True)
|
||||
policy_file = self._policies / f"{workdir.name}-{language}.sb"
|
||||
atomic.write_text(policy_file, build_policy(workdir, language=language, network=limits.network))
|
||||
|
||||
argv = [SANDBOX_EXEC, "-f", str(policy_file), *cmd_of(), entry]
|
||||
|
||||
# HOME 指向工作区:既挡住家目录,又让解释器的缓存有地方落
|
||||
child_env = {
|
||||
"PATH": "/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin",
|
||||
"HOME": str(workdir),
|
||||
"TMPDIR": str(workdir),
|
||||
"LANG": "en_US.UTF-8",
|
||||
"PYTHONDONTWRITEBYTECODE": "1",
|
||||
"PYTHONUNBUFFERED": "1",
|
||||
**(env or {}),
|
||||
}
|
||||
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*argv,
|
||||
cwd=str(workdir),
|
||||
env=child_env,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
preexec_fn=self._preexec(limits),
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 首行只说原因,类名和 errno 这些留给日志
|
||||
logger.warning("%s 沙箱起进程失败:%s", self.name, raw(e))
|
||||
return ExecResult(ok=False, backend=self.name, error=f"没能启动进程:{describe_exception(e)}")
|
||||
|
||||
try:
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False,
|
||||
backend=self.name,
|
||||
timed_out=True,
|
||||
exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
)
|
||||
except BaseException:
|
||||
if proc.returncode is None:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
raise
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
code_ = proc.returncode or 0
|
||||
# CPU 时间用尽是 SIGXCPU(24),归到超时里更好理解
|
||||
timed_out = code_ in (-24, 152)
|
||||
written = [
|
||||
p.name for p in workdir.iterdir()
|
||||
if p.is_file() and not p.name.startswith(ENTRY_PREFIX)
|
||||
][:50]
|
||||
|
||||
try:
|
||||
out, err = await asyncio.wait_for(proc.communicate(), timeout=limits.timeout)
|
||||
except asyncio.TimeoutError:
|
||||
self._kill(proc)
|
||||
await proc.wait()
|
||||
return ExecResult(
|
||||
ok=False,
|
||||
ok=code_ == 0,
|
||||
exit_code=code_,
|
||||
stdout=stdout,
|
||||
stderr=stderr if not timed_out else (stderr or "CPU 时间超限,已终止"),
|
||||
truncated=t1 or t2,
|
||||
timed_out=timed_out,
|
||||
backend=self.name,
|
||||
timed_out=True,
|
||||
exit_code=124,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
error=f"执行超过 {limits.timeout} 秒,已终止",
|
||||
files_written=written,
|
||||
)
|
||||
|
||||
stdout, t1 = truncate(out.decode(errors="replace"), limits.max_output)
|
||||
stderr, t2 = truncate(err.decode(errors="replace"), limits.max_output)
|
||||
code_ = proc.returncode or 0
|
||||
# CPU 时间用尽是 SIGXCPU(24),归到超时里更好理解
|
||||
timed_out = code_ in (-24, 152)
|
||||
written = [
|
||||
p.name for p in workdir.iterdir()
|
||||
if p.is_file() and not p.name.startswith(ENTRY_PREFIX)
|
||||
][:50]
|
||||
|
||||
return ExecResult(
|
||||
ok=code_ == 0,
|
||||
exit_code=code_,
|
||||
stdout=stdout,
|
||||
stderr=stderr if not timed_out else (stderr or "CPU 时间超限,已终止"),
|
||||
truncated=t1 or t2,
|
||||
timed_out=timed_out,
|
||||
backend=self.name,
|
||||
duration_ms=int((time.perf_counter() - started) * 1000),
|
||||
files_written=written,
|
||||
)
|
||||
finally:
|
||||
if ephemeral:
|
||||
if policy_file is not None:
|
||||
policy_file.unlink(missing_ok=True)
|
||||
root_resolved = self._root.resolve()
|
||||
if work_root != root_resolved and work_root.is_relative_to(root_resolved):
|
||||
shutil.rmtree(workdir, ignore_errors=True)
|
||||
|
||||
@staticmethod
|
||||
def _kill(proc: asyncio.subprocess.Process) -> None:
|
||||
|
||||
Reference in New Issue
Block a user