diff --git a/VERSION b/VERSION index 769ed6a..b005e30 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.14 +0.2.15 diff --git a/backfill_summaries.py b/backfill_summaries.py index 80bb298..480e8de 100755 --- a/backfill_summaries.py +++ b/backfill_summaries.py @@ -59,8 +59,10 @@ def missing_message_dates( overwrite: bool = False, refresh_tagged_from: date | None = None, daily_overwrite_from: date | None = None, + already_overwritten: set[str] | None = None, ): - """Yield message days that are missing or explicitly tagged for refresh.""" + """Yield message days that are missing, tagged, or pending overwrite.""" + overwritten = already_overwritten or set() current = start while current <= end: output = Path(output_for_day(current)) @@ -70,7 +72,11 @@ def missing_message_dates( and current >= refresh_tagged_from and has_missing_report_tag(output) ) - forced = daily_overwrite_from is not None and current >= daily_overwrite_from + forced = ( + daily_overwrite_from is not None + and current >= daily_overwrite_from + and current.isoformat() not in overwritten + ) if (overwrite or not exists or refresh or forced) and has_messages(current): yield current current += timedelta(days=1) @@ -224,6 +230,15 @@ def main(): flush=True, ) + state_path = Path(args.state).expanduser() + previous_overwritten: set[str] = set() + if state_path.exists(): + try: + previous = json.loads(state_path.read_text(encoding="utf-8")) + previous_overwritten = set(previous.get("overwritten_dates", [])) + except (ValueError, OSError): + previous_overwritten = set() + state = { "model": args.model, "start_date": start.isoformat(), @@ -235,8 +250,8 @@ def main(): "failed": 0, "last_completed": None, "last_attempted": None, + "overwritten_dates": sorted(previous_overwritten), } - state_path = Path(args.state).expanduser() write_state(state_path, state) print( f"补日志范围: {daily_start} 至 {end};" @@ -244,7 +259,7 @@ def main(): flush=True, ) - def run_step(command, output, label, kind="daily"): + def run_step(command, output, label, kind="daily", day: date | None = None): state["last_attempted"] = label write_state(state_path, state) print(f"===== {label} 开始 =====", flush=True) @@ -259,6 +274,10 @@ def main(): state[key] += 1 state_key = {"daily": "last_completed", "weekly": "last_weekly_completed", "monthly": "last_monthly_completed"}[kind] state[state_key] = label + if day is not None: + overwritten = set(state.get("overwritten_dates", [])) + overwritten.add(day.isoformat()) + state["overwritten_dates"] = sorted(overwritten) write_state(state_path, state) print(f"===== {label} 完成 =====", flush=True) return 0 @@ -321,6 +340,7 @@ def main(): args.overwrite, refresh_tagged_from, daily_overwrite_from, + set(state["overwritten_dates"]), ): command = [ sys.executable, str(ROOT / "lark_snooper.py"), "summarize", @@ -338,7 +358,9 @@ def main(): or (daily_overwrite_from and day >= daily_overwrite_from) ): command.append("--overwrite") - result = run_step(command, output, f"补日志 {day}") + result = run_step( + command, output, f"补日志 {day}", kind="daily", day=day + ) if result: return result diff --git a/tests/test_backfill.py b/tests/test_backfill.py index 42f8258..8b6df85 100644 --- a/tests/test_backfill.py +++ b/tests/test_backfill.py @@ -97,3 +97,19 @@ def test_backfill_does_not_probe_days_that_do_not_need_regeneration(tmp_path): assert days == [] assert probes == [] # 已存在且无需重生成的日子不应触发拉取判断 + + +def test_daily_overwrite_skips_dates_already_overwritten(tmp_path): + for day in (date(2026, 11, 11), date(2026, 11, 12)): + (tmp_path / f"{day.isoformat()}.md").write_text("旧日报", encoding="utf-8") + + days = list(missing_message_dates( + date(2026, 11, 11), + date(2026, 11, 12), + lambda day: True, + lambda day: tmp_path / f"{day.isoformat()}.md", + daily_overwrite_from=date(2026, 11, 11), + already_overwritten={"2026-11-11"}, + )) + + assert days == [date(2026, 11, 12)]