fix: idempotent _run_step (DELETE stale) + checkpoint after gate save

This commit is contained in:
2026-07-16 06:27:38 -04:00
parent efbdeb1647
commit 77fd35830c
3 changed files with 95 additions and 16 deletions
@@ -212,6 +212,47 @@ async def test_diagnosis_reads_traces_from_steps_json(
assert traces[0]["tool_name"] == "search_tree"
@pytest.mark.asyncio
async def test_run_step_deletes_stale_rows_before_rerun(
runner_with_real_store: Runner, monkeypatch: pytest.MonkeyPatch
) -> None:
"""同 run_id 重跑前先清 predictions/traces,避免重复行双计(幂等)。"""
from unittest.mock import AsyncMock
from app.harness.inference import PREDICTIONS_SCHEMA, TRACES_SCHEMA
from app.harness.log import HarnessLog
run_id = "infer_adhoc_e1_s0"
# 预置该 step run_id 的旧 predictions/traces 行
with HarnessLog(str(runner_with_real_store._paths.db_path), run_id) as log:
log.create_table("predictions", PREDICTIONS_SCHEMA)
log.create_table("traces", TRACES_SCHEMA)
log.insert("predictions", {"video_id": "vA", "question_id": "q1", "prediction": "A"})
log.insert("traces", {"video_id": "vA", "question_id": "q1", "step": 0})
batch = [_fake_question("q1", "vA")]
runner_with_real_store._rollout_batch = AsyncMock()
monkeypatch.setattr("app.harness.runner._apply_batch_correctness", lambda *a, **k: None)
runner_with_real_store._run_diagnosis = AsyncMock(return_value=DiagnosisResult(run_id=run_id))
runner_with_real_store._gate_batch_skills = AsyncMock()
state = MagicMock()
state.correctness = {"q1": True}
state.gate_cooldown = {}
pools = MagicMock()
pools.baseline_run_id = "infer_adhoc"
await runner_with_real_store._run_step(1, 0, 10, batch, pools, state)
with HarnessLog(
str(runner_with_real_store._paths.db_path), run_id, register_run=False
) as log:
preds = log.query("SELECT * FROM predictions WHERE run_id=?", (run_id,))
traces = log.query("SELECT * FROM traces WHERE run_id=?", (run_id,))
assert preds == []
assert traces == []
@pytest.mark.asyncio
async def test_run_step_aborts_on_high_degrade_rate(
runner_with_real_store: Runner, monkeypatch: pytest.MonkeyPatch