Skip to content

Commit f44145f

Browse files
committed
testing: keep the scheduler's idle release to one unit and at most three hours
scheduler_wrap passed the command timeout to --idle-timeout as seconds. Measured on the scheduler (2026-11-25): it reads one unit only and caps the idle release at three hours. Above the cap it refuses the request ("idle timeout cannot exceed 3h"), so nothing runs; in between it takes 8100s, then hands its supervisor "2h15m", which that process cannot read. The v0.35.0 campaign asked for 6h15m for its six-hour module tests: three of them never ran, and the runner reported each as a silent exit. idle_timeout() rounds the duration up to one unit (seconds, minutes or hours) and caps it at IDLE_MAX_SEC, so the reservation still outlives the runner's own timers whenever the cap allows. The command timeout stays in seconds; the scheduler takes any duration there. The log no longer claims cards that were not granted. Without a "Reserved ..." line nothing ran, so it says that and quotes the scheduler's error, cut before the flag list that follows it.
1 parent 8e2fa1b commit f44145f

4 files changed

Lines changed: 99 additions & 15 deletions

File tree

‎doc/architecture/SPEC/testing.md‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!-- verified-against: 2026-11-25 -->
44

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

77
## 职责
88
仓库中立的测试执行底座:gpu_lock.sh / kill_test_tree.sh / test_watchdog.sh /
@@ -40,9 +40,10 @@ test_runner.sh 的 Python 移植。仓库专属数据(watchdog 模式、artifa
4040
`RunPlan`、`TestOutcome`、`strip_cov_flags`、`cleanup_test_artifacts`、
4141
`log_file_for`/`pass_marker_for`、`TIMEOUT_RC=124`、`PY_TIMEOUT_MARGIN_SEC`。
4242
- `gpu_demand`:`GpuDemand`(`.from_env`)、`command_gpus`、`shell_needs_gpu`、
43-
`scheduler_wrap`、`granted_gpus`、`scheduler_error`、`duration_seconds`、
44-
`session_devices`、`prompt_devices`;常量 `MODE_ENV`、`HELD_ENV`、`WAIT_ENV`、
45-
`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`。
43+
`scheduler_wrap`、`idle_timeout`、`granted_gpus`、`scheduler_error`、
44+
`duration_seconds`、`session_devices`、`prompt_devices`;常量 `MODE_ENV`、
45+
`HELD_ENV`、`WAIT_ENV`、`NO_GPU_RC`、`SCHEDULER_TIMEOUT_MARGIN_SEC`、
46+
`IDLE_MAX_SEC`。
4647
- `watchdog`:`WatchdogPatterns(.from_yaml)`、`LogWatchdog(.start/.stop/.check_once/.result)`、`WatchdogResult`。
4748
- `watchdog_learn`:`record`、`harvest`、`eligible_patterns`、`promote`、
4849
`read_decisions`、`normalize_pattern`、`repair_tail`。
@@ -95,9 +96,10 @@ env 前缀 → `scrub_agent_shell_env(extra_safe_prefixes=…)`(manifest 数
9596
(TestRunner 对 shell 的 command-echo 金样)、`test_module_pytest_signals.py`
9697
(经包装器:`terminate_active` 停树、stdin=/dev/null);`watchdog_learn` 另由
9798
`test_curator.py`(harvest exactly-once)与 `test_knowledge_foundation.py`
98-
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(50 个)以一个替身 `gpu`
99+
(record 身份字段、撕尾修复)钉住。`test_gpu_demand.py`(64 个)以一个替身 `gpu`
99100
可执行文件覆盖按需模式:卡数、shell 命令的判定、包装程序的输出路由与等待到期、
100-
`TestRunner` 的 skip,以及 agent shell 与模块测试包装器。
101+
空闲回收的单位与上限、调度器拒绝请求时的日志行、`TestRunner` 的 skip,以及 agent
102+
shell 与模块测试包装器。
101103

102104
## 重构备注
103105
`runner.py`(643L)是包内最重的一块:`_spawn` 把快照线程、双定时器、watchdog
@@ -137,13 +139,20 @@ campaign,改为每条 GPU 命令向调度器申请自己需要的卡、只在
137139
返回一段给 `bash --norc -c` 的 shell:命令的 stdout/stderr 留在调用方给的描述符
138140
(fd 3/4),调度器自己的输出写进 `<日志>.scheduler`。实测调度器把进度打到
139141
stdout、把 `Error: wait timeout exceeded` 打到 stderr,等待到期时退出码为 1;
140-
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。调度器的超时
141-
与空闲回收都设为 `timeout_sec + SCHEDULER_TIMEOUT_MARGIN_SEC`,让运行器自己的
142-
定时器先触发。
142+
包装程序把它换成 `NO_GPU_RC`(97),其余失败保留调度器的退出码。调度器的超时设为
143+
`timeout_sec + SCHEDULER_TIMEOUT_MARGIN_SEC`(秒),让运行器自己的定时器先触发。
144+
空闲回收由 `idle_timeout()` 给出:同一个时长向上取整到**单一单位**(秒、分或小时),
145+
且不超过 `IDLE_MAX_SEC`(3 小时)。实测(2026-11-25)调度器只解析单一单位的空闲
146+
时长:给 `1h30m` 直接拒绝;给 `8100s` 它接受,再把 `2h15m` 交给自己的 supervisor,
147+
那个进程读不了(卡照给、照释放,只是不会因空闲回收);超过 3 小时也直接拒绝。
148+
v0.35.0 campaign 最初把整个超时按秒传给它,超时 6 小时的模块测试因此一次都没跑成。
149+
命令的超时用秒则没有限制。
143150
- **调度器的输出绝不进测试日志。** 本机的 `gpu run` 会打印一条 "failed to
144151
activate booking" 警告,命中看门狗 Tier-2 的 `failed to`,会让每个 job 多一次
145152
LLM 评审。日志里只留运行器自己写的一行 `[gpu] …`(拿到的卡,或没跑成的原因);
146-
命令失败且调度器报了错时才附上它的错误文本。
153+
命令失败且调度器报了错时才附上它的错误文本。日志里没有 `Reserved …` 可依时写
154+
`[gpu] not run: the scheduler named no card(s) and said: …`——没有卡,命令就没跑;
155+
错误文本在它随后的 `Usage:` 处截断。
147156
- **排队到期是一次显式 skip。** `TestRunner.run` 返回 `rc=0, skipped=True`,
148157
`skip_reason` 写明要几张、等了多久;命令没有运行,也不写 pass marker。等待
149158
上限不超过 `timeout_sec` 的一半:运行器的定时器从 spawn 起算,排队时间也在内。

‎src/infermatrix_copilot/testing/gpu_demand.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
CUDA_VISIBLE_DEVICES to them. Its own output is kept out of test logs: on this
1919
machine it carries a "failed to activate booking" warning, which matches the
2020
watchdog's `failed to` pattern and would start an LLM review of every job.
21+
Its idle release takes one unit and at most three hours (`idle_timeout`); its
22+
command timeout takes any duration in seconds.
2123
"""
2224
from __future__ import annotations
2325

@@ -35,6 +37,9 @@
3537
#: The scheduler stops a command this long after the runner's own timers
3638
#: would have, so the runner's timeout stays the one that fires.
3739
SCHEDULER_TIMEOUT_MARGIN_SEC = 900
40+
#: The longest idle release the scheduler accepts; it refuses the request
41+
#: outright above this ("idle timeout cannot exceed 3h").
42+
IDLE_MAX_SEC = 3 * 3600
3843
#: The wrapper's own status for "the scheduler granted no cards in time".
3944
NO_GPU_RC = 97
4045

@@ -167,7 +172,7 @@ def scheduler_wrap(command: str, *, gpus: int, timeout_sec: float, wait: str,
167172
# the scheduler releases an idle card after 30 min by default; a test
168173
# may take longer before its first kernel (a model download), and the
169174
# timeout already bounds this reservation
170-
"--idle-timeout", f"{int(timeout_sec)}s",
175+
"--idle-timeout", idle_timeout(timeout_sec),
171176
"--wait", wait, "--note", note, "--",
172177
"bash", "--norc", "-c", "exec >&3 2>&4\n" + command])
173178
side = shlex.quote(str(side_log))
@@ -181,6 +186,22 @@ def scheduler_wrap(command: str, *, gpus: int, timeout_sec: float, wait: str,
181186
])
182187

183188

189+
def idle_timeout(seconds: float) -> str:
190+
"""`seconds` as an idle release the scheduler takes: one unit (seconds,
191+
minutes or hours), rounded up so the reservation outlives the command's
192+
own timers, and never above IDLE_MAX_SEC. It parses one unit only. Given
193+
`2h15m` it refuses the request; given `8100s` it accepts, then hands its
194+
supervisor `"2h15m"`, which that process cannot read (the card is still
195+
granted and released, but never for idleness)."""
196+
seconds = min(int(seconds), IDLE_MAX_SEC)
197+
if seconds < 60:
198+
return f"{max(seconds, 1)}s"
199+
minutes = -(-seconds // 60)
200+
if minutes < 60:
201+
return f"{minutes}m"
202+
return f"{-(-seconds // 3600)}h"
203+
204+
184205
def granted_gpus(side_log: Path) -> list[str]:
185206
"""The physical cards the scheduler named for one command."""
186207
try:
@@ -198,7 +219,11 @@ def scheduler_error(side_log: Path) -> str:
198219
except OSError:
199220
return ""
200221
hit = text.find("Error:")
201-
return text[hit:hit + 300].strip() if hit >= 0 else ""
222+
if hit < 0:
223+
return ""
224+
error = text[hit:]
225+
usage = error.find("\nUsage:") # the flag list that follows is not the error
226+
return (error if usage < 0 else error[:usage]).strip()[:300]
202227

203228

204229
INHERITED_DEFAULT = "0,1"

‎src/infermatrix_copilot/testing/runner.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,22 @@ def _note_scheduler(self, job: TestJob, side_log: Path, rc: int,
530530
ran on, or why it did not run. The scheduler's own text stays in the
531531
side log unless the command failed anyway."""
532532
gpus = self._job_gpus(job)
533+
granted = granted_gpus(side_log)
533534
if rc == NO_GPU_RC:
534535
lines = [f"[gpu] not run: the scheduler granted no {gpus} card(s) "
535536
f"within {self._wait(job)}."]
536-
else:
537+
elif granted:
537538
lines = [f"[gpu] {gpus} card(s) from the scheduler: "
538-
f"[{' '.join(granted_gpus(side_log))}]"]
539+
f"[{' '.join(granted)}]"]
539540
if rc not in (0, TIMEOUT_RC):
540541
error = scheduler_error(side_log)
541542
if error:
542543
lines.append(f"[gpu] scheduler: {error}")
544+
else:
545+
# no card was ever named, so nothing ran under the scheduler
546+
error = scheduler_error(side_log)
547+
lines = ["[gpu] not run: the scheduler named no card(s)"
548+
+ (f" and said: {error}" if error else ".")]
543549
try:
544550
with open(log_file, "a", encoding="utf-8") as lf:
545551
lf.write("\n" + "\n".join(lines) + "\n")

‎test/test_gpu_demand.py‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import json
1111
import os
12+
import re
1213
import subprocess
1314
from pathlib import Path
1415

@@ -22,12 +23,14 @@
2223
from infermatrix_copilot.testing import gpu_demand
2324
from infermatrix_copilot.testing.gpu_demand import (
2425
DEFAULT_WAIT,
26+
IDLE_MAX_SEC,
2527
NO_GPU_RC,
2628
SCHEDULER_TIMEOUT_MARGIN_SEC,
2729
GpuDemand,
2830
command_gpus,
2931
duration_seconds,
3032
granted_gpus,
33+
idle_timeout,
3134
prompt_devices,
3235
scheduler_error,
3336
scheduler_wrap,
@@ -47,6 +50,11 @@
4750
esac
4851
done
4952
printf 'gpus=%s\n' "$gpus" >> "$IMX_GPU_STUB_LOG"
53+
if [ -f "$(dirname "$IMX_GPU_STUB_REFUSE")/deny" ]; then
54+
printf 'Error: invalid idle timeout: idle timeout cannot exceed 3h\nUsage:\n canhazgpu run\nFlags:\n --gpus int Number of GPUs\n' >&2
55+
printf 'Error: invalid idle timeout: idle timeout cannot exceed 3h\n' >&2
56+
exit 1
57+
fi
5058
if [ -f "$IMX_GPU_STUB_REFUSE" ]; then
5159
printf 'Waiting for GPUs... (queue position: 3)\n'
5260
printf 'Error: wait timeout exceeded\nUsage:\n canhazgpu run\n' >&2
@@ -152,6 +160,20 @@ def test_duration_seconds_reads_the_scheduler_formats():
152160
assert duration_seconds("later", -1) == -1
153161

154162

163+
@pytest.mark.parametrize("seconds, text", [
164+
(0, "1s"), (59, "59s"), (60, "1m"), (1040, "18m"), (1500, "25m"),
165+
(1501, "26m"), (3599, "1h"), (3600, "1h"), (7200, "2h"), (7201, "3h"),
166+
(8100, "3h"), (IDLE_MAX_SEC, "3h"),
167+
(21600 + SCHEDULER_TIMEOUT_MARGIN_SEC, "3h"),
168+
])
169+
def test_idle_timeout_is_one_unit_within_the_schedulers_cap(seconds, text):
170+
assert idle_timeout(seconds) == text
171+
# measured on the scheduler (2026-11-25): it reads one unit only (given
172+
# `1h30m` it refuses the request) and at most three hours
173+
assert re.fullmatch(r"\d+[smh]", text)
174+
assert duration_seconds(text) <= IDLE_MAX_SEC
175+
176+
155177
def test_mode_comes_from_the_environment(monkeypatch):
156178
assert GpuDemand.from_env({}) == GpuDemand(enabled=False, wait="30m")
157179
demand = {gpu_demand.MODE_ENV: "demand", gpu_demand.WAIT_ENV: "2m"}
@@ -239,7 +261,29 @@ def test_runner_asks_for_the_jobs_cards_and_bounds_the_wait(scheduler, tmp_path)
239261
# the runner's own timer starts at the spawn: the wait is at most half of it
240262
assert "--wait 50s" in program
241263
assert f"--timeout {int(100 + SCHEDULER_TIMEOUT_MARGIN_SEC)}s" in program
242-
assert f"--idle-timeout {int(100 + SCHEDULER_TIMEOUT_MARGIN_SEC)}s" in program
264+
assert "--idle-timeout 17m" in program
265+
266+
267+
def test_runner_caps_the_idle_release_of_a_long_job(scheduler, tmp_path):
268+
env, _, _ = scheduler
269+
job = TestJob(key="long", command="printf 'ran\\n'", timeout_sec=21600)
270+
outcome = _runner(tmp_path).run(job, dict(env))
271+
assert outcome.rc == 0
272+
program = outcome.plan.argv[-1]
273+
assert f"--timeout {21600 + SCHEDULER_TIMEOUT_MARGIN_SEC}s" in program
274+
assert "--idle-timeout 3h" in program
275+
276+
277+
def test_runner_says_so_when_the_scheduler_refuses_the_request(scheduler, tmp_path):
278+
env, _, _ = scheduler
279+
(tmp_path / "deny").touch()
280+
job = TestJob(key="probe", command="printf 'ran\\n'", timeout_sec=600)
281+
outcome = _runner(tmp_path).run(job, dict(env))
282+
assert outcome.rc == 1 and not outcome.skipped # the scheduler's own status
283+
log = Path(outcome.log_file).read_text()
284+
assert ("[gpu] not run: the scheduler named no card(s) and said: "
285+
"Error: invalid idle timeout: idle timeout cannot exceed 3h\n") in log
286+
assert "Usage:" not in log and "\nran\n" not in log
243287

244288

245289
def test_runner_leaves_a_command_that_asks_for_no_cards_alone(scheduler, tmp_path):

0 commit comments

Comments
 (0)