Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/architecture/SPEC/engine/steps/rebase_v3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# engine/steps/rebase_v3.py —— 规范

<!-- verified-against: 2026-11-25 -->
<!-- verified-against: 2026-11-26 -->

`LOC ~2660 · step 库(v3 rebase 装配层) · refactor-status: oversized`

Expand Down Expand Up @@ -172,7 +172,9 @@ campaign 改为按命令向调度器取卡之后(`testing` 规范的"按命令
经本文件启动的 GPU 命令各自声明要几张:

- `run_pytest`/`reproduce`(`_run_tests`)以 `TestJob(min_gpus=0, gpu_lock=True,
gpus=1)` 运行,向调度器要 1 张卡。返回值新增 `skipped` 与 `skip_reason`;
gpus=None)` 运行:卡数由 `TestRunner` 按 `command_gpus` 从命令及其测试的声明推导,
至少 1 张(2026-11-26 起"按需要申请",此前固定 1 张)。返回值新增 `skipped` 与
`skip_reason`;
`passed` 只在 `rc == 0 and not skipped` 时为真——调度器没给卡的运行没有通过。
- `run_precommit` 以 `gpus=0` 运行,不申请。
- 两处 `ModuleRunConfig` 的 `cuda_devices` 改取 `testing.gpu_demand.session_devices()`:
Expand Down
17 changes: 13 additions & 4 deletions doc/architecture/SPEC/rebase_engine.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rebase_engine/ —— 规范

<!-- verified-against: 2026-11-25 -->
<!-- verified-against: 2026-11-26 -->

`LOC ~9530(30 个模块) · repo-rebase-v3 的原生 rebase 引擎 · refactor-status: ok`

Expand Down Expand Up @@ -269,14 +269,17 @@ campaign 不再从头到尾持有一份外层预约,每条 GPU 命令向调度
`_run_shell`(原有的 Popen、后代快照与超时逻辑未变)。按需模式(读本进程环境的
`IMX_GPU_MODE`)下,它把模式写进子进程环境,使命令启动的 `imx-omni-pytest`
自己去申请;`testing.gpu_demand.shell_needs_gpu` 判定会起测试或 CUDA 代码的命令,
用 `scheduler_wrap` 向调度器要 1 张卡(等待上限是工具超时的一半),并给子进程设
用 `scheduler_wrap` 向调度器要卡,张数取 `command_gpus(command, 1, root)`——
命令的实际 cwd,缺省是目标仓库;至少 1 张(等待上限是工具超时的一半),并给子进程设
`IMX_GPU_HELD=1`,命令启动的东西不再申请。等待到期时返回
`{"exit_code": 1, "gpu_unavailable": true, "stderr": "Not run: …"}`;其余失败
在 stderr 前附上调度器的错误文本。2026-11-24/25 的 campaign 里,agent 的 1039 次
工具调用有 882 次走 `run_shell`(计划评审之前 `run_pytest` 是锁住的),其中绝大多数
是 `git`、`grep` 与读取,不受影响。
- **`module_pytest.py`。** 包装器的 `TestJob` 带 `gpus=1`:测试可能用到卡,如同它
过去在外层预约下运行时那样。包装器仍返回结果自己的 rc(既有契约,由
- **`module_pytest.py`。** 包装器的 `TestJob` 带
`gpus=command_gpus(command, 1, root=repo, maximum=…)`:以它所选测试声明的需要
为准(`testing` 规范的"卡数 = 需要"),至少 1 张——测试可能用到卡,如同它过去在
外层预约下运行时那样。包装器仍返回结果自己的 rc(既有契约,由
`test_module_pytest_outcomes.py` 钉住),所以跳过时把
`imx-omni-pytest: skipped — <原因>` 同时写到 stdout 和 stderr。
- **提示词。** `build_module_prompt(..., cuda_devices=prompt_devices(config.cuda_devices))`:
Expand All @@ -295,3 +298,9 @@ campaign 不再从头到尾持有一份外层预约,每条 GPU 命令向调度
final 只有 `cu130`。按目标的 variant 去取,测试循环用来区分既有失败与回归的基线
运行时就建不出来,退回到让基线也跑在 campaign 的 venv 上。vllm-omni adapter 把
`baseline_variant` 设为 `cu130`。测试:`test_rebase_release_target.py`。

2026-11-26:负责人定了"一直等到有卡、按需要申请、不做 reserve"。两处的卡数从固定
1 张改为按需要推导:此前模块测试只申请 1 张,而外层预约时代每个模块的测试都看得到
4 张卡,需要多张卡的测试因此失败(`tests/diffusion/models/hy_nomas/
test_pixelumm_parallel.py`)。等待不再有缺省上限(`run_shell` 里同步起的测试除外,
见 `testing` 规范)。
90 changes: 72 additions & 18 deletions doc/architecture/SPEC/testing.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# testing/ —— 规范

<!-- verified-against: 2026-11-25 -->
<!-- verified-against: 2026-11-26 -->

`LOC ~2190(9 个文件) · 测试执行底座(rebase agent shell 测试层的 Python 移植) · refactor-status: ok`
`LOC ~2450(9 个文件) · 测试执行底座(rebase agent shell 测试层的 Python 移植) · refactor-status: ok`

## 职责
仓库中立的测试执行底座:gpu_lock.sh / kill_test_tree.sh / test_watchdog.sh /
Expand Down Expand Up @@ -42,8 +42,8 @@ test_runner.sh 的 Python 移植。仓库专属数据(watchdog 模式、artifa
- `gpu_demand`:`GpuDemand`(`.from_env`)、`command_gpus`、`shell_needs_gpu`、
`scheduler_wrap`、`idle_timeout`、`granted_gpus`、`scheduler_error`、
`duration_seconds`、`session_devices`、`prompt_devices`;常量 `MODE_ENV`、
`HELD_ENV`、`WAIT_ENV`、`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`、
`IDLE_MAX_SEC`。
`HELD_ENV`、`WAIT_ENV`、`MAX_ENV`、`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`、
`IDLE_MAX_SEC`、`RUN_LEVEL_OPTION`、`RUN_LEVELS`、`DEFAULT_RUN_LEVEL`。
- `watchdog`:`WatchdogPatterns(.from_yaml)`、`LogWatchdog(.start/.stop/.check_once/.result)`、`WatchdogResult`。
- `watchdog_learn`:`record`、`harvest`、`eligible_patterns`、`promote`、
`read_decisions`、`normalize_pattern`、`repair_tail`。
Expand Down Expand Up @@ -96,10 +96,11 @@ env 前缀 → `scrub_agent_shell_env(extra_safe_prefixes=…)`(manifest 数
(TestRunner 对 shell 的 command-echo 金样)、`test_module_pytest_signals.py`
(经包装器:`terminate_active` 停树、stdin=/dev/null);`watchdog_learn` 另由
`test_curator.py`(harvest exactly-once)与 `test_knowledge_foundation.py`
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(64 个)以一个替身 `gpu`
可执行文件覆盖按需模式:卡数、shell 命令的判定、包装程序的输出路由与等待到期、
空闲回收的单位与上限、调度器拒绝请求时的日志行、`TestRunner` 的 skip,以及 agent
shell 与模块测试包装器。
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(74 个)以一个替身 `gpu`
可执行文件覆盖按需模式:卡数(含测试声明的读取与上限)、shell 命令的判定、
包装程序的输出路由、不限等待与有界等待到期、给卡后才计时、锁仍有界、
空闲回收的单位与上限、调度器拒绝请求时的日志行、`TestRunner` 的 skip,以及
agent shell 与模块测试包装器。

## 重构备注
`runner.py`(643L)是包内最重的一块:`_spawn` 把快照线程、双定时器、watchdog
Expand Down Expand Up @@ -127,19 +128,29 @@ GPU 锁下一次只跑一个,52 个只用 1 张卡。负责人决定:不再
campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在运行期间持有。

- **开关。** `IMX_GPU_MODE=demand` 选择按需模式;未设置时行为不变(调用方已
持有卡,命令继承)。`IMX_GPU_WAIT`(缺省 `30m`)是命令在调度器队列里的等待
持有卡,命令继承)。`IMX_GPU_WAIT` 给命令在调度器队列里的等待设上限;不设则
一直等到拿到卡(2026-11-26 起,见下文)。`IMX_GPU_MAX` 是一条命令申请卡数的
上限。`IMX_GPU_HELD=1` 表示所在命令树已持有卡,其下任何 `TestRunner` 都不再
申请。`TestRunner` 缺省从环境读取(`GpuDemand.from_env`),测试可显式传
`gpu_demand=`。
- **卡数。** `TestJob.gpus` 显式给出;缺省取三者最大值:`min_gpus`、命令里最大
的 `cards_N` pytest marker(manifest 的每个 job 都是 `hw: any`,卡数只在 marker
里)、以及"取 GPU 锁的运行算 1 张"。0 表示不申请(`run_precommit`)。agent 的
`run_pytest`/`reproduce` 与 `imx-omni-pytest` 包装器各要 1 张。
- **卡数 = 需要。** `TestJob.gpus` 显式给出;否则 `command_gpus(command, minimum,
root, maximum)`:命令自己的 `-m` 表达式选中的 `cards_N` 以其中最大者为准
(manifest 的每个 job 都是 `hw: any`,卡数只在 marker 里);被 `not` 否定的是
**排除**,不是需要——目标仓库自己的 CPU job 写的就是
`core_model and cpu and not (cards_2 or … or cards_8)`。没有选中任何 `cards_N`
时读取命令所指定的测试文件,取其中**与命令同属一个运行级别**(`--run-level`,
缺省 `core_model`)的测试所声明的最大卡数——`cards_N` 标记,或装饰器的
`num_cards=`。其它级别的、以及被表达式排除的卡数的测试不计:卡不够时它们按
自己的保护跳过,不会失败。没有声明算 1 张。
结果不低于 `minimum`(取 GPU 锁的运行至少 1 张,`run_precommit` 为 0、不申请),
不高于 `maximum`。agent 的 `run_pytest`/`reproduce`、`imx-<仓库>-pytest` 包装器
与 agent 在 shell 里起的测试走同一套算法。
- **包装程序。** `scheduler_wrap(command, gpus, timeout_sec, wait, note, side_log)`
返回一段给 `bash --norc -c` 的 shell:命令的 stdout/stderr 留在调用方给的描述符
(fd 3/4),调度器自己的输出写进 `<日志>.scheduler`。实测调度器把进度打到
stdout、把 `Error: wait timeout exceeded` 打到 stderr,等待到期时退出码为 1;
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。调度器的超时设为
stdout、把 `Error: wait timeout exceeded` 打到 stderr,有界等待到期时退出码为 1;
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。`wait` 为空时
不传 `--wait`,调度器一直排队。调度器的超时设为
`timeout_sec + SCHEDULER_TIMEOUT_MARGIN_SEC`(秒),让运行器自己的定时器先触发。
空闲回收由 `idle_timeout()` 给出:同一个时长向上取整到**单一单位**(秒、分或小时),
且不超过 `IDLE_MAX_SEC`(3 小时)。实测(2026-11-25)调度器只解析单一单位的空闲
Expand All @@ -153,9 +164,10 @@ campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在
命令失败且调度器报了错时才附上它的错误文本。日志里没有 `Reserved …` 可依时写
`[gpu] not run: the scheduler named no card(s) and said: …`——没有卡,命令就没跑;
错误文本在它随后的 `Usage:` 处截断。
- **排队到期是一次显式 skip。** `TestRunner.run` 返回 `rc=0, skipped=True`,
`skip_reason` 写明要几张、等了多久;命令没有运行,也不写 pass marker。等待
上限不超过 `timeout_sec` 的一半:运行器的定时器从 spawn 起算,排队时间也在内。
- **有界等待到期是一次显式 skip**(只在设了 `IMX_GPU_WAIT` 时)。`TestRunner.run`
返回 `rc=0, skipped=True`,`skip_reason` 写明要几张、等了多久;命令没有运行,
也不写 pass marker。上限不超过 `timeout_sec` 的一半:那时运行器的定时器从
spawn 起算,排队时间也在内。
- **按需模式下由调度器决定有没有卡**:不做硬件门槛,丢掉继承的
`CUDA_VISIBLE_DEVICES`(调度器授权时自己命名),并给子进程设 `IMX_GPU_HELD=1`。
- **命令结束后不在已释放的卡上清理进程。** 外层预约时代
Expand All @@ -178,3 +190,45 @@ campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在

实测(2026-11-25):一条 `TestRunner` 命令在真实调度器下恰好持有 1 张卡 35 秒,
前后都是 0;torch 看到 1 张卡;import vllm 及其编译扩展不建立 CUDA 上下文。

## 一直等到有卡、按需要申请(2026-11-26)

负责人在 2026-11-26 定了三条:排不到卡就一直等,不放弃也不跳过;不指定要哪几张
卡,申请的数量与需要的一致;不做任何 reserve,只走调度器的队列。

- **一直等。** `IMX_GPU_WAIT` 不再设缺省值:`gpu run` 不带 `--wait`。运行器的
定时器与看门狗改为在调度器给卡之后才启动——`_await_grant` 每秒读一次旁路文件,
直到出现 `Reserved …` 或进程退出。排队时间因此不计入测试的超时;此前定时器从
spawn 起算,一条排了两小时的命令会在队列里被自己的超时杀掉。旁路文件在启动前
清空:上一次运行留下的给卡记录不能让这一次的等待提前结束。日志里写两行:
`[gpu] queued for the scheduler's cards at …` 与 `[gpu] granted after N s in the
queue: [..]`——空了几小时的日志在读的人看来是挂死。调度器**拒绝**请求不是等待:
进程退出,日志照旧写 `not run`。GPU 锁保持它一小时的有界等待,没有跟着放开:
持锁的那个运行自己可能在队列里排几个小时,后面等锁的若不设上限,agent 的会话
就会无声地卡住;锁超时是它能据以行动的反馈。"一直等"说的是卡,锁是自己的串行。
- **按需要。** 模块测试此前固定申请 1 张,而以前外层预约时代每个模块的测试都能
看到 4 张卡:`tests/diffusion/models/hy_nomas/test_pixelumm_parallel.py` 按
2、4 个进程 `mp.spawn`,没有 `cards_N` 标记也没有保护,1 张卡下必然失败。
卡数于是改为读测试自己的声明:`command_gpus` 的规则见上文"卡数 = 需要"。读取
用 AST,不认字符串里的名字——目标仓库自己的测试会引用写着 `cards_8` 的 CI 命令。
只扫仓库根目录之内、路径名以 `test` 开头的目录,至多 4000 个文件;命令里出现的
别的目录一律不碰(一个以 `/` 为根的测试里,命令说了 `cd /tmp`)。结果缓存 5 分钟:
agent 一边工作一边改测试。
- **上限。** `tests/e2e --run-level advanced_model` 声明的需求是 8 张,也就是整台
机器;配上"一直等",这样一条命令可能永远排不上。`IMX_GPU_MAX` 封顶,launcher
里取 4——以往每个 campaign 的配置,也是 `core_model` 级别上声明的最大需求。
超出部分的测试按它们自己的保护跳过,和以前只看得到 4 张卡时一样。
- **仍然有界的一处。** agent 在 `run_shell` 里同步起的测试,等待不超过该次工具
调用超时的一半:工具调用的期限是 harness 定的,超了它等不下去;到期时返回
`gpu_unavailable`,提示词本就要求长验证走包装器的分离运行。

实测(2026-11-25/26):`tests/` 全树在 `core_model` 级别上声明 1 张的 196 个文件、
2 张的 14 个、4 张的 6 个;按模块,worker_runner / input_output / online_serving /
model_config 需要 1 张(后者无声明),model_executor / scheduler / platform 的
全量运行需要 4 张。AST 解析全树约 6 秒,单个模块目录 1–2 秒。

2026-11-26,实机上发现并补上:`-m` 表达式里被 `not` 否定的 `cards_N` 曾被当成需要
——卡数是对整条命令做正则取最大值。campaign 自己的 agent 跑
`tests/distributed/ -m 'core_model and cuda and L4 and not cards_2'` 时因此申请了
两张卡,而 manifest 里 CPU job 的排除表达式会申请八张。现在按表达式解析
(`_expression_cards`):`not` 作用于紧随其后的那一项,或一个括号组。
3 changes: 2 additions & 1 deletion src/infermatrix_copilot/engine/steps/rebase_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ def _run_tests(kw: Mapping, key: str) -> dict:
outcome = runner.run(
TestJob(key=f"{key}_{abs(hash(cmd)) % 10 ** 8}", command=cmd,
timeout_sec=float(kw.get("timeout") or 1800),
min_gpus=0, gpu_lock=True, gpus=1),
# as many cards as the command's tests declare, one at least
min_gpus=0, gpu_lock=True, gpus=None),
_target_test_env(settings, manifest, host_env=host_env))
tail = ""
try:
Expand Down
7 changes: 5 additions & 2 deletions src/infermatrix_copilot/rebase_engine/module_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import time
from pathlib import Path

from ..testing.gpu_demand import GpuDemand, command_gpus
from ..testing.runner import TestJob, TestOutcome, TestRunner
from ..testing.watchdog import WatchdogPatterns

Expand Down Expand Up @@ -459,9 +460,11 @@ def review_fn(test_name: str, snippet: str) -> str:
# pass), while gpu_lock=True holds the lock for the run
job = TestJob(key=_job_key(args), command=command, timeout_sec=timeout,
min_gpus=0, gpu_lock=_needs_gpu_lock(args), index=0,
# the tests may use a card, as they could under the
# as many cards as the tests it runs declare, and one at
# least: they may use a card, as they could under the
# campaign-wide reservation this wrapper used to run in
gpus=1)
gpus=command_gpus(command, 1, root=repo,
maximum=GpuDemand.from_env().maximum))
if process_setup:
process.install_signal_handlers(runner)
outcome = runner.run(job, dict(os.environ))
Expand Down
16 changes: 10 additions & 6 deletions src/infermatrix_copilot/rebase_engine/rebase_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ def handle_run_shell(command: str, workdir: str | None = None,
import tempfile

from ..testing.gpu_demand import (
HELD_ENV, MODE_ENV, NO_GPU_RC, GpuDemand, scheduler_error,
scheduler_wrap, shell_needs_gpu)
HELD_ENV, MODE_ENV, NO_GPU_RC, GpuDemand, command_gpus,
scheduler_error, scheduler_wrap, shell_needs_gpu)

env, side_log, wait = (dict(paths.env) or None), None, ""
if GpuDemand.from_env(os.environ).enabled:
root = workdir or paths.omni_path # where the command runs
demand = GpuDemand.from_env(os.environ)
if demand.enabled:
# The mode is this process's (a bridge learns it from its env)
# and must reach what the command starts: the test wrapper asks
# for its own cards.
Expand All @@ -201,10 +203,12 @@ def handle_run_shell(command: str, workdir: str | None = None,
os.close(fd)
side_log, wait = Path(name), f"{max(int(timeout / 2), 1)}s"
command = scheduler_wrap(
command, gpus=1, timeout_sec=timeout + 60, wait=wait,
command, gpus=command_gpus(command, 1, root=root,
maximum=demand.maximum),
timeout_sec=timeout + 60, wait=wait,
note=f"imx agent shell {os.getpid()}", side_log=side_log)
try:
result = _run_shell(command, workdir, timeout, env)
result = _run_shell(command, root, timeout, env)
finally:
scheduler = scheduler_error(side_log) if side_log else ""
if side_log:
Expand All @@ -225,7 +229,7 @@ def _run_shell(command: str, workdir: str | None, timeout: int,
from ..testing.process_tree import collect_descendants, kill_tree
from ..testing.runner import TestRunner, _record_walk

cwd = workdir or paths.omni_path
cwd = workdir # resolved by the caller
try:
with subprocess.Popen(
# Match TestRunner's runtime protection: inherited SSH
Expand Down
Loading
Loading