fix(monitor): 目标是一串没解析开的链接时,别再报「登录态失效」
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

任务 22 的目标被存成了 `https://xhslink.cn/o/AhQpHsqcme5`(短链,链接校验关掉之后
原样存下来的),于是永远抓不到东西,而报出来的是「疑似登录态失效」—— 同一份 cookie
下任务 1 采集完全正常。查了半天 cookie,其实是目标无效。

为什么会报成登录失效:那条判断有个「兄弟任务最近成功过就说明登录没问题」的兜底,
窗口是**往前 6 小时**,而任务 1 的成功正好比这一轮的失败**晚 6 秒** —— 撞不上。

修法:在「没抓到数据」的分支里,**先**查目标本身像不像一个 ID(只看是不是长得像 URL,
刻意收得很紧,免得误伤合法的老目标);像 URL 就直说「这是没解析开的链接,请重新
粘贴完整链接 —— 登录态和它没关系」。这条比兄弟任务那条**具体且确定**得多。

测试验过能复现:把这段拿掉,任务 22 那次的「登录失效」就会重现。
This commit is contained in:
2026-10-14 08:00:12 +08:00
parent 6aba3a73c1
commit f6276ec314
2 changed files with 109 additions and 0 deletions
+57
View File
@@ -59,6 +59,7 @@ from api.monitor.models import (
MonitorNote,
MonitorNoteMetric,
MonitorRun,
MonitorTarget,
MonitorTask,
RUN_FAILED,
RUN_PARTIAL,
@@ -1089,3 +1090,59 @@ class TestNicknameRefresh:
await ingest_run(db, run, task, tmp_path)
assert (await db.scalar(select(MonitorNote))).creator_name == "有名字"
class TestABrokenTargetIsNotBlamedOnTheCookie:
"""目标是一串没解析开的链接时,**不许报「登录态失效」**。
真事(2026-10-14):任务 22 的目标被存成了 `https://xhslink.cn/o/AhQpHsqcme5`(短链,
链接校验关掉之后原样存下来的),于是永远抓不到东西。而报出来的是「疑似登录态失效」,
因为那一刻往前 6 小时正好没有别的任务成功过 —— 隔壁任务 1 的成功**比它晚 6 秒**,
就是撞不上。同一份 cookie,任务 1 采集正常。
"""
@staticmethod
async def _seed_target(db, task, external_id: str) -> None:
db.add(
MonitorTarget(
task_id=task.id,
kind=task.mode,
external_id=external_id,
xsec_token="",
xsec_source="",
raw_value=external_id,
label=external_id,
enabled=True,
created_at=0,
)
)
await db.flush()
@pytest.mark.asyncio
async def test_an_unparsed_url_target_is_reported_as_such(self, db, tmp_path):
task = await _make_task(db)
await self._seed_target(db, task, "https://xhslink.cn/o/AhQpHsqcme5")
run = await _make_run(db, task, started_at=1)
_write_run_dir(tmp_path, [], comments=[])
await ingest_run(db, run, task, tmp_path)
# 不许出现「登录失效」那条 —— 那会把人的注意力引到 cookie 上,而 cookie 是好的。
assert await _events(db, EVENT_AUTH_FAILURE) == []
no_data = await _events(db, EVENT_NO_DATA)
assert no_data
assert "没解析开" in no_data[0].title
assert "xhslink.cn" in no_data[0].title
@pytest.mark.asyncio
async def test_a_normal_target_still_gets_the_usual_diagnosis(self, db, tmp_path):
"""不能因为加了这条就把原有的判断顶掉 —— 正常的目标还是走原来那套。"""
task = await _make_task(db)
await self._seed_target(db, task, "5f58bd990000000001003753")
run = await _make_run(db, task, started_at=1)
_write_run_dir(tmp_path, [], comments=[])
await ingest_run(db, run, task, tmp_path)
assert await _events(db, EVENT_AUTH_FAILURE)
assert await _events(db, EVENT_NO_DATA) == []