test: locate the companion repo instead of assuming one layout

test_m6_r2_real_dto_shape resolved the companion repo as a sibling directory,
which is only true of the development checkout. On the VM the skipif was
always true, so the seven cases that exercise the companion's real DTO builder
-- the only genuine check of the cross-plugin envelope shape -- never ran in
the environment that most needed them, and a skip shows up as a missing dot
rather than a failure.

test_ops_migration had the harder version of the same bug: the import simply
failed, and working around it with PYTHONPATH let the memory side's own
core/ops_migration shadow the companion module, producing two failures that
looked like product defects and were not.

Resolution now lives in tests/_companion_repo.py and tries, in order, the
COMPANION_PLUGIN_ROOT override, the sibling development directory, the
sibling deployed directory (_ops suffix), and one level up. The path helper
inserts at the front of sys.path deliberately: getting that order wrong fails
as "green but testing the wrong module", which is worse than an ImportError.

Memory suite 330 passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 05:41:32 +08:00
父节点 c1acfce738
当前提交 600b07e15c
共有 4 个文件被更改,包括 91 次插入 和 2 次删除

查看文件

@ -299,3 +299,13 @@
- 附带收获:`tests/test_security_cache.py` 连续五轮被排除的那 2 个失败用例,根因就是同一个陷阱——夹具 `group_memory()` 构造记录不传域 → 落库 `unknown` → `search` 恒返回空集。它们要测的是 ACL 与缓存失效,不是域分类;夹具补上显式域后 6 项全通过,该文件不必再排除在测试口径外。
- 预防规则:给「缺省时自动推断」的字段设默认值,默认值必须是**假值**(`""`/`None`),否则推断分支永远不会执行,而且不报错。凡是「压缩/合并/归档」这类会让原始数据不可访问的操作,都要在丢弃原文之前回读一次产物、用**消费侧同一个判据**确认它真的可用——不能只断言「写进去了」。
- 验证证据:`tests/test_sleep_decay_recall.py` 5 项(含端到端召回、按域分桶、非用户域仍正常清理且隔离不放宽、摘要不可召回则跳过归档并告警、dry-run 零写入)。反证:临时摘掉域继承后,端到端召回那条必红(`reason=missing_user_subject`),且新守卫当场拦下归档。全量 330 项通过(此前 323 通过 / 2 失败)。
## 2026-07-27 — 跨仓测试写死「同级目录」,换个布局就静默降级
- 阶段:P2 验收可信度收口
- 类型:测试 / 环境
- 现象:`test_m6_r2_real_dto_shape.py` 用 `parents[2] / "astrbot_plugin_private_companion-main"` 定位陪伴仓,VM 上不是这个布局,于是 7 个「用陪伴侧真实 DTO 回归」的用例 `skipif` 恒成立——真机验收时这组从没跑过,跨插件信封形状的唯一真实校验在最需要它的环境里是空的。`test_ops_migration.py` 更直接:import 失败,只能手工挂 `PYTHONPATH` 绕过,而那次绕过又让记忆侧的 `core/ops_migration.py` 遮蔽了陪伴侧同名模块,产生两个假失败。
- 根因:每个测试各写各的相对路径,把「两个仓库是同级目录」这一种开发布局当成了唯一布局。而 skip 是静默的:结果里只少了几个点,不会红。
- 修复或规避:定位逻辑收进 `tests/_companion_repo.py`,按「环境变量 `COMPANION_PLUGIN_ROOT` → 同级开发目录 → 同级部署目录(`_ops` 后缀)→ 再上一层」依次尝试;`ensure_companion_on_path` 明确把陪伴目录插到 `sys.path` **最前面**,避免同名模块遮蔽——顺序反了会以「跑绿了但测错了对象」的形态失败,比 ImportError 难查得多。
- 预防规则:跨仓/跨目录的路径假设要收成一处并允许显式覆盖,不要每个文件各写一遍。`skipif` 的条件如果可能在某个环境里恒成立,就等于那个环境没有这条覆盖——要么让定位更健壮,要么让 skip 的原因出现在验收记录里。
- 验证证据:`tests/test_m6_r2_real_dto_shape.py` + `tests/test_ops_migration.py` 共 52 项通过;全量 330 项通过。

62
tests/_companion_repo.py 普通文件
查看文件

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
"""定位陪伴插件仓库。
跨仓测试原先各写各的相对路径(`parents[1].parent / "astrbot_plugin_private_companion-main"`),
只在「两个仓库是同级目录」这一种布局下成立。VM 上不是这个布局,于是:
- `test_m6_r2_real_dto_shape.py` 的 7 个用例 skipif 恒成立,真机口径下从没跑过;
- `test_ops_migration.py` 直接 import 失败,只能靠手工挂 PYTHONPATH 绕过,
而那次绕过又让 `core/ops_migration.py` 遮蔽了陪伴侧同名模块,产生了两个假失败。
这里把定位逻辑收成一处,按优先级依次尝试,并允许用环境变量显式指定。
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
_MEMORY_ROOT = Path(__file__).resolve().parents[1]
_DIR_NAME = "astrbot_plugin_private_companion-main"
# VM 上插件目录用的是 _ops 后缀名
_DEPLOYED_NAME = "astrbot_plugin_private_companion_ops"
def _candidates():
override = os.environ.get("COMPANION_PLUGIN_ROOT", "").strip()
if override:
yield Path(override)
# 开发布局:两个仓库是项目根下的同级目录
yield _MEMORY_ROOT.parent / _DIR_NAME
# 部署布局:两个插件目录都在 AstrBot 的 plugins/ 下
yield _MEMORY_ROOT.parent / _DEPLOYED_NAME
# 再往上一层找一次,容忍多包一层目录的情况
yield _MEMORY_ROOT.parent.parent / _DIR_NAME
def find_companion_root() -> Path | None:
"""返回陪伴插件根目录;找不到返回 None(调用方据此 skip)。"""
for candidate in _candidates():
try:
if (candidate / "bot_personal_dto.py").is_file():
return candidate.resolve()
except OSError:
continue
return None
def ensure_companion_on_path() -> Path | None:
"""把陪伴插件根目录放到 sys.path 最前面,返回该目录。
放最前面是必要的:记忆侧也有 `core/ops_migration.py`,若陪伴目录排在后面,
`import ops_migration` 会命中记忆侧那个同名模块,测试会以「跑绿了但测错了对象」
的形态失败,比直接 ImportError 更难查。
"""
root = find_companion_root()
if root is None:
return None
text = str(root)
if text in sys.path:
sys.path.remove(text)
sys.path.insert(0, text)
return root

查看文件

@ -25,7 +25,13 @@ from core.store import MemoryStore
TZ = timezone(timedelta(hours=8))
NOW = datetime(2026, 7, 27, 18, 0, 0, tzinfo=TZ)
COMPANION_ROOT = Path(__file__).resolve().parents[2] / "astrbot_plugin_private_companion-main"
# 定位逻辑收在 _companion_repo 里:原先写死「同级目录」这一种布局,
# VM 上不成立,于是这 7 个用例在真机口径下 skipif 恒成立、从没跑过。
sys.path.insert(0, str(Path(__file__).resolve().parent))
from _companion_repo import find_companion_root # noqa: E402
_FOUND_COMPANION = find_companion_root()
COMPANION_ROOT = _FOUND_COMPANION or (Path(__file__).resolve().parents[2] / "astrbot_plugin_private_companion-main")
# 陪伴侧 _memory_companion_archive_type 里的七个非五类 kind → memory_type → 期望 reality_level
LEGACY_KINDS = (

查看文件

@ -9,7 +9,18 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT.parent / 'astrbot_plugin_private_companion-main'))
sys.path.insert(0, str(Path(__file__).resolve().parent))
from _companion_repo import ensure_companion_on_path # noqa: E402
# 必须排在记忆侧之前:记忆侧也有 core/ops_migration.py,顺序反了会 import 到同名的
# 那一个,测试会以「跑绿了但测错了对象」的形态失败,比 ImportError 更难查。
_COMPANION_ROOT = ensure_companion_on_path()
if _COMPANION_ROOT is None:
import pytest
pytest.skip("找不到陪伴插件仓库,跨仓迁移测试跳过(可用 COMPANION_PLUGIN_ROOT 指定)",
allow_module_level=True)
import ops_migration as companion_migration
from core import ops_migration as memory_migration