Skip to content

fix(test): eliminate four macOS release-qualification shard flakes - #10256

Merged
loopx-agent merged 1 commit into
mainfrom
codex/fix-macos-qualification-flakes-20261206
Dec 6, 2026
Merged

loopx-agent merged 1 commit into
mainfrom
codex/fix-macos-qualification-flakes-20261206

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Delivery Brief

  • Goal / Source: Eliminate the four deterministic/concurrency flakes observed during macOS single-wave release qualification (scripts/qualify_release_commit.py).
  • Observed Gaps & Root Causes:
    1. Cross-test monkeypatch capture in benchmark/runtime/planning_batch.py (and benchmark/runtime/sforge_entry.py): When benchmark/tests/test_resume_sessions.py ran earlier in the same pytest shard, it monkeypatched benchmark.runtime.planning.task_plan_packet to a stub and triggered the first import of benchmark.runtime.planning_batch, which bound from .planning import task_plan_packet at module level. After monkeypatch teardown, planning_batch stayed permanently poisoned with the stub for subsequent tests in the same shard (test_planning_batch_preserves_upstream_error_detail_on_refine_rejection failed with KeyError: 'stage' and test_planning_batch_packet_survives_gbk_parent_locale[file] failed with KeyError: 'goal_id').
    2. Tight subprocess spawn budget in benchmark/tests/test_dual_model_microbench.py: test_codex_runner_enforces_single_arm_deadline_across_setup_and_subprocess Subcase B gave timeout_seconds=1.00 minus 0.35s simulated prepare (0.65s remaining), causing CPython spawn under concurrent shard load to time out (codex_timeout); Subcase C used timeout_seconds=0.05 across real prepare_codex_home.
    3. Non-atomic readiness file writes in tests/canary/test_telemetry_isolation.py: test_smoke_failure_reaps_children_before_cleaning_fixture wrote child PID via ready.write_text(...) while the parent polled ready.exists(), letting the parent observe a 0-byte ready file on cancellation and fail with ValueError: invalid literal for int() with base 10: '' (same race on marker in test_smoke_retires_every_owned_runtime_revision).
    4. Long macOS /var/folders/... shard temp roots in scripts/qualify_release_commit.py: Per-shard and preflight namespaces called tempfile.mkdtemp(prefix=...) directly under macOS's 49-character /var/folders/.../T/ default TMPDIR, needlessly inflating child TMPDIR, HOME, and POSIX socket paths.
  • Changes:
    • benchmark/runtime/planning_batch.py, benchmark/runtime/sforge_entry.py: import .planning as _planning and resolve _planning.task_plan_packet / _planning.validate_plan_readback dynamically at call time so import-time monkeypatches never leak across tests.
    • tests/control_plane/test_benchmark_planning_batch.py: add regression test test_planning_batch_resolves_live_task_plan_packet_after_import_time_patch verifying reload-under-monkeypatch does not poison subsequent calls.
    • benchmark/tests/test_dual_model_microbench.py: scale Subcase B to timeout_seconds=5.00 with 1.75s prepare (pytest.approx(3.25, abs=0.05) remaining) and Subcase C to timeout_seconds=0.25 with FAKE_CODEX_SLEEP_SECONDS="2.0", preserving the exact single-arm deadline subtraction invariant.
    • tests/canary/test_telemetry_isolation.py: write ready (and marker) atomically via sibling .tmp + os.replace, and guard finally cleanup 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 /tmp on POSIX when the default temp dir exceeds 32 chars and TMPDIR is not explicitly short; cover all branches with a portable unit test.
  • Owning Boundary: Test reliability, benchmark runtime helper import hygiene (no scoring/task/permission/control-plane semantics changed), and release-qualification shard isolation.

Verification

  • Exact shard-order reproducer (test_resume_sessions.py::test_planning_heartbeat_and_turn_share_one_binding[None-heartbeat] + test_benchmark_planning_batch.py, previously failing 2 failed, 1 passed in 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_required due to benchmark_sensitive surface classification; no scoring/task/permission behavior changed).

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/ 控制面或前端。

  1. 消除导入期 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 后后续测试仍能调用真实实现,完全消除分片内跨用例顺序依赖。
  2. 保持单臂 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 验证。
  3. 就绪文件原子发布:tests/canary/test_telemetry_isolation.py 中子进程改为先写同目录 .tmp 临时文件再 os.replace 到 ready(以及 marker),消除父进程在 exists() 为真但文件内容仍为空的瞬时窗口读到空字符串引发 ValueError 的竞态,同时在 finally 清理段对空文本做防御校验。
  4. 缩短 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 /tmp 8 条分支。

对主干的风险 / 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 C 0.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.

@loopx-agent
loopx-agent merged commit 6ad1583 into main Dec 6, 2026
5 of 6 checks passed
@loopx-agent
loopx-agent deleted the codex/fix-macos-qualification-flakes-20261206 branch December 6, 2026 06:59
maxliux5 added a commit that referenced this pull request Dec 6, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant