diff --git a/docs/product/release-readiness.md b/docs/product/release-readiness.md index a5b679cc29..6306f86c51 100644 --- a/docs/product/release-readiness.md +++ b/docs/product/release-readiness.md @@ -1085,11 +1085,25 @@ After the individual lanes pass, bind their compact receipts to the exact clean release checkout before tagging or moving `stable`: ```bash +python3 scripts/qualify_release_commit.py \ + --repo-root . \ + --output-dir .local/release-qualification \ + --preflight-since v2.3.0 \ + --resume loopx canary release-qualification \ --manifest-json release-qualification.json \ --repo-root . ``` +For clean full-suite release runs, `scripts/qualify_release_commit.py` runs an +optional changed-file preflight first (`--preflight-since`), then executes +hardware-sized single-wave isolated pytest shards (defaulting to +two-thirds of host CPUs capped at 12, with optional `--durations-path`, +`--durations-out`, and fail-closed exact-commit/tree `--resume`) and reduces +the shards into `receipt-pytest.json` with no local paths. Keep +`scripts/qualify-doubao-model-behavior-live.py` (`--max-workers 6` by default) +concurrent with network/CPU lanes without mutating the clean candidate tree. + The `exact_release_commit_qualification_manifest_v0` contract requires the same Git commit, Git tree id, package version, and version tag across pytest, Ruff, mypy, risk-based canary, full-public, install/upgrade/host, diff --git a/docs/product/releases/v2.4.0.md b/docs/product/releases/v2.4.0.md index 829365a5fb..57218f78ee 100644 --- a/docs/product/releases/v2.4.0.md +++ b/docs/product/releases/v2.4.0.md @@ -85,7 +85,7 @@ loopx authority-archive upgrade --all-known --require-current - **SQLite reliability & ENOSPC matrix:** Expanded regression coverage across lagging persisted-cursor consumers, backup/restore lineage, concurrent CAS and lost-response retries, and disk-full injection (`SQLITE_FULL` and real macOS `ENOSPC` sparse volume fail-closed recovery). [#8367](https://github.com/loopx-project/loopx/pull/8367), [#8482](https://github.com/loopx-project/loopx/pull/8482), [#8498](https://github.com/loopx-project/loopx/pull/8498), [#9126](https://github.com/loopx-project/loopx/pull/9126) - **Type safety, CLI modularization & test isolation:** Eliminated `Any` annotations with ratchet baselines across control-plane, quota, Lark, reward-memory, benchmark, and domain-pack modules (`#8971`–`#8993`, `#8013`), modularized large CLI, `issue-fix`, and Lark/Chat modules (including internal typed `loopx/chat/` action grouping `#9056`), pruned ceremonial/obsolete smoke stacks (`#8883`, `#8884`, `#9073`), reclaimed completed and read-only pytest temporary trees cleanly (`#9070`, `#9189`), pinned C1/Complete Turn output budgets, CLI help catalogs, architecture import guards, onboarding gates, UTF-8 host transports, and settlement tests (`#8940`, `#8994`, `#9010`, `#9019`, `#9025`, `#9042`, `#9044`, `#9048`, `#9052`, `#9102`, `#9104`, `#9152`, `#9154`, `#9156`, `#9158`, `#9160`, `#9161`), and bound `doctor` package update readback to its owning Python environment (`#9009`). [#8013](https://github.com/loopx-project/loopx/pull/8013), [#8897](https://github.com/loopx-project/loopx/pull/8897), [#8933](https://github.com/loopx-project/loopx/pull/8933), [#8940](https://github.com/loopx-project/loopx/pull/8940), [#8971](https://github.com/loopx-project/loopx/pull/8971), [#8994](https://github.com/loopx-project/loopx/pull/8994), [#9009](https://github.com/loopx-project/loopx/pull/9009), [#9044](https://github.com/loopx-project/loopx/pull/9044), [#9056](https://github.com/loopx-project/loopx/pull/9056), [#9070](https://github.com/loopx-project/loopx/pull/9070), [#9102](https://github.com/loopx-project/loopx/pull/9102), [#9104](https://github.com/loopx-project/loopx/pull/9104), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9154](https://github.com/loopx-project/loopx/pull/9154), [#9156](https://github.com/loopx-project/loopx/pull/9156), [#9158](https://github.com/loopx-project/loopx/pull/9158), [#9160](https://github.com/loopx-project/loopx/pull/9160), [#9161](https://github.com/loopx-project/loopx/pull/9161), [#9189](https://github.com/loopx-project/loopx/pull/9189) -- **Canary & release qualification gates:** Strengthened base/head CLI probe and budget-baseline isolation (`#8948`, `#9103`), complete C1 input routing for external-wait host prompt probes (`#8981`), peer-runtime controller-ownership classification in canary checks (`#9092`), runtime snapshot qualification (`#8919`, `#8922`, `#8952`, `#9018`), canary timeout/incomplete diagnostics (`#9001`), Git pathname preservation (`#8426`, `#8429`, `#8558`, `#8585`, `#8645`, `#8678`), unified local-path URL vs `path:` verdicts across presentation and public-output guards (`#9080`, preserving bare-root inline code examples in `#9190`), and local-first verification and public smoke workflows (`#9040`, `#9152`), alongside `--max-workers` concurrent live portfolio qualification, an `8 KiB` host-tool error output cap preserving deep-path replan context recovery, digest consumer manifest & multi-goal bootstrap smoke alignment, standalone `test_effect_turn_live_quota_decision` import qualification (`#9168`), `--settle-turn` vision-closeout receipt / creation-allowance wakeup race hardening (`#9178`), lazy resumed task-lease rebinding plus goal-frontier maintainability extraction (`#9183`), and unmanaged Lark poll retirement / `generic-cli` validator lease gating fixes (`#9190`). [#8426](https://github.com/loopx-project/loopx/pull/8426), [#8645](https://github.com/loopx-project/loopx/pull/8645), [#8919](https://github.com/loopx-project/loopx/pull/8919), [#8922](https://github.com/loopx-project/loopx/pull/8922), [#8948](https://github.com/loopx-project/loopx/pull/8948), [#8952](https://github.com/loopx-project/loopx/pull/8952), [#8981](https://github.com/loopx-project/loopx/pull/8981), [#9001](https://github.com/loopx-project/loopx/pull/9001), [#9018](https://github.com/loopx-project/loopx/pull/9018), [#9040](https://github.com/loopx-project/loopx/pull/9040), [#9080](https://github.com/loopx-project/loopx/pull/9080), [#9092](https://github.com/loopx-project/loopx/pull/9092), [#9103](https://github.com/loopx-project/loopx/pull/9103), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9168](https://github.com/loopx-project/loopx/pull/9168), [#9178](https://github.com/loopx-project/loopx/pull/9178), [#9183](https://github.com/loopx-project/loopx/pull/9183), [#9190](https://github.com/loopx-project/loopx/pull/9190) +- **Canary & release qualification gates:** Strengthened base/head CLI probe and budget-baseline isolation (`#8948`, `#9103`), complete C1 input routing for external-wait host prompt probes (`#8981`), peer-runtime controller-ownership classification in canary checks (`#9092`), runtime snapshot qualification (`#8919`, `#8922`, `#8952`, `#9018`), canary timeout/incomplete diagnostics (`#9001`), Git pathname preservation (`#8426`, `#8429`, `#8558`, `#8585`, `#8645`, `#8678`), unified local-path URL vs `path:` verdicts across presentation and public-output guards (`#9080`, preserving bare-root inline code examples in `#9190`), and local-first verification and public smoke workflows (`#9040`, `#9152`), alongside `--max-workers` concurrent live portfolio qualification, an `8 KiB` host-tool error output cap preserving deep-path replan context recovery, digest consumer manifest & multi-goal bootstrap smoke alignment, standalone `test_effect_turn_live_quota_decision` import qualification (`#9168`), `--settle-turn` vision-closeout receipt / creation-allowance wakeup race hardening (`#9178`), lazy resumed task-lease rebinding plus goal-frontier maintainability extraction (`#9183`), and unmanaged Lark poll retirement / `generic-cli` validator lease gating fixes (`#9190`), plus pruned pytest directory discovery, parallel-default live model qualification, and single-wave clean-checkout pytest sharding (`#9193`). [#8426](https://github.com/loopx-project/loopx/pull/8426), [#8645](https://github.com/loopx-project/loopx/pull/8645), [#8919](https://github.com/loopx-project/loopx/pull/8919), [#8922](https://github.com/loopx-project/loopx/pull/8922), [#8948](https://github.com/loopx-project/loopx/pull/8948), [#8952](https://github.com/loopx-project/loopx/pull/8952), [#8981](https://github.com/loopx-project/loopx/pull/8981), [#9001](https://github.com/loopx-project/loopx/pull/9001), [#9018](https://github.com/loopx-project/loopx/pull/9018), [#9040](https://github.com/loopx-project/loopx/pull/9040), [#9080](https://github.com/loopx-project/loopx/pull/9080), [#9092](https://github.com/loopx-project/loopx/pull/9092), [#9103](https://github.com/loopx-project/loopx/pull/9103), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9168](https://github.com/loopx-project/loopx/pull/9168), [#9178](https://github.com/loopx-project/loopx/pull/9178), [#9183](https://github.com/loopx-project/loopx/pull/9183), [#9190](https://github.com/loopx-project/loopx/pull/9190), [#9193](https://github.com/loopx-project/loopx/pull/9193) ## Benchmarks & Integrations @@ -474,7 +474,7 @@ loopx doctor - SQLite 回归矩阵覆盖落后消费者、备份 lineage、并发 CAS/响应丢失重试以及 `SQLITE_FULL` / 真实 macOS `ENOSPC` 磁盘满 fail-closed 与恢复。 [#8367](https://github.com/loopx-project/loopx/pull/8367), [#8482](https://github.com/loopx-project/loopx/pull/8482), [#8498](https://github.com/loopx-project/loopx/pull/8498), [#9126](https://github.com/loopx-project/loopx/pull/9126) - 控制面、配额、Lark、奖励记忆、基准与领域包消除 `Any` 并建立基线护栏,大体积 CLI、`issue-fix` 与 Lark/Chat 模块完成拆分(含内部 `loopx/chat/` typed action 分组 `#9056`),及时回收已通过与只读的 pytest 临时目录树(`#9070`, `#9189`),补齐 C1 规划/交付/结算与 UTF-8 传输/扩展/工作台回归,加固 CLI help 目录、架构导入守卫与新手引导选标门禁,精简冗余示例,并使 `doctor` 升级读回绑定所属 Python 环境。 [#8013](https://github.com/loopx-project/loopx/pull/8013), [#8897](https://github.com/loopx-project/loopx/pull/8897), [#8933](https://github.com/loopx-project/loopx/pull/8933), [#8940](https://github.com/loopx-project/loopx/pull/8940), [#8971](https://github.com/loopx-project/loopx/pull/8971), [#8994](https://github.com/loopx-project/loopx/pull/8994), [#9009](https://github.com/loopx-project/loopx/pull/9009), [#9044](https://github.com/loopx-project/loopx/pull/9044), [#9056](https://github.com/loopx-project/loopx/pull/9056), [#9070](https://github.com/loopx-project/loopx/pull/9070), [#9102](https://github.com/loopx-project/loopx/pull/9102), [#9104](https://github.com/loopx-project/loopx/pull/9104), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9154](https://github.com/loopx-project/loopx/pull/9154), [#9156](https://github.com/loopx-project/loopx/pull/9156), [#9158](https://github.com/loopx-project/loopx/pull/9158), [#9160](https://github.com/loopx-project/loopx/pull/9160), [#9161](https://github.com/loopx-project/loopx/pull/9161), [#9189](https://github.com/loopx-project/loopx/pull/9189) -- 加固 base/head CLI 差分与预算基线隔离(`#8948`、`#9103`)、外部等待宿主提示词探针走完整 C1 输入(`#8981`)、canary 对等运行时检查正确识别 controller 归属(`#9092`)、统一展示与公开输出边界对本地路径 URL 与 `path:` 的判定(`#9080`,并在 `#9190` 保留行内代码根路径示例与同步 DSH 插件观察器规则)、runtime snapshot 默认契约校验、canary 超时诊断与 Git 原始路径保留,并沉淀本地优先验证规范与公开 smoke 门禁修复(`#9152`),同时支持 `--max-workers` 并发真实模型行为评估、深路径重规划上下文恢复的 `8 KiB` 宿主工具错误输出上限、对齐摘要消费者清单、多 Goal 引导 smoke 契约与 `test_effect_turn_live_quota_decision` 独立导入(`#9168`)、修复 `--settle-turn` 视觉结案回执读取及 `creation_allowance` 测试唤醒竞态(`#9178`),并实现按需惰性重绑定恢复租约以保全已提交 Turn 重放与已退役租约结案恢复、收敛 goal-frontier 投影可维护性圈复杂度(`#9183`)以及修复非托管飞书轮询清理与 `generic-cli` 独立校验器租约门控(`#9190`)。 [#8426](https://github.com/loopx-project/loopx/pull/8426), [#8645](https://github.com/loopx-project/loopx/pull/8645), [#8919](https://github.com/loopx-project/loopx/pull/8919), [#8922](https://github.com/loopx-project/loopx/pull/8922), [#8948](https://github.com/loopx-project/loopx/pull/8948), [#8952](https://github.com/loopx-project/loopx/pull/8952), [#8981](https://github.com/loopx-project/loopx/pull/8981), [#9001](https://github.com/loopx-project/loopx/pull/9001), [#9018](https://github.com/loopx-project/loopx/pull/9018), [#9040](https://github.com/loopx-project/loopx/pull/9040), [#9080](https://github.com/loopx-project/loopx/pull/9080), [#9092](https://github.com/loopx-project/loopx/pull/9092), [#9103](https://github.com/loopx-project/loopx/pull/9103), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9168](https://github.com/loopx-project/loopx/pull/9168), [#9178](https://github.com/loopx-project/loopx/pull/9178), [#9183](https://github.com/loopx-project/loopx/pull/9183), [#9190](https://github.com/loopx-project/loopx/pull/9190) +- 加固 base/head CLI 差分与预算基线隔离(`#8948`、`#9103`)、外部等待宿主提示词探针走完整 C1 输入(`#8981`)、canary 对等运行时检查正确识别 controller 归属(`#9092`)、统一展示与公开输出边界对本地路径 URL 与 `path:` 的判定(`#9080`,并在 `#9190` 保留行内代码根路径示例与同步 DSH 插件观察器规则)、runtime snapshot 默认契约校验、canary 超时诊断与 Git 原始路径保留,并沉淀本地优先验证规范与公开 smoke 门禁修复(`#9152`),同时支持 `--max-workers` 并发真实模型行为评估、深路径重规划上下文恢复的 `8 KiB` 宿主工具错误输出上限、对齐摘要消费者清单、多 Goal 引导 smoke 契约与 `test_effect_turn_live_quota_decision` 独立导入(`#9168`)、修复 `--settle-turn` 视觉结案回执读取及 `creation_allowance` 测试唤醒竞态(`#9178`),并实现按需惰性重绑定恢复租约以保全已提交 Turn 重放与已退役租约结案恢复、收敛 goal-frontier 投影可维护性圈复杂度(`#9183`)以及修复非托管飞书轮询清理与 `generic-cli` 独立校验器租约门控(`#9190`)并收敛 pytest 目录发现、并发默认真实模型评估与单波干净检出分片验收(`#9193`)。 [#8426](https://github.com/loopx-project/loopx/pull/8426), [#8645](https://github.com/loopx-project/loopx/pull/8645), [#8919](https://github.com/loopx-project/loopx/pull/8919), [#8922](https://github.com/loopx-project/loopx/pull/8922), [#8948](https://github.com/loopx-project/loopx/pull/8948), [#8952](https://github.com/loopx-project/loopx/pull/8952), [#8981](https://github.com/loopx-project/loopx/pull/8981), [#9001](https://github.com/loopx-project/loopx/pull/9001), [#9018](https://github.com/loopx-project/loopx/pull/9018), [#9040](https://github.com/loopx-project/loopx/pull/9040), [#9080](https://github.com/loopx-project/loopx/pull/9080), [#9092](https://github.com/loopx-project/loopx/pull/9092), [#9103](https://github.com/loopx-project/loopx/pull/9103), [#9152](https://github.com/loopx-project/loopx/pull/9152), [#9168](https://github.com/loopx-project/loopx/pull/9168), [#9178](https://github.com/loopx-project/loopx/pull/9178), [#9183](https://github.com/loopx-project/loopx/pull/9183), [#9190](https://github.com/loopx-project/loopx/pull/9190), [#9193](https://github.com/loopx-project/loopx/pull/9193) ### 基准与集成 diff --git a/pyproject.toml b/pyproject.toml index e0f4a62e08..a5b3231a8f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -203,7 +203,7 @@ include = ["loopx*"] [tool.pytest.ini_options] markers = ["stage2c_e2e: real process Stage 2C correctness and recovery acceptance"] # Private snapshots and packaging copies are not source tests; optional provider suites have dedicated workflows. -norecursedirs = [".local", "build", "dist", "deprecate", "packages/loopx-ark-turn", "packages/loopx-memt-turn"] +norecursedirs = [".*", "*.egg", ".local", "build", "dist", "deprecate", "node_modules", "venv", "packages/loopx-ark-turn", "packages/loopx-memt-turn"] [tool.coverage.run] source = ["loopx"] diff --git a/scripts/qualify-doubao-model-behavior-live.py b/scripts/qualify-doubao-model-behavior-live.py index 416b10d8d7..2115521abb 100755 --- a/scripts/qualify-doubao-model-behavior-live.py +++ b/scripts/qualify-doubao-model-behavior-live.py @@ -57,7 +57,7 @@ def _parser() -> argparse.ArgumentParser: parser.add_argument("--timeout-seconds", type=float, default=90.0) parser.add_argument("--required-vision-timeout-seconds", type=float, default=180.0) parser.add_argument("--repo-root", type=Path, default=Path.cwd()) - parser.add_argument("--max-workers", type=int, default=1) + parser.add_argument("--max-workers", type=int, default=6) return parser diff --git a/scripts/qualify_release_commit.py b/scripts/qualify_release_commit.py new file mode 100644 index 0000000000..b9e57816f8 --- /dev/null +++ b/scripts/qualify_release_commit.py @@ -0,0 +1,755 @@ +#!/usr/bin/env python3 +"""Run isolated, exact-commit pytest preflight and single-wave shard qualification.""" + +from __future__ import annotations + +import argparse +import concurrent.futures +import fnmatch +import hashlib +import importlib.util +import json +import os +import shutil +import stat +import subprocess +import sys +import tempfile +import time +import tomllib +import xml.etree.ElementTree as ET +from collections.abc import Mapping, Sequence +from datetime import datetime, timezone +from pathlib import Path +from typing import Any + +REPO_ROOT = Path(__file__).resolve().parents[1] +if sys.path[0] != str(REPO_ROOT): + sys.path.insert(0, str(REPO_ROOT)) + +# Ensure the checked-out source tree beats any foreign installed loopx package. +from loopx.control_plane.testing.release_commit_qualification import ( # noqa: E402 + EXACT_RELEASE_CHECK_RECEIPT_SCHEMA_VERSION, + EXPECTED_RESULT_SCHEMA_BY_QUALIFICATION, + collect_release_source_identity, +) + +PYTEST_SUMMARY_SCHEMA_VERSION = EXPECTED_RESULT_SCHEMA_BY_QUALIFICATION["pytest"] +_STRIPPED_ENV_PREFIXES = ("CODEX_", "LOOPX_", "ANTIGRAVITY_") +_MAX_DEFAULT_SHARDS = 12 +_PUBLIC_SAFETY_SURFACE_PREFIXES = ( + "loopx/public_safe_text.py", + "loopx/chat.py", + "packages/dsh-loopx-plugin/src/observer.ts", + "packages/dsh-loopx-plugin/tests/observer.spec.ts", +) +_PUBLIC_SAFETY_TESTS = ( + "tests/capabilities/test_reliability_diagnostics_dsh_provider.py", + "tests/control_plane/test_public_safe_local_path_owner.py", + "tests/test_chat_path_redaction.py", +) + + +def default_pytest_parallelism(cpu_count: int | None = None) -> int: + """Choose single-wave shard concurrency sized to two-thirds of host CPUs (1..12).""" + cpus = cpu_count if cpu_count is not None else (os.cpu_count() or 4) + if cpus < 1: + cpus = 1 + return max(1, min(_MAX_DEFAULT_SHARDS, round(cpus * 2 / 3))) + + +def _canonical_sha256(payload: Mapping[str, Any]) -> str: + encoded = json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8") + return f"sha256:{hashlib.sha256(encoded).hexdigest()}" + + +def _is_direct_test_path(rel_path: str) -> bool: + name = Path(rel_path).name + if not name.endswith(".py") or not (name.startswith("test_") or name.endswith("_test.py")): + return False + return rel_path.startswith(("tests/", "benchmark/", "packages/", "scripts/")) + + +def _load_pytest_norecursedirs(repo_root: Path) -> tuple[str, ...]: + pyproject = repo_root / "pyproject.toml" + if not pyproject.is_file(): + return () + try: + data = tomllib.loads(pyproject.read_text(encoding="utf-8")) + except (OSError, tomllib.TOMLDecodeError): + return () + tool = data.get("tool") + if not isinstance(tool, Mapping): + return () + pytest_cfg = tool.get("pytest") + if not isinstance(pytest_cfg, Mapping): + return () + ini_options = pytest_cfg.get("ini_options") + if not isinstance(ini_options, Mapping): + return () + raw = ini_options.get("norecursedirs") + if not isinstance(raw, list): + return () + patterns: list[str] = [] + for item in raw: + if isinstance(item, str): + normalized = item.strip().replace("\\", "/").strip("/") + if normalized: + patterns.append(normalized) + return tuple(patterns) + + +def _is_excluded_by_norecursedirs(rel_path: str, patterns: Sequence[str]) -> bool: + normalized = rel_path.replace("\\", "/").lstrip("./").strip("/") + if not normalized or not patterns: + return False + parts = Path(normalized).parent.parts + if not parts: + return False + dir_path = "/".join(parts) + for raw_pattern in patterns: + pattern = raw_pattern.replace("\\", "/").strip("/") + if not pattern: + continue + if "/" in pattern: + if dir_path == pattern or dir_path.startswith(f"{pattern}/"): + return True + elif any(fnmatch.fnmatchcase(part, pattern) for part in parts): + return True + return False + + +def select_preflight_tests( + changed_paths: Sequence[str], + *, + repo_root: Path, +) -> list[str]: + """Map changed repository paths to existing high-signal pytest modules.""" + norecursedirs = _load_pytest_norecursedirs(repo_root) + selected: list[str] = [] + seen: set[str] = set() + + def add(rel_path: str) -> None: + normalized = rel_path.replace("\\", "/").lstrip("./") + if normalized in seen or _is_excluded_by_norecursedirs(normalized, norecursedirs): + return + if (repo_root / normalized).is_file(): + seen.add(normalized) + selected.append(normalized) + + for raw in changed_paths: + rel = raw.replace("\\", "/").lstrip("./") + if not rel or _is_excluded_by_norecursedirs(rel, norecursedirs): + continue + if _is_direct_test_path(rel): + add(rel) + if any(rel.startswith(prefix) for prefix in _PUBLIC_SAFETY_SURFACE_PREFIXES): + for test_path in _PUBLIC_SAFETY_TESTS: + add(test_path) + if rel.startswith("loopx/cli_commands/turn"): + add("tests/test_loopx_turn_driver.py") + if rel.startswith("loopx/extensions/lark/"): + add("tests/extensions/test_lark_realtime_dispatch.py") + add("tests/test_chat_startup_isolation.py") + if rel == "loopx/registry.py": + add("tests/test_registry_git_probe.py") + if rel in { + "scripts/qualify_release_commit.py", + "scripts/qualify-doubao-model-behavior-live.py", + "loopx/control_plane/testing/release_commit_qualification.py", + }: + add("tests/control_plane/test_release_commit_qualification.py") + add("tests/control_plane/test_actual_default_model_behavior_live.py") + if rel == "pyproject.toml": + add("tests/test_python_ci_workflow.py") + if rel.endswith(".py") and rel.startswith(("loopx/", "scripts/")): + stem = Path(rel).stem + if stem != "__init__" and len(stem) >= 4: + for directory in ( + "tests", + "tests/control_plane", + "tests/capabilities", + "tests/extensions", + "tests/cli_commands", + ): + add(f"{directory}/test_{stem}.py") + return selected + + +def git_changed_paths_since(repo_root: Path, git_ref: str) -> list[str]: + completed = subprocess.run( + ["git", "-C", str(repo_root), "diff", "--name-only", f"{git_ref}..HEAD"], + check=True, + capture_output=True, + text=True, + ) + return [line.strip() for line in completed.stdout.splitlines() if line.strip()] + + +def _load_runtime_stopper(repo_root: Path) -> Any: + helper = repo_root / "tests" / "managed_effect_runtime_fixture.py" + if not helper.is_file(): + return None + repo_text = str(repo_root) + if sys.path[0] != repo_text: + sys.path.insert(0, repo_text) + spec = importlib.util.spec_from_file_location("_qualify_runtime_cleanup", helper) + if spec is None or spec.loader is None: + return None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return getattr(module, "stop_disposable_effect_runtimes", None) + + +def _is_link_or_junction(path: Path | str) -> bool: + is_junction = getattr(os.path, "isjunction", None) + return os.path.islink(path) or bool(is_junction and is_junction(path)) + + +def _make_owned_tree_removable(root: Path) -> None: + """Restore owner access inside a script-owned tree without following links.""" + user_access = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR + root_stat = os.lstat(root) + if not stat.S_ISDIR(root_stat.st_mode): + return + os.chmod(root, stat.S_IMODE(root_stat.st_mode) | user_access) + for directory, subdirectories, files in os.walk(root, topdown=True, followlinks=False): + subdirectories[:] = [ + name + for name in subdirectories + if not _is_link_or_junction(os.path.join(directory, name)) + ] + for name in [directory, *(os.path.join(directory, item) for item in subdirectories)]: + try: + status = os.lstat(name) + if stat.S_ISDIR(status.st_mode): + os.chmod(name, stat.S_IMODE(status.st_mode) | user_access) + except FileNotFoundError: + continue + for name in files: + target = os.path.join(directory, name) + if _is_link_or_junction(target): + continue + try: + status = os.lstat(target) + # A hard link shares attributes with an entry outside this tree. + if stat.S_ISREG(status.st_mode) and status.st_nlink == 1: + os.chmod(target, stat.S_IMODE(status.st_mode) | stat.S_IWUSR) + except FileNotFoundError: + continue + + +def _remove_tree_tolerant(root: Path) -> None: + try: + if _is_link_or_junction(root): + root.unlink() + return + if not root.exists(): + return + try: + shutil.rmtree(root) + except OSError: + _make_owned_tree_removable(root) + shutil.rmtree(root) + except FileNotFoundError: + return + + +def _isolated_env( + root: Path, + *, + python_bin: Path, + extra_bin_dirs: Sequence[Path], +) -> dict[str, str]: + home = root / "home" + tmp = root / "tmp" + hypothesis_dir = root / "hypothesis" + for directory in ( + home, + home / "config", + home / "cache", + home / "data", + home / "state", + home / ".codex", + tmp, + root / "pytest-cache", + hypothesis_dir, + ): + directory.mkdir(parents=True, exist_ok=True) + gitconfig = root / "gitconfig" + gitconfig.write_text( + "[user]\n name = LoopX Release\n email = 337587101+loopx-agent@users.noreply.github.com\n", + encoding="utf-8", + ) + env = { + key: value + for key, value in os.environ.items() + if not key.startswith(_STRIPPED_ENV_PREFIXES) + } + path_parts = [str(python_bin.parent), *(str(item) for item in extra_bin_dirs)] + if env.get("PATH"): + path_parts.append(env["PATH"]) + env.update( + { + "PATH": os.pathsep.join(path_parts), + "HOME": str(home), + "USERPROFILE": str(home), + "TMPDIR": str(tmp), + "TMP": str(tmp), + "TEMP": str(tmp), + "XDG_CONFIG_HOME": str(home / "config"), + "XDG_CACHE_HOME": str(home / "cache"), + "XDG_DATA_HOME": str(home / "data"), + "XDG_STATE_HOME": str(home / "state"), + "CODEX_HOME": str(home / ".codex"), + "GIT_CONFIG_GLOBAL": str(gitconfig), + "GIT_CONFIG_NOSYSTEM": "1", + "LOOPX_USAGE_PING": "0", + "HYPOTHESIS_STORAGE_DIRECTORY": str(hypothesis_dir), + } + ) + if env.get("NODE_DISABLE_COMPILE_CACHE") != "1" and not env.get("NODE_V8_COVERAGE"): + node_compile_cache = root / "node-compile-cache" + node_compile_cache.mkdir(parents=True, exist_ok=True) + os.chmod(node_compile_cache, 0o700) + env["NODE_COMPILE_CACHE"] = str(node_compile_cache) + else: + env.pop("NODE_COMPILE_CACHE", None) + return env + + +def shard_selection_fingerprint( + *, + shards: int, + use_pytest_split: bool, + durations_path: Path | None, + extra_pytest_args: Sequence[str], +) -> str: + """Compute a deterministic fingerprint over shard-selection inputs for safe resume.""" + durations_sha256: str | None = None + if durations_path is not None and durations_path.is_file(): + durations_sha256 = hashlib.sha256(durations_path.read_bytes()).hexdigest() + return _canonical_sha256( + { + "shards": shards, + "use_pytest_split": use_pytest_split, + "durations_sha256": durations_sha256, + "extra_pytest_args": list(extra_pytest_args), + } + ) + + +def can_reuse_shard_receipt( + summary_path: Path, + xml_path: Path, + *, + expected_source: Mapping[str, Any], + expected_groups: int, + expected_selection_fingerprint: str | None = None, +) -> bool: + """Reuse a shard receipt only when commit, tree, clean status, and selection match.""" + if not summary_path.is_file() or not xml_path.is_file(): + return False + if expected_source.get("git_dirty") is not False: + return False + try: + data = json.loads(summary_path.read_text(encoding="utf-8")) + except (OSError, json.JSONDecodeError): + return False + if not isinstance(data, Mapping): + return False + if data.get("exit_code") != 0 or data.get("groups") != expected_groups: + return False + if ( + expected_selection_fingerprint is not None + and data.get("selection_fingerprint") != expected_selection_fingerprint + ): + return False + expected = dict(expected_source) + return data.get("source") == expected and data.get("current_source") == expected + + +def summarize_junit_xml_files(xml_paths: Sequence[Path]) -> dict[str, int]: + """Aggregate passed, failed+error, and skipped counts across pytest JUnit XML files.""" + total = 0 + failed = 0 + skipped = 0 + for xml_path in xml_paths: + if not xml_path.is_file(): + continue + root = ET.parse(xml_path).getroot() + suites = [root] if root.tag == "testsuite" else root.findall(".//testsuite") + for suite in suites: + tests = int(suite.attrib.get("tests", 0)) + failures = int(suite.attrib.get("failures", 0)) + errors = int(suite.attrib.get("errors", 0)) + suite_skipped = int(suite.attrib.get("skipped", 0)) + total += tests + failed += failures + errors + skipped += suite_skipped + passed = max(0, total - failed - skipped) + return {"passed_count": passed, "failed_count": failed, "skipped_count": skipped} + + +def _require_clean_source(repo_root: Path) -> dict[str, Any]: + source = collect_release_source_identity(repo_root) + if source["git_dirty"]: + raise RuntimeError("clean candidate checkout required (git_dirty must be false)") + return source + + +def run_preflight( + *, + repo_root: Path, + output_dir: Path, + python_bin: Path, + extra_bin_dirs: Sequence[Path], + git_ref: str, + runtime_stopper: Any, +) -> dict[str, Any]: + changed = git_changed_paths_since(repo_root, git_ref) + selected = select_preflight_tests(changed, repo_root=repo_root) + receipt: dict[str, Any] = { + "git_ref": git_ref, + "changed_file_count": len(changed), + "selected_tests": selected, + "exit_code": 0, + "wall_seconds": 0.0, + } + if not selected: + (output_dir / "preflight-summary.json").write_text( + json.dumps(receipt, indent=2) + "\n", encoding="utf-8" + ) + return receipt + root = Path(tempfile.mkdtemp(prefix="lxq-preflight-")).resolve() + env = _isolated_env(root, python_bin=python_bin, extra_bin_dirs=extra_bin_dirs) + cmd = [ + str(python_bin), + "-m", + "pytest", + "-q", + "-o", + "tmp_path_retention_policy=failed", + "-o", + f"cache_dir={root / 'pytest-cache'}", + "--junitxml=" + str(output_dir / "preflight.xml"), + *selected, + ] + start = time.perf_counter() + try: + with (output_dir / "preflight.log").open("w", encoding="utf-8") as stream: + result = subprocess.run( + cmd, cwd=repo_root, env=env, stdout=stream, stderr=subprocess.STDOUT, check=False + ) + finally: + if runtime_stopper is not None: + runtime_stopper(root) + receipt["wall_seconds"] = time.perf_counter() - start + receipt["exit_code"] = result.returncode + if result.returncode == 0: + _remove_tree_tolerant(root) + (output_dir / "preflight-summary.json").write_text( + json.dumps(receipt, indent=2) + "\n", encoding="utf-8" + ) + return receipt + + +def _run_shard( + *, + group: int, + shards: int, + repo_root: Path, + output_dir: Path, + python_bin: Path, + extra_bin_dirs: Sequence[Path], + source: Mapping[str, Any], + selection_fingerprint: str, + durations_path: Path | None, + store_durations: bool, + use_pytest_split: bool, + extra_pytest_args: Sequence[str], + keep_failed_namespaces: bool, + runtime_stopper: Any, + resume: bool, +) -> tuple[dict[str, Any], Path | None]: + name = f"pytest-shard-{group}" + summary_path = output_dir / f"{name}-process-summary.json" + xml_path = output_dir / f"{name}.xml" + if resume and can_reuse_shard_receipt( + summary_path, + xml_path, + expected_source=source, + expected_groups=shards, + expected_selection_fingerprint=selection_fingerprint, + ): + cached = dict(json.loads(summary_path.read_text(encoding="utf-8"))) + cached["resumed"] = True + return cached, None + + root = Path(tempfile.mkdtemp(prefix=f"lxq-{group}-")).resolve() + env = _isolated_env(root, python_bin=python_bin, extra_bin_dirs=extra_bin_dirs) + shard_durations_file: Path | None = None + cmd = [ + str(python_bin), + "-m", + "pytest", + "-q", + "-o", + "tmp_path_retention_policy=failed", + "-o", + f"cache_dir={root / 'pytest-cache'}", + ] + if (repo_root / "scripts" / "ci" / "pytest_timing.py").is_file(): + cmd.extend(["-p", "scripts.ci.pytest_timing", "--ci-stall-after", "120"]) + if use_pytest_split: + cmd.extend(["--splits", str(shards), "--group", str(group)]) + if store_durations: + shard_durations_file = output_dir / f".shard-{group}-durations.json" + if durations_path is not None and durations_path.is_file(): + shutil.copyfile(durations_path, shard_durations_file) + cmd.extend(["--splitting-algorithm", "least_duration"]) + cmd.extend( + [ + "--clean-durations", + "--store-durations", + f"--durations-path={shard_durations_file}", + ] + ) + elif durations_path is not None and durations_path.is_file(): + cmd.extend( + [ + "--splitting-algorithm", + "least_duration", + f"--durations-path={durations_path}", + ] + ) + elif shards > 1: + cmd.extend( + [ + "-p", + "scripts.ci.module_shard", + "--ci-module-shards", + str(shards), + "--ci-module-shard", + str(group), + ] + ) + cmd.extend(["--junitxml=" + str(xml_path), *extra_pytest_args]) + start = time.perf_counter() + try: + with (output_dir / f"{name}.log").open("w", encoding="utf-8") as stream: + result = subprocess.run( + cmd, cwd=repo_root, env=env, stdout=stream, stderr=subprocess.STDOUT, check=False + ) + wall_seconds = time.perf_counter() - start + completed_at_unix = time.time() + current_source = collect_release_source_identity(repo_root) + if current_source != dict(source): + raise RuntimeError(f"candidate source mutated during shard {group}") + finally: + if runtime_stopper is not None: + runtime_stopper(root) + if result.returncode == 0 or not keep_failed_namespaces: + _remove_tree_tolerant(root) + cleanup = "removed_after_runtime_shutdown" + else: + cleanup = "retained_for_diagnosis" + receipt: dict[str, Any] = { + "source": dict(source), + "current_source": current_source, + "selection_fingerprint": selection_fingerprint, + "group": group, + "groups": shards, + "exit_code": result.returncode, + "wall_seconds": wall_seconds, + "completed_at_unix": completed_at_unix, + "completed_at": datetime.fromtimestamp(completed_at_unix, timezone.utc).isoformat(), + "cleanup": cleanup, + "resumed": False, + "operator_state_used": False, + } + summary_path.write_text(json.dumps(receipt, indent=2) + "\n", encoding="utf-8") + return receipt, shard_durations_file + + +def _load_durations_mapping(file_path: Path) -> dict[str, float]: + if not file_path.is_file(): + return {} + data = json.loads(file_path.read_text(encoding="utf-8")) + if not isinstance(data, Mapping): + return {} + return { + key: float(value) + for key, value in data.items() + if isinstance(key, str) and isinstance(value, (int, float)) and not isinstance(value, bool) + } + + +def _merge_shard_durations( + duration_files: Sequence[Path], + out_path: Path, + *, + seed_durations_path: Path | None = None, + preserve_seed: bool = False, +) -> None: + merged: dict[str, float] = {} + if preserve_seed and seed_durations_path is not None: + merged.update(_load_durations_mapping(seed_durations_path)) + for file_path in sorted(duration_files): + merged.update(_load_durations_mapping(file_path)) + file_path.unlink(missing_ok=True) + out_path.parent.mkdir(parents=True, exist_ok=True) + out_path.write_text( + json.dumps(merged, indent=2, sort_keys=True) + "\n", encoding="utf-8" + ) + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description="Run isolated, single-wave exact-commit pytest release qualification." + ) + parser.add_argument("--repo-root", type=Path, default=Path.cwd()) + parser.add_argument("--output-dir", type=Path, required=True) + parser.add_argument("--python", type=Path, default=Path(sys.executable)) + parser.add_argument("--shards", type=int, default=default_pytest_parallelism()) + parser.add_argument("--max-workers", type=int, default=None) + parser.add_argument("--preflight-since", type=str, default=None) + parser.add_argument("--only-preflight", action="store_true") + parser.add_argument("--resume", action="store_true") + parser.add_argument("--durations-path", type=Path, default=None) + parser.add_argument("--durations-out", type=Path, default=None) + parser.add_argument("--no-pytest-split", action="store_true") + parser.add_argument("--extra-bin-dir", type=Path, action="append", default=[]) + parser.add_argument("--pytest-arg", type=str, action="append", default=[]) + parser.add_argument("--no-keep-failed-namespaces", action="store_true") + return parser + + +def main(argv: Sequence[str] | None = None) -> int: + os.umask(0o022) + parser = _parser() + args = parser.parse_args(argv) + if args.only_preflight and not args.preflight_since: + parser.error("--only-preflight requires --preflight-since") + if args.shards < 1: + raise ValueError("--shards must be at least 1") + workers = args.shards if args.max_workers is None else max(1, min(args.shards, args.max_workers)) + repo_root = args.repo_root.expanduser().resolve() + output_dir = args.output_dir.expanduser().resolve() + output_dir.mkdir(parents=True, exist_ok=True) + python_bin = Path(os.path.abspath(os.fspath(args.python.expanduser()))) + extra_bins = [path.expanduser().resolve() for path in args.extra_bin_dir] + + source = _require_clean_source(repo_root) + (output_dir / "source-identity.json").write_text( + json.dumps(source, indent=2) + "\n", encoding="utf-8" + ) + stopper = _load_runtime_stopper(repo_root) + + if args.preflight_since: + preflight = run_preflight( + repo_root=repo_root, + output_dir=output_dir, + python_bin=python_bin, + extra_bin_dirs=extra_bins, + git_ref=args.preflight_since, + runtime_stopper=stopper, + ) + if preflight["exit_code"] != 0 or args.only_preflight: + print(json.dumps({"source": source, "preflight": preflight}, indent=2)) + return int(preflight["exit_code"]) + + started = time.perf_counter() + results: list[dict[str, Any]] = [] + duration_files: list[Path] = [] + use_pytest_split = not args.no_pytest_split + store_durations = use_pytest_split and args.durations_out is not None + resolved_durations = ( + args.durations_path.expanduser().resolve() if args.durations_path else None + ) + selection_fp = shard_selection_fingerprint( + shards=args.shards, + use_pytest_split=use_pytest_split, + durations_path=resolved_durations, + extra_pytest_args=args.pytest_arg, + ) + with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as pool: + futures = { + pool.submit( + _run_shard, + group=group, + shards=args.shards, + repo_root=repo_root, + output_dir=output_dir, + python_bin=python_bin, + extra_bin_dirs=extra_bins, + source=source, + selection_fingerprint=selection_fp, + durations_path=resolved_durations, + store_durations=store_durations, + use_pytest_split=use_pytest_split, + extra_pytest_args=args.pytest_arg, + keep_failed_namespaces=not args.no_keep_failed_namespaces, + runtime_stopper=stopper, + resume=args.resume, + ): group + for group in range(1, args.shards + 1) + } + for future in concurrent.futures.as_completed(futures): + receipt, dur_file = future.result() + results.append(receipt) + if dur_file is not None: + duration_files.append(dur_file) + + if store_durations and args.durations_out is not None: + _merge_shard_durations( + duration_files, + args.durations_out.expanduser().resolve(), + seed_durations_path=resolved_durations, + preserve_seed=any(bool(row.get("resumed")) for row in results), + ) + + final_source = _require_clean_source(repo_root) + if final_source != source: + raise RuntimeError("candidate source changed during qualification") + + xml_paths = [output_dir / f"pytest-shard-{g}.xml" for g in range(1, args.shards + 1)] + counts = summarize_junit_xml_files(xml_paths) + all_ok = ( + all(path.is_file() for path in xml_paths) + and all(row["exit_code"] == 0 for row in results) + and counts["failed_count"] == 0 + ) + completed_at = datetime.now(timezone.utc).isoformat() + (output_dir / "pytest-summary.json").write_text( + json.dumps(counts, indent=2) + "\n", encoding="utf-8" + ) + receipt_payload = { + "schema_version": EXACT_RELEASE_CHECK_RECEIPT_SCHEMA_VERSION, + "source": source, + "status": "passed" if (all_ok and counts["passed_count"] > 0) else "failed", + "result_schema_version": PYTEST_SUMMARY_SCHEMA_VERSION, + "result_digest": _canonical_sha256(counts), + "completed_at": completed_at, + "summary": counts, + } + (output_dir / "receipt-pytest.json").write_text( + json.dumps(receipt_payload, indent=2) + "\n", encoding="utf-8" + ) + shards_summary = { + "source": source, + "shards": sorted(results, key=lambda row: int(row["group"])), + "passed": receipt_payload["status"] == "passed", + "wall_seconds": time.perf_counter() - started, + "completed_at": completed_at, + "summary": counts, + } + (output_dir / "pytest-shards-process-summary.json").write_text( + json.dumps(shards_summary, indent=2) + "\n", encoding="utf-8" + ) + print(json.dumps(receipt_payload, indent=2)) + return 0 if receipt_payload["status"] == "passed" else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/control_plane/test_release_commit_qualification.py b/tests/control_plane/test_release_commit_qualification.py index 8476486314..214d303c2f 100644 --- a/tests/control_plane/test_release_commit_qualification.py +++ b/tests/control_plane/test_release_commit_qualification.py @@ -451,3 +451,472 @@ def from_environment(**kwargs: object) -> object: assert json.loads(capsys.readouterr().out)["qualification_passed"] is True assert observed["DoubaoReplanSemanticActionBehaviorActor"] == vision_timeout assert all(observed[name] == ordinary_timeout for name in actor_names if name != "DoubaoReplanSemanticActionBehaviorActor") + + +def test_pyproject_pytest_norecursedirs_prunes_dotfiles_and_node_modules() -> None: + import tomllib + + config = tomllib.loads((REPO_ROOT / "pyproject.toml").read_text(encoding="utf-8")) + norecursedirs = config["tool"]["pytest"]["ini_options"]["norecursedirs"] + assert ".*" in norecursedirs + assert "node_modules" in norecursedirs + assert "packages/loopx-ark-turn" in norecursedirs + assert "packages/loopx-memt-turn" in norecursedirs + + +def test_live_doubao_script_defaults_max_workers_to_six_and_allows_serial( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + script = runpy.run_path(str(REPO_ROOT / "scripts" / "qualify-doubao-model-behavior-live.py")) + main = script["main"] + globals_ = main.__globals__ + observed_workers: list[int] = [] + actor_names = ( + "DoubaoModelBehaviorActor", + "DoubaoOnboardingModelBehaviorActor", + "DoubaoSelectedTodoToolBehaviorActor", + "DoubaoReplanSemanticActionBehaviorActor", + "DoubaoScopedGateSuccessorToolBehaviorActor", + "DoubaoCapabilityMonitorRepairToolBehaviorActor", + "DoubaoTerminalSettlementToolBehaviorActor", + ) + for name in actor_names: + monkeypatch.setitem( + globals_, + name, + SimpleNamespace(from_environment=lambda **_: SimpleNamespace(qualify=lambda **__: {})), + ) + monkeypatch.setitem(globals_, "collect_release_source_identity", lambda _root: {"git_dirty": False}) + monkeypatch.setitem(globals_, "build_actual_default_model_behavior_scenario_inputs", lambda _root: ({}, {})) + + def fake_portfolio(*_args: object, **kwargs: object) -> dict[str, object]: + observed_workers.append(int(kwargs["max_workers"])) + return {"qualification_passed": True} + + monkeypatch.setitem(globals_, "run_actual_default_model_behavior_portfolio", fake_portfolio) + + monkeypatch.setattr(sys, "argv", ["qualify-doubao-model-behavior-live.py", "--qualification-id", "default-workers"]) + assert main() == 0 + capsys.readouterr() + + monkeypatch.setattr( + sys, + "argv", + ["qualify-doubao-model-behavior-live.py", "--qualification-id", "serial-workers", "--max-workers", "1"], + ) + assert main() == 0 + capsys.readouterr() + assert observed_workers == [6, 1] + + +def test_qualify_release_commit_parallelism_and_preflight_selection(tmp_path: Path) -> None: + runner = runpy.run_path(str(REPO_ROOT / "scripts" / "qualify_release_commit.py")) + parallelism = runner["default_pytest_parallelism"] + assert parallelism(4) == 3 + assert parallelism(12) == 8 + assert parallelism(18) == 12 + assert parallelism(32) == 12 + assert parallelism(1) == 1 + + repo = tmp_path / "repo" + for rel in ( + "tests/capabilities/test_reliability_diagnostics_dsh_provider.py", + "tests/control_plane/test_public_safe_local_path_owner.py", + "tests/test_chat_path_redaction.py", + "tests/test_custom.py", + ): + target = repo / rel + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text("def test_ok(): pass\n", encoding="utf-8") + + selected = runner["select_preflight_tests"]( + [ + "packages/dsh-loopx-plugin/src/observer.ts", + "tests/test_custom.py", + "tests/test_missing.py", + ], + repo_root=repo, + ) + assert selected == [ + "tests/capabilities/test_reliability_diagnostics_dsh_provider.py", + "tests/control_plane/test_public_safe_local_path_owner.py", + "tests/test_chat_path_redaction.py", + "tests/test_custom.py", + ] + + +def test_qualify_release_commit_resume_requires_exact_source_and_reduces_junit(tmp_path: Path) -> None: + import importlib.util + + spec = importlib.util.spec_from_file_location( + "qualify_release_commit", REPO_ROOT / "scripts" / "qualify_release_commit.py" + ) + assert spec is not None and spec.loader is not None + runner = importlib.util.module_from_spec(spec) + spec.loader.exec_module(runner) + + summary_path = tmp_path / "pytest-shard-1-process-summary.json" + xml_path = tmp_path / "pytest-shard-1.xml" + xml_path.write_text( + '' + '' + "\n", + encoding="utf-8", + ) + expected = _source() + selection_fp = runner.shard_selection_fingerprint( + shards=12, use_pytest_split=True, durations_path=None, extra_pytest_args=[] + ) + summary_path.write_text( + json.dumps( + { + "source": expected, + "current_source": expected, + "selection_fingerprint": selection_fp, + "exit_code": 0, + "groups": 12, + } + ), + encoding="utf-8", + ) + assert ( + runner.can_reuse_shard_receipt( + summary_path, + xml_path, + expected_source=expected, + expected_groups=12, + expected_selection_fingerprint=selection_fp, + ) + is True + ) + assert ( + runner.can_reuse_shard_receipt( + summary_path, + xml_path, + expected_source=expected, + expected_groups=12, + expected_selection_fingerprint="sha256:" + "0" * 64, + ) + is False + ) + assert runner.can_reuse_shard_receipt(summary_path, xml_path, expected_source=expected, expected_groups=6) is False + assert ( + runner.can_reuse_shard_receipt( + summary_path, xml_path, expected_source=_source(commit="9" * 40), expected_groups=12 + ) + is False + ) + assert ( + runner.can_reuse_shard_receipt( + summary_path, xml_path, expected_source=_source(tree="9" * 40), expected_groups=12 + ) + is False + ) + assert ( + runner.can_reuse_shard_receipt( + summary_path, xml_path, expected_source=_source(dirty=True), expected_groups=12 + ) + is False + ) + + xml_two = tmp_path / "pytest-shard-2.xml" + xml_two.write_text( + '\n', + encoding="utf-8", + ) + counts = runner.summarize_junit_xml_files([xml_path, xml_two]) + assert counts == {"passed_count": 12, "failed_count": 0, "skipped_count": 3} + + receipt_check = { + "schema_version": "exact_release_check_receipt_v0", + "source": expected, + "status": "passed", + "result_schema_version": "pytest_summary_v0", + "result_digest": runner._canonical_sha256(counts), + "completed_at": datetime.now(timezone.utc).isoformat(), + "summary": counts, + } + manifest = _manifest(source=expected) + manifest["qualifications"]["pytest"] = receipt_check # type: ignore[index] + evaluated = build_exact_release_commit_qualification(manifest, observed_source=expected) + assert evaluated["ready_for_release"] is True + assert evaluated["qualifications"]["pytest"]["summary"] == counts + + +def test_qualify_release_commit_runs_end_to_end_on_clean_repo(tmp_path: Path) -> None: + repo = tmp_path / "clean-repo" + (repo / "loopx").mkdir(parents=True) + (repo / "tests").mkdir(parents=True) + (repo / ".gitignore").write_text("__pycache__/\n*.pyc\n", encoding="utf-8") + (repo / "pyproject.toml").write_text(f'[project]\nname = "loopx"\nversion = "{__version__}"\n', encoding="utf-8") + (repo / "loopx" / "__init__.py").write_text(f'__version__ = "{__version__}"\n', encoding="utf-8") + (repo / "tests" / "test_sample.py").write_text( + "import pytest\ndef test_pass_a(): assert 1 + 1 == 2\ndef test_skip_b(): pytest.skip('ok')\n", + encoding="utf-8", + ) + for args in ( + ("init",), + ("config", "user.email", "loopx@example.invalid"), + ("config", "user.name", "LoopX Test"), + ("add", "."), + ("commit", "-m", "init"), + ): + subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True) + + out_dir = tmp_path / "out" + completed = subprocess.run( + [ + sys.executable, + str(REPO_ROOT / "scripts" / "qualify_release_commit.py"), + "--repo-root", + str(repo), + "--output-dir", + str(out_dir), + "--shards", + "1", + "--no-pytest-split", + ], + cwd=REPO_ROOT, + check=False, + capture_output=True, + text=True, + ) + assert completed.returncode == 0, completed.stdout + completed.stderr + receipt = json.loads((out_dir / "receipt-pytest.json").read_text(encoding="utf-8")) + assert receipt["status"] == "passed" + assert receipt["summary"] == {"passed_count": 1, "failed_count": 0, "skipped_count": 1} + assert str(tmp_path) not in json.dumps(receipt) + + +def test_select_preflight_tests_honors_pyproject_norecursedirs(tmp_path: Path) -> None: + runner = runpy.run_path(str(REPO_ROOT / "scripts" / "qualify_release_commit.py")) + repo = tmp_path / "repo" + repo.mkdir() + (repo / "pyproject.toml").write_text( + '[tool.pytest.ini_options]\n' + 'norecursedirs = [".*", "*.egg", ".local", "build", "dist", "deprecate", ' + '"node_modules", "venv", "packages/loopx-ark-turn", "packages/loopx-memt-turn"]\n', + encoding="utf-8", + ) + candidates = [ + "packages/loopx-ark-turn/tests/test_host.py", + "packages/loopx-memt-turn/tests/test_memt.py", + "packages/loopx-ark-turn-other/tests/test_other.py", + ".venv/lib/test_hidden.py", + "tests/.local/test_private.py", + "tests/pkg.egg/test_egg.py", + "build/test_build.py", + "node_modules/pkg/test_node.py", + "tests/test_core.py", + ] + for rel in candidates: + target = repo / rel + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text("def test_ok(): pass\n", encoding="utf-8") + + selected = runner["select_preflight_tests"](candidates, repo_root=repo) + assert selected == [ + "packages/loopx-ark-turn-other/tests/test_other.py", + "tests/test_core.py", + ] + + +def test_qualify_release_commit_clean_durations_and_resume_seed_merge(tmp_path: Path) -> None: + repo = tmp_path / "clean-repo" + (repo / "loopx").mkdir(parents=True) + (repo / "tests").mkdir(parents=True) + (repo / ".gitignore").write_text("__pycache__/\n*.pyc\n", encoding="utf-8") + (repo / "pyproject.toml").write_text( + f'[project]\nname = "loopx"\nversion = "{__version__}"\n', + encoding="utf-8", + ) + (repo / "loopx" / "__init__.py").write_text( + f'__version__ = "{__version__}"\n', + encoding="utf-8", + ) + (repo / "tests" / "test_alpha.py").write_text( + "def test_alpha(): assert 1 + 1 == 2\n", + encoding="utf-8", + ) + (repo / "tests" / "test_beta.py").write_text( + "def test_beta(): assert 2 + 2 == 4\n", + encoding="utf-8", + ) + for args in ( + ("init",), + ("config", "user.email", "loopx@example.invalid"), + ("config", "user.name", "LoopX Test"), + ("add", "."), + ("commit", "-m", "init"), + ): + subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True, text=True) + + seed_path = tmp_path / "seed-durations.json" + seed_path.write_text( + json.dumps( + { + "tests/test_alpha.py::test_alpha": 99.0, + "tests/test_beta.py::test_beta": 88.0, + "tests/test_removed.py::test_removed": 77.0, + } + ), + encoding="utf-8", + ) + out_dir = tmp_path / "out" + merged_path = tmp_path / "merged-durations.json" + completed = subprocess.run( + [ + sys.executable, + str(REPO_ROOT / "scripts" / "qualify_release_commit.py"), + "--repo-root", + str(repo), + "--output-dir", + str(out_dir), + "--shards", + "2", + "--durations-path", + str(seed_path), + "--durations-out", + str(merged_path), + ], + cwd=REPO_ROOT, + check=False, + capture_output=True, + text=True, + ) + assert completed.returncode == 0, completed.stdout + completed.stderr + merged = json.loads(merged_path.read_text(encoding="utf-8")) + assert set(merged) == { + "tests/test_alpha.py::test_alpha", + "tests/test_beta.py::test_beta", + } + assert 0.0 <= merged["tests/test_alpha.py::test_alpha"] < 10.0 + assert 0.0 <= merged["tests/test_beta.py::test_beta"] < 10.0 + assert not (out_dir / ".shard-1-durations.json").exists() + assert not (out_dir / ".shard-2-durations.json").exists() + + # Now simulate resuming shard 1 while re-running shard 2 fresh: shard 1 keeps seed, + # shard 2 writes fresh timing without overwriting shard 1's timing back to seed. + shard1_xml = (out_dir / "pytest-shard-1.xml").read_text(encoding="utf-8") + shard1_key = ( + "tests/test_alpha.py::test_alpha" + if "test_alpha" in shard1_xml + else "tests/test_beta.py::test_beta" + ) + shard2_key = ( + "tests/test_beta.py::test_beta" + if shard1_key == "tests/test_alpha.py::test_alpha" + else "tests/test_alpha.py::test_alpha" + ) + (out_dir / "pytest-shard-2-process-summary.json").unlink() + (out_dir / "pytest-shard-2.xml").unlink() + resumed_out = tmp_path / "resumed-durations.json" + resumed = subprocess.run( + [ + sys.executable, + str(REPO_ROOT / "scripts" / "qualify_release_commit.py"), + "--repo-root", + str(repo), + "--output-dir", + str(out_dir), + "--shards", + "2", + "--resume", + "--durations-path", + str(seed_path), + "--durations-out", + str(resumed_out), + ], + cwd=REPO_ROOT, + check=False, + capture_output=True, + text=True, + ) + assert resumed.returncode == 0, resumed.stdout + resumed.stderr + resumed_merged = json.loads(resumed_out.read_text(encoding="utf-8")) + expected_shard1_seed = 99.0 if shard1_key == "tests/test_alpha.py::test_alpha" else 88.0 + assert resumed_merged[shard1_key] == expected_shard1_seed + assert 0.0 <= resumed_merged[shard2_key] < 10.0 + + +def test_remove_tree_tolerant_reclaims_readonly_and_skips_links(tmp_path: Path) -> None: + runner = runpy.run_path(str(REPO_ROOT / "scripts" / "qualify_release_commit.py")) + remove_tree = runner["_remove_tree_tolerant"] + + outside_dir = tmp_path / "outside-dir" + outside_dir.mkdir() + outside_file = outside_dir / "keep.txt" + outside_file.write_text("keep\n", encoding="utf-8") + hard_source = tmp_path / "hard-source.txt" + hard_source.write_text("hard\n", encoding="utf-8") + + tree = tmp_path / "tree" + readonly_sub = tree / "ro-sub" + readonly_sub.mkdir(parents=True) + readonly_file = readonly_sub / "ro.txt" + readonly_file.write_text("ro\n", encoding="utf-8") + (tree / "dir-link").symlink_to(outside_dir) + (tree / "file-link").symlink_to(outside_file) + os.link(hard_source, tree / "hard-link.txt") + + os.chmod(outside_file, 0o444) + os.chmod(outside_dir, 0o555) + os.chmod(hard_source, 0o444) + os.chmod(readonly_file, 0o444) + os.chmod(readonly_sub, 0o555) + try: + remove_tree(tree) + assert not tree.exists() + assert (outside_dir.stat().st_mode & 0o777) == 0o555 + assert (outside_file.stat().st_mode & 0o777) == 0o444 + assert (hard_source.stat().st_mode & 0o777) == 0o444 + + top_link = tmp_path / "top-link" + top_link.symlink_to(outside_dir) + remove_tree(top_link) + assert not top_link.is_symlink() + assert outside_dir.exists() + finally: + os.chmod(outside_dir, 0o755) + os.chmod(outside_file, 0o644) + os.chmod(hard_source, 0o644) + + +def test_only_preflight_requires_preflight_since_and_isolated_node_compile_cache( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + runner = runpy.run_path(str(REPO_ROOT / "scripts" / "qualify_release_commit.py")) + main = runner["main"] + isolated_env = runner["_isolated_env"] + + with pytest.raises(SystemExit) as exc: + main(["--output-dir", str(tmp_path / "out"), "--only-preflight"]) + assert exc.value.code == 2 + assert "--only-preflight requires --preflight-since" in capsys.readouterr().err + + monkeypatch.delenv("NODE_DISABLE_COMPILE_CACHE", raising=False) + monkeypatch.delenv("NODE_V8_COVERAGE", raising=False) + monkeypatch.delenv("NODE_COMPILE_CACHE", raising=False) + enabled_root = tmp_path / "env-enabled" + enabled_env = isolated_env(enabled_root, python_bin=Path(sys.executable), extra_bin_dirs=[]) + cache_dir = enabled_root / "node-compile-cache" + assert enabled_env["NODE_COMPILE_CACHE"] == str(cache_dir) + assert cache_dir.is_dir() + if hasattr(os, "getuid"): + assert (cache_dir.stat().st_mode & 0o777) == 0o700 + + monkeypatch.setenv("NODE_DISABLE_COMPILE_CACHE", "1") + disabled_root = tmp_path / "env-disabled" + disabled_env = isolated_env(disabled_root, python_bin=Path(sys.executable), extra_bin_dirs=[]) + assert "NODE_COMPILE_CACHE" not in disabled_env + assert not (disabled_root / "node-compile-cache").exists() + + monkeypatch.delenv("NODE_DISABLE_COMPILE_CACHE", raising=False) + monkeypatch.setenv("NODE_V8_COVERAGE", str(tmp_path / "cov")) + cov_root = tmp_path / "env-cov" + cov_env = isolated_env(cov_root, python_bin=Path(sys.executable), extra_bin_dirs=[]) + assert "NODE_COMPILE_CACHE" not in cov_env + assert not (cov_root / "node-compile-cache").exists()