astrbot_plugin_private_comp.../tests/test_daily_plan_version.py
YokiiroBW 2518f7b6bf fix: make the daily plan chain survive startup timing and same-day rewrites
Two defects that only showed up on the real machine, both of which left the
schedule frozen on a fallback plan with nothing in the logs to say so.

The plugin asked for a chat provider 0.8s before AstrBot finished loading
them, wrote a template fallback, and stamped it with today's date -- so the
next 24 hours never retried. Resolution now falls back through get_config()
(4.26.6's Context exposes nothing else) and validates the configured id with
get_provider_by_id, because a configured default is not a loaded instance.
When nothing is ready yet the round is skipped instead of downgraded, up to
DAILY_PLAN_PROVIDER_MAX_WAITS rounds.

Once the model recovered, the real plan still could not reach the archive:
the idempotency key is always daily_plan:<date> but the plan dict never
carried a version, so every rewrite re-sent v1 and the memory side rejected
it as version_conflict. The companion swallowed that -- outbox state is only
touched when enqueue accepts, and a same key/version enqueue does not. Plans
now carry a version watermark drawn from both daily_plan and its history, and
any ok=False archive response logs a warning regardless of outbox state.

Verified on the VM: provider wait counted once and no fallback written, plan
generated at v2 with 10 segments from the model, and the stored record
upserted from the 8-segment fallback to the same v2 content.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 04:16:06 +08:00

79 行
3.2 KiB
Python

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

# -*- coding: utf-8 -*-
"""日程版本水位回归。
真机上暴露的问题:daily_plan 的幂等键恒为 daily_plan:<date>,而计划本身从不带 version。
兜底计划先落库(v1),同一天模型恢复后生成真实计划仍发 v1 —— 记忆侧判 version_conflict
丢弃,陪伴侧 outbox 因同键同版本 accepted=False 连错误状态都不落,整条归档静默消失。
库里于是永远停在那份 8 段兜底上。
"""
from __future__ import annotations
import importlib
import logging
import pathlib
import sys
import types
import unittest
if "astrbot.api" not in sys.modules:
_astrbot = types.ModuleType("astrbot")
_api = types.ModuleType("astrbot.api")
_api.logger = logging.getLogger("test.astrbot")
_astrbot.api = _api
sys.modules.setdefault("astrbot", _astrbot)
sys.modules.setdefault("astrbot.api", _api)
_package_name = "_planver_companion"
_package = types.ModuleType(_package_name)
_package.__path__ = [str(pathlib.Path(__file__).resolve().parents[1])]
sys.modules.setdefault(_package_name, _package)
planning = importlib.import_module(f"{_package_name}.planning")
_next_daily_plan_version = planning._next_daily_plan_version
class _Host:
def __init__(self, data=None):
self.data = data if data is not None else {}
class DailyPlanVersionTests(unittest.TestCase):
def test_first_plan_of_the_day_starts_at_one(self):
self.assertEqual(_next_daily_plan_version(_Host(), "2026-07-27"), 1)
def test_regenerating_the_same_day_bumps_the_version(self):
host = _Host({"daily_plan": {"date": "2026-07-27", "version": 1}})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 2)
def test_a_different_day_restarts_at_one(self):
host = _Host({"daily_plan": {"date": "2026-07-26", "version": 7}})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 1)
def test_history_keeps_the_watermark_when_daily_plan_is_cleared(self):
"""运维清空 daily_plan 后版本不能回退到 1,否则又撞上库里已有的同版本记录。"""
host = _Host({"daily_plan_history": [
{"date": "2026-07-26", "version": 2},
{"date": "2026-07-27", "version": 3},
]})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 4)
def test_takes_the_higher_of_plan_and_history(self):
host = _Host({
"daily_plan": {"date": "2026-07-27", "version": 5},
"daily_plan_history": [{"date": "2026-07-27", "version": 2}],
})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 6)
def test_legacy_entries_without_version_count_as_one(self):
"""升级前写下的计划没有 version 字段,必须视为 v1 而不是 v0。"""
host = _Host({"daily_plan": {"date": "2026-07-27"}})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 2)
def test_missing_or_broken_data_is_tolerated(self):
self.assertEqual(_next_daily_plan_version(_Host(None), "2026-07-27"), 1)
host = _Host({"daily_plan": "不是字典", "daily_plan_history": "也不是列表"})
self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 1)
if __name__ == "__main__":
unittest.main()