Repository navigation
fix(test): eliminate four macOS release-qualification shard flakes - #10256
Conversation
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · Gemini · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
动机 / Motivation
受影响的调用方与维护者:在 macOS 上执行单波分片发布验收脚本(scripts/qualify_release_commit.py)或并行运行 pytest 分片的发布维护者与开发者。
改动前后的具体场景:改动前,在 macOS 并行分片验收中会反复触发四类环境与顺序相关抖动:当 benchmark/tests/test_resume_sessions.py 在同一分片中早于 test_benchmark_planning_batch.py 运行时,其对 planning.task_plan_packet 的 monkeypatch 会在首次导入 planning_batch 时被静态捕获,导致后续用例报 KeyError: 'stage' 与 KeyError: 'goal_id';同时 microbench 子进程仅有 0.65s 剩余预算易在并发启动 CPython 时超时,telemetry isolation 夹具非原子写入 PID 就绪文件会读到空字符串引发 ValueError,且分片临时目录落在 macOS 49 字符的 /var/folders 路径下不断拉长子进程路径。改动后,四类根因均被精准消除,分片验收与跨用例顺序复现全部稳定通过。
可观测改进结果:跨用例顺序复现(test_resume_sessions 紧接 test_benchmark_planning_batch)从 2 failed 转为 28 passed,telemetry isolation(14 passed)、release qualification(28 passed)和 dual-model microbench(23 passed)均无抖动通过,且不改变任何评分逻辑、任务语义、权限边界或控制面协议。
本 PR 明确的非目标:本 PR 不修改控制面核心状态机、不放宽 128 UTF-16 字符的 source_ref 校验规则、不调整基准评分或任务语义,也不修改发布门禁清单。
从实际发布验收代价来看,上述四类假阴性会迫使维护者整批重跑多核 pytest 分片并人工排查日志,甚至误判正常版本提交存在回归;通过把顺序污染、非原子就绪文件、过紧的 CPython 启动窗口以及过长的 macOS 临时目录根逐一在最小边界内消除,单波分片验收的确定性得到直接提升。
改动思路 / Approach
本次采用根因对症的最小改动策略,而非简单重跑或放宽断言。架构判断结论:四处改动均属于最小必要局部修复:规划助手模块改用延迟属性查找以隔离测试期 monkeypatch,两个测试文件修复竞态写入与子进程启动预算,发布脚本增加可注入的短临时目录选择函数。无需抽象新的公共框架或修改控制面拥有者。 本次 PR 的边界严格收敛为:仅限 benchmark/runtime 两个薄入口助手(planning_batch.py、sforge_entry.py)、发布分片脚本 scripts/qualify_release_commit.py 及对应的 4 个测试文件,不触及 loopx/ 控制面或前端。
- 消除导入期 monkeypatch 永久污染:
benchmark/runtime/planning_batch.py与benchmark/runtime/sforge_entry.py不再使用from .planning import task_plan_packet在模块首次加载时固化函数引用,而是改为from . import planning as _planning并在调用点动态解析_planning.task_plan_packet(以及_planning.validate_plan_readback)。这样即使前序测试(如test_resume_sessions.py)在 monkeypatch 生效期间触发了首次导入,teardown 恢复planning.task_plan_packet后后续测试仍能调用真实实现,完全消除分片内跨用例顺序依赖。 - 保持单臂 deadline 扣减不变量的同时消除启动抖动:
benchmark/tests/test_dual_model_microbench.py中 Subcase B 将总预算由1.00s按比例扩展为5.00s、模拟准备耗时由0.35s扩展为1.75s,依然严格断言子进程收到pytest.approx(3.25, abs=0.05)的剩余预算且返回ok;Subcase C 将真实准备预算由0.05s调整为0.25s、子进程休眠由0.25s调整为2.0s,避免高负载下prepare_codex_home自身超过 50ms 误触codex_budget_exhausted而跳过codex_timeout验证。 - 就绪文件原子发布:
tests/canary/test_telemetry_isolation.py中子进程改为先写同目录.tmp临时文件再os.replace到ready(以及marker),消除父进程在exists()为真但文件内容仍为空的瞬时窗口读到空字符串引发ValueError的竞态,同时在finally清理段对空文本做防御校验。 - 缩短 macOS 分片临时目录前缀:
scripts/qualify_release_commit.py新增_shard_temp_base_dir()辅助函数,在 POSIX 宿主默认临时目录超过 32 字符且调用方未显式设置短TMPDIR、同时/tmp存在且可写时优先选用/tmp作为mkdtemp(dir=...)基础目录,避免/private/var/folders/...拉长分片内TMPDIR、HOME与 Unix socket 路径;通过关键字参数注入实现跨平台(含 Windows/Linux/macOS)单测覆盖。
具体改动 / Changes
benchmark/runtime/planning_batch.py(apply_initial_plan):将顶层静态绑定from .planning import task_plan_packet替换为from . import planning as _planning,并在CliPlanningBatchBackend(packet_reader=...)中通过_planning.task_plan_packet(env, cli)动态解析调用,保留原__all__与全部对外契约不变。benchmark/runtime/sforge_entry.py(prepare_entry):同步将from .planning import task_plan_packet, validate_plan_readback替换为_planning.task_plan_packet与_planning.validate_plan_readback,防止 SForge 入口模块在相似测试顺序下捕获临时补丁。tests/control_plane/test_benchmark_planning_batch.py(test_planning_batch_resolves_live_task_plan_packet_after_import_time_patch):新增回归测试,显式模拟在planning.task_plan_packet被 monkeypatch 为 stub 时执行importlib.reload(planning_batch),随后退出补丁上下文并断言apply_initial_plan调用的是恢复后的实时函数而非残留 stub。benchmark/tests/test_dual_model_microbench.py(test_codex_runner_enforces_single_arm_deadline_across_setup_and_subprocess):将 Subcase B 调整为timeout_seconds=5.00、clock_b["t"] += 1.75、seen_timeouts == [pytest.approx(3.25, abs=0.05)],将 Subcase C 调整为FAKE_CODEX_SLEEP_SECONDS="2.0"与timeout_seconds=0.25,验证完全相同的单臂时间预算扣减与超时分类逻辑。tests/canary/test_telemetry_isolation.py(test_smoke_failure_reaps_children_before_cleaning_fixture、test_smoke_retires_every_owned_runtime_revision):子进程写ready和marker均改为先写.tmp再os.replace原子替换,finally段先检查pid_text.isdigit()再调用os.kill,防止清理路径覆盖真实异常。scripts/qualify_release_commit.py(_shard_temp_base_dir、run_preflight、_run_shard):新增_MAX_SHARD_TEMP_BASE_DIR_LEN = 32与_shard_temp_base_dir(*, os_name=None, env=None, default_tempdir=None, short_base=Path("/tmp")),并在run_preflight与_run_shard的tempfile.mkdtemp调用中传入dir=_shard_temp_base_dir()。tests/control_plane/test_release_commit_qualification.py(test_qualify_release_commit_prefers_short_posix_tmp_base_over_long_default):覆盖长 macOS 合成路径、未设TMPDIR、显式短TMPDIR、短默认路径、目录不存在、不可写/tmp、非 POSIX(nt)以及真实 POSIX/tmp8 条分支。
对主干的风险 / Risks
- 兼容性与行为语义风险(低):全仓检索确认无任何生产代码或测试向
benchmark.runtime.planning_batch.task_plan_packet或benchmark.runtime.sforge_entry.task_plan_packet直接注入补丁,两者均未把该函数放入__all__;延迟解析在生产路径下调用同一函数对象,零行为漂移。 - 平台与容器环境风险(低):
_shard_temp_base_dir()显式校验os.name == "posix"、short_base.is_dir()及os.access(short_base, os.W_OK | os.X_OK),在 Windows、无/tmp或只读/tmp的受限容器内均自动回退为None(沿用系统默认tempfile.mkdtemp),同时完整尊重显式设置的短TMPDIR。 - 测试时序风险(低):microbench Subcase B 放宽至
3.25s剩余窗口与 Subcase C0.25s准备窗口仅影响高负载容忍度,不降低codex_budget_exhausted/ 剩余超时扣减 /codex_timeout三段不变量的严格性;Subcase C 触发超时后立即终止子进程,不会增加 2 秒实际耗时。 - 公私边界与门禁风险(无):已对全部 7 个改动文件执行
loopx check --scan-path扫描(0 errors),测试中使用的 macOS 路径均为合成占位字符串,无任何本机真实路径、凭据或内部信息泄露;canary premerge全部 4/4 直接检查与 10/10 目录金丝雀通过(0 failures),仅因触及benchmark/路径触发常规benchmark_sensitive人工复核标记,且已确认未改动任何基准评分、任务或权限逻辑。
我的整体评价 / Verdict
本 PR 在精确提交 0a249a80c2eef70830c86642cc19a047afdfe4bd 上完整、干净地解决了今天 macOS 发布分片验收中观测到的全部四类抖动,尤其是通过复现定位到 test_benchmark_planning_batch.py 失败的真实根因是 test_resume_sessions.py 触发的导入期 monkeypatch 泄漏而非路径长度,从而避免治标不治本。视觉评审说明:已检查 7 个改动文件,全部为测试、benchmark 运行时薄入口与 CLI 脚本,不涉及任何渲染界面或前端展示变更。(unverified_scope: none)。
本地验证汇总(wait_for_ci=false,未轮询远端 CI):
- PASS:精确跨用例顺序复现(
test_resume_sessions+test_benchmark_planning_batch,含 >80 字符--basetemp)— 28 passed - PASS:
tests/canary/test_telemetry_isolation.py— 14 passed - PASS:
tests/control_plane/test_release_commit_qualification.py— 28 passed - PASS:
benchmark/tests/test_dual_model_microbench.py— 23 passed - PASS:
ruff check(7 文件)、mypy --follow-imports=silent(修改的类型注解文件 0 issues)、git diff --check、check_rfc_bilingual.py、loopx check --scan-path(0 errors) - PASS:
loopx --format json canary premerge --from-git-diff --git-diff-base origin/main -j 1— 4/4 direct checks、python compile、10/10 catalog canaries 全部通过,0 failures(manual_review_required仅因benchmark_sensitive路径归类,经审查无评分/任务/权限变更)
English verdict: APPROVE — exact head 0a249a80c2eef70830c86642cc19a047afdfe4bd eliminates all four measured macOS release-qualification flakes (import-time task_plan_packet monkeypatch leak in planning_batch.py/sforge_entry.py, tight CPython spawn budget in microbench Subcases B/C, non-atomic PID/marker barrier writes in test_telemetry_isolation.py, and long macOS shard temp roots in qualify_release_commit.py) with 0 blocking findings and all targeted pytest suites, ruff, mypy, public-boundary scan, and 10/10 canary premerge checks passing locally.
…kbench - Resolve merge conflicts in oar.test.ts, oar.spec.mjs, runtime/workbench.ts, and runtime/workbench_service.ts while preserving all #9854 OAR fusion behavior. - Align #9854 OAR Core bridge env vars to LOOPX_WORKBENCH_OAR_CORE_* (retaining LOOCHAT_OAR_CORE_* compat fallback and secret scrubbing in daemon, runtime_loader.ts, and oar_client.py). - Update python-tests.yml Workbench OAR job workspace name from @loopx/loochat to @loopx/workbench-daemon. - Fix CodexSession.onPeerClosed() collision with ThreadManager's session.onClosed(listener) hook and await in-flight sessionStarts during shutdown without breaking cancelled-turn semantics. Signed-off-by: maxliux5 <5958865+maxliux5@users.noreply.github.com>
Delivery Brief
scripts/qualify_release_commit.py).benchmark/runtime/planning_batch.py(andbenchmark/runtime/sforge_entry.py): Whenbenchmark/tests/test_resume_sessions.pyran earlier in the same pytest shard, it monkeypatchedbenchmark.runtime.planning.task_plan_packetto a stub and triggered the first import ofbenchmark.runtime.planning_batch, which boundfrom .planning import task_plan_packetat module level. After monkeypatch teardown,planning_batchstayed permanently poisoned with the stub for subsequent tests in the same shard (test_planning_batch_preserves_upstream_error_detail_on_refine_rejectionfailed withKeyError: 'stage'andtest_planning_batch_packet_survives_gbk_parent_locale[file]failed withKeyError: 'goal_id').benchmark/tests/test_dual_model_microbench.py:test_codex_runner_enforces_single_arm_deadline_across_setup_and_subprocessSubcase B gavetimeout_seconds=1.00minus0.35ssimulated prepare (0.65sremaining), causing CPython spawn under concurrent shard load to time out (codex_timeout); Subcase C usedtimeout_seconds=0.05across realprepare_codex_home.tests/canary/test_telemetry_isolation.py:test_smoke_failure_reaps_children_before_cleaning_fixturewrote child PID viaready.write_text(...)while the parent polledready.exists(), letting the parent observe a 0-bytereadyfile on cancellation and fail withValueError: invalid literal for int() with base 10: ''(same race onmarkerintest_smoke_retires_every_owned_runtime_revision)./var/folders/...shard temp roots inscripts/qualify_release_commit.py: Per-shard and preflight namespaces calledtempfile.mkdtemp(prefix=...)directly under macOS's 49-character/var/folders/.../T/defaultTMPDIR, needlessly inflating childTMPDIR,HOME, and POSIX socket paths.benchmark/runtime/planning_batch.py,benchmark/runtime/sforge_entry.py: import.planning as _planningand resolve_planning.task_plan_packet/_planning.validate_plan_readbackdynamically at call time so import-time monkeypatches never leak across tests.tests/control_plane/test_benchmark_planning_batch.py: add regression testtest_planning_batch_resolves_live_task_plan_packet_after_import_time_patchverifying reload-under-monkeypatch does not poison subsequent calls.benchmark/tests/test_dual_model_microbench.py: scale Subcase B totimeout_seconds=5.00with1.75sprepare (pytest.approx(3.25, abs=0.05)remaining) and Subcase C totimeout_seconds=0.25withFAKE_CODEX_SLEEP_SECONDS="2.0", preserving the exact single-arm deadline subtraction invariant.tests/canary/test_telemetry_isolation.py: writeready(andmarker) atomically via sibling.tmp+os.replace, and guardfinallycleanup against empty PID reads.scripts/qualify_release_commit.py,tests/control_plane/test_release_commit_qualification.py: add_shard_temp_base_dir()to prefer writable/tmpon POSIX when the default temp dir exceeds 32 chars andTMPDIRis not explicitly short; cover all branches with a portable unit test.Verification
test_resume_sessions.py::test_planning_heartbeat_and_turn_share_one_binding[None-heartbeat]+test_benchmark_planning_batch.py, previously failing2 failed, 1 passedin 2.86s): 28 passed (including with >80-char--basetemp).uv run --extra test python -m pytest -q tests/canary/test_telemetry_isolation.py: 14 passed.uv run --extra test python -m pytest -q tests/control_plane/test_release_commit_qualification.py: 28 passed.uv run --extra test python -m pytest -q benchmark/tests/test_dual_model_microbench.py: 23 passed.uv run --extra test ruff check <7 changed files>: All checks passed.uv run --extra test mypy --follow-imports=silent --explicit-package-bases scripts/qualify_release_commit.py benchmark/runtime/planning_batch.py: 0 issues.loopx check --scan-path <7 changed files>: public boundary scan clean (0 errors).loopx --format json canary premerge --from-git-diff --git-diff-base origin/main -j 1: 4/4 direct checks passed, python compile passed, 10/10 catalog canaries passed, 0 failures (manual_review_requireddue tobenchmark_sensitivesurface classification; no scoring/task/permission behavior changed).