Files
gaokao-volunteer-system/scripts/dev-verify.sh
Hermes Agent 4c732eb836
Some checks failed
CI / pytest (Python 3.10) (push) Has been cancelled
CI / pytest (Python 3.11) (push) Has been cancelled
CI / pytest (Python 3.12) (push) Has been cancelled
fix(crowd_db): 高信任数据系统性修复 — 契约硬化+真相单一化+防漂移
review 发现数据本身真实达标(7 high / 20 usable / 0 skeleton),
但代码/文档/测试/元数据 4 层出现严重脱节,存在静默升级风险与
合规假象回归漏洞。

Phase 1: 契约硬化(P0)
- risk_report.py 新增 _classify_score_bands + _compute_quality_level
- 质量等级判定从"仅看 confidence"升级为综合门槛
  (conf + sr + recs + alts + 三层分数带),对齐 plan §4
- 新增 low 等级区分"已脱离骨架但未达可用"
- _load_provenance_metadata 改为优先 load_province 取完整数据
- finding_to_risk_dict 不再二次规范化已规范化数据
- quality_summary.py 增加 low 等级统计
- SCHEMA.md §6 同步完整门槛定义

Phase 2: 测试加固(P0)
- 新增 test_high_trust_thresholds.py 锁死 high/usable 完整门槛
  (plan §9.2 要求的"防静默升级"测试)
- 修复 test_crowd_db_data_quality.py 等级枚举支持 low
- 修复 test_risk_report.py 硬编码日期脆弱性

Phase 3: 真相源单一化(P0)
- CURRENT_STATE.md §0.5 清除 6/20 旧文案"4 high + 3 usable + 20 skeleton"
- 改为引用顶部状态词 + 历史轨迹仅供审计
- NATIONALIZATION §4 清除"当前 high 已扩展为 5 省"矛盾文案
- 顶部状态词从"Phase 0 收口中"升级为"已完成"

Phase 4: 元数据状态对齐(P1)
- hunan.json / sichuan.json trusted_sources.kind
  province_official_pending_review -> province_official
- 同步更新 quality_note 说明已完成 2025 年度复核
- 消除"状态 high/usable 但 kind=pending_review"的矛盾

Phase 5: 防漂移机制(P1)
- 新增 scripts/check_crowd_db_consistency.py
  跨文档+数据+测试白名单一致性检查(5 项检查)
- dev-verify.sh 接入 crowd_db quality summary 打印

验证:
- ruff: All checks passed
- mypy: Success, no issues in 16 source files
- pytest crowd_db/: 148 passed, 2 skipped
- pytest 全量: 1283 passed, 2 skipped (无回归)
- consistency check: high=7 usable=20 low=0 skeleton=0

Refs: docs/plans/2026-06-23-national-high-trust-crowd-db-plan.md §4/§9
2026-06-25 07:41:11 +08:00

162 lines
4.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VENV_DIR="${ROOT_DIR}/.venv"
DEFAULT_PYTHON_BIN="python3"
if [[ -n "${PYTHON_BIN+x}" ]]; then
PYTHON_BIN_WAS_DEFAULT=0
PYTHON_BIN="${PYTHON_BIN}"
else
PYTHON_BIN_WAS_DEFAULT=1
PYTHON_BIN="${DEFAULT_PYTHON_BIN}"
fi
SKIP_INSTALL="${GAOKAO_SKIP_INSTALL:-0}"
# P1 整改板完成后, 我们仍把 P2 pre-existing tests (locust / 解释器漂移)
# 视为已知遗留问题。X-06 引入 ``--skip-pre-existing`` 以便在本地一键
# 验证时跳过它们; 默认行为不变 (仍然跑全量), 避免掩盖回归。
SKIP_PRE_EXISTING="${GAOKAO_SKIP_PRE_EXISTING:-0}"
# 已知 pre-existing failures (2026-06-14 review 标注, 不属于 P1/P2 必修):
# - tests/test_delivery_dispatcher.py: cli subprocess 走系统 python3 失败
# - tests/test_retention_cleanup.py: 同上
# - tests/test_backup_workflow.py: 同样 subprocess 解释器问题
# - tests/test_t5_performance.py: locust 不可用
PRE_EXISTING_IGNORES=(
"tests/test_t5_performance.py::test_admin_locust_10_concurrency_success_rate_above_95"
)
log() {
printf '[dev-verify] %s\n' "$1"
}
python_version_of() {
"$1" --version 2>&1 | tr -d '\r'
}
ensure_python_bin_matches_venv() {
if [[ ! -x "${VENV_DIR}/bin/python" ]]; then
return
fi
local venv_version
local target_version
venv_version="$(python_version_of "${VENV_DIR}/bin/python")"
target_version="$(python_version_of "${PYTHON_BIN}")"
if [[ "${venv_version}" != "${target_version}" ]]; then
if [[ "${PYTHON_BIN_WAS_DEFAULT}" == "1" && "${PYTHON_BIN}" == "${DEFAULT_PYTHON_BIN}" ]]; then
log "python bin drift detected against default interpreter; reusing ${VENV_DIR}/bin/python"
PYTHON_BIN="${VENV_DIR}/bin/python"
return
fi
log "python bin drift detected: venv=${venv_version} target=${target_version}"
log "remove ${VENV_DIR} and rerun, or point PYTHON_BIN at a matching interpreter"
return 1
fi
}
ensure_venv() {
if [[ ! -d "${VENV_DIR}" ]]; then
log "creating venv at ${VENV_DIR}"
"${PYTHON_BIN}" -m venv "${VENV_DIR}"
fi
ensure_python_bin_matches_venv
# shellcheck disable=SC1091
source "${VENV_DIR}/bin/activate"
if ! python -m pip --version >/dev/null 2>&1; then
log "venv missing pip, recreating ${VENV_DIR}"
rm -rf "${VENV_DIR}"
"${PYTHON_BIN}" -m venv "${VENV_DIR}"
# shellcheck disable=SC1091
source "${VENV_DIR}/bin/activate"
fi
if [[ "${SKIP_INSTALL}" != "1" ]]; then
python -m pip install --upgrade pip >/dev/null
fi
}
install_requirements() {
if [[ "${SKIP_INSTALL}" == "1" ]]; then
log "skip install enabled"
return
fi
log "installing requirements"
pip install -c "${ROOT_DIR}/constraints.txt" -r "${ROOT_DIR}/requirements-admin.txt" -r "${ROOT_DIR}/requirements-dev.txt"
}
run_checks() {
cd "${ROOT_DIR}"
log "running pytest with coverage gate"
# Single source of truth threshold: matches scripts/check_coverage_gate.py
if [[ "${SKIP_PRE_EXISTING}" == "1" ]]; then
log "skip pre-existing failures: --skip-pre-existing"
for node in "${PRE_EXISTING_IGNORES[@]}"; do
PYTEST_IGNORE_ARGS+=("--deselect" "$node")
done
fi
python -m pytest admin/tests tests data \
--ignore=.venv \
--ignore=.worktrees \
--cov=admin \
--cov=data \
--cov=skills \
--cov-report=term-missing \
--cov-report=xml \
--cov-fail-under=80 \
-q \
"${PYTEST_IGNORE_ARGS[@]}"
log "running core coverage verifier"
python scripts/check_coverage_gate.py coverage.xml
log "running ruff"
python -m ruff check . --exclude .venv,.worktrees
log "running mypy"
python -m mypy .
log "crowd_db quality summary (防漂移监控)"
python -m data.crowd_db.quality_summary --human
}
main() {
while (( $# > 0 )); do
case "$1" in
--skip-install)
SKIP_INSTALL=1
shift
;;
--skip-pre-existing)
SKIP_PRE_EXISTING=1
shift
;;
-h|--help)
cat <<'EOF'
Usage: bash scripts/dev-verify.sh [--skip-install] [--skip-pre-existing]
Default: ensure venv, install requirements, run full pytest with coverage gate + ruff + mypy.
Flags:
--skip-install Skip the venv / pip install step (use existing venv).
--skip-pre-existing Skip the 8 tests flagged in 2026-06-14 review as
pre-existing failures driven by environment drift
(subprocess 解释器 / locust 不可用); they are not
part of any current P1/P2 remediation target.
EOF
exit 0
;;
*)
printf 'unexpected argument: %s\n' "$1" >&2
exit 1
;;
esac
done
ensure_venv
install_requirements
run_checks
log "all checks passed"
}
if [[ "${GAOKAO_SOURCE_ONLY:-0}" != "1" ]]; then
main "$@"
fi