refactor(account): S2 读写一次切换 —— CK/健康/重登彻底搬到账号,登录记录退化为历史
S1 加好了列,本提交把**读与写一起切**过去。这是设计里「同一份数据的读与写必须
在同一个提交里切」的要求:先切读或先切写都会出现「写账号、读记录」的中间态,
跑起来不报错,只是默默用错数据。
## 核心改动
**判定「谁有 CK」不再借道登录记录**
- `cookie_holding_filter()` / `cookie_operable_filter()` / `cookie_holding_account_ids_query()`
/ `visible_douyu_task_accounts()` 全部改为作用于 `Account`;
- `cookie_operable_filter()` 的「在途排除」仍查 `login_tasks`(那是**某次尝试**的属性),
用 `NOT EXISTS` 子查询 + 迁移 0042 新增的 `(account_id, attempt_status)` 索引;
- 删除死代码 `latest_cookie_login_task()`;`latest_cookie()` 改为直接读账号。
**CK 写入**
- `_write_login_success()` 写 `Account.cookie / cookie_updated_at / cookie_source`;
- `_mark_attempt_failed()` 改看**账号**有没有 CK(决定是否沿用旧的失败语义);
- 检测模式的 `task.cookie = ""` **不再清空账号的 CK**(见下「行为变更」)。
**自动化作业按账号调度**
- `ck_health`:`_select_tasks` → `_select_accounts`,`db.query(Account)`;新增
`count_remaining()`(刻意不带 LIMIT,否则积压数恒等于批次上限);
- `ck_relogin`:同样按账号选行;结算改为「账号 + 本轮尝试记录」,
退避进度写账号;`settle_fresh_results(batch_id)` 按 `attempt_batch_id` 配对,
`run_ck_relogin` 把 **account_ids** 交给 runner;
- 顺带让**没有任何登录历史的账号**也能被巡检/重登(以前它们永远进不了队列)。
**登录批次**
- `LoginBatchRunner` 去掉 `relogin_task_ids`,重登与登录统一走 `account_ids`;
- 批次行复用逻辑重写:只保证「同一账号不并发两行」,不再关心「哪一行持有 CK」。
失败行复用同时看 `attempt_status`(账号有 CK 时 `_mark_attempt_failed` 刻意不动
`status`,只看 status 会永远命不中 → 每次失败刷一行)。
**删除护栏语义变更(用户可感知)**
- 去掉「持有 CK 的记录不可删」——CK 在账号上,删历史不再影响登录态,历史终于可清理;
- **保留**「在途尝试不可删」(批次线程正拿着那条记录)。
## 行为变更(需评审)
1. **账号检测不再清空 CK**。旧代码在 check 模式写 `task.cookie = ""`,而那时记录的
cookie 就是账号唯一凭证 —— 一次「批量检测」会把账号登录态删掉。解耦后
「检测账号凭据」与「CK 是否有效」本就是两件事(后者归 `ck_health` 判),
继续清空等于凭空制造不可逆的数据丢失(重登当前成功率极低)。
2. **Cookie 页的记录单位从「登录记录」变成「账号」**,所有接口的 id 入参/出参
变成 `account_id`;`DELETE /cookies/{id}` 清的是该账号的 CK,**必写审计**。
3. 重复检测从「同一账号多行持 CK」改为「**同一个 CK 出现在多个账号上**」——
后者才是真风险(两个账号共用一个登录态)。用 sha256 指纹分组:
CK 是 AES-GCM + 随机 nonce 加密,**同一明文每次密文都不同**,比密文永远比不出重复。
## 修掉一个真实并发缺陷
`_write_login_success` 原先被传入 `self.db`(runner 主会话),而它是在**工作线程**里
用 `worker_db` 执行的。`emit()` 会往 `account_events` 插一行,其 `account_id` 外键对
`accounts` 那一行加**共享锁**;这把锁加在另一个会话上,紧接着本会话的
`UPDATE accounts SET cookie=...` 就被自己人挡住 —— 实测 **50 秒后
`Lock wait timeout exceeded`,登录明明成功却写不回 CK**(改造前 UPDATE 打在
`login_tasks` 上所以没撞上)。改为传 `worker_db`:事件本就该和状态变更同事务提交,
顺带修掉跨线程共用一个 Session 的隐患。
## 验证
- 全量 pytest **368 passed / 6 skipped**;迁移链 3 passed;
- 前端 `tsc -b && vite build` 通过;
- `ruff check` / `ruff format --check` 全绿(顺带修掉 main 上遗留的 F821);
- 新增/改写的关键回归:**删掉全部登录历史后账号的 CK 与健康状态仍在**、
**无登录历史的账号也能进巡检队列**、结算不碰 CK、删除护栏改为在途语义。
仍未做(设计里排期的后续阶段):`account_lifecycle` 状态收口(S3)、事件发射点补齐(S4)、
业务事实直发事件(S5)、账号页重构(S6)。注意 `status` 目前只有模型默认值与迁移回填,
所以 `disabled/archived` 还没有任何写入方——S3 落地时要把自动化选行的状态过滤一起补上。
This commit is contained in:
@@ -231,16 +231,15 @@ class TestBulkDelete:
|
||||
assert "无效的任务ID" in str(exc.value)
|
||||
|
||||
|
||||
class TestCkProtection:
|
||||
"""⚠️ 最重要的护栏:删除不能丢 CK。
|
||||
class TestDeleteGuard:
|
||||
"""⚠️ 护栏语义已随 CK 解耦改变(2026-10-04)。
|
||||
|
||||
事故背景(2026-10-03):斗鱼账号的 CK **只存在 `login_tasks.cookie`**,
|
||||
`accounts` 表没有 cookie 列。列表显示的「状态」是最近一次尝试
|
||||
(``attempt_status``),于是一条**显示「失败」但实际持有有效 CK** 的记录
|
||||
(``status='success'`` + cookie + 上次尝试失败)会被「清空失败」当成垃圾删掉,
|
||||
账号的登录态随之永久丢失。
|
||||
旧护栏:「持有 CK 的记录不可删」—— 因为那时 CK 只存在 `login_tasks.cookie`,
|
||||
删历史就是删账号的登录态(2026-10-03 事故)。
|
||||
|
||||
所以判定必须看 ``cookie``,不能看显示状态。
|
||||
新护栏:CK 已迁到 `accounts.cookie`,**删除历史不再影响任何账号的登录态**,
|
||||
所以 CK 不再是护栏理由(历史终于可以自由清理)。保留的是**在途护栏**:
|
||||
批次线程正拿着那条记录,删掉会让它找不到目标。
|
||||
"""
|
||||
|
||||
def setup_method(self):
|
||||
@@ -252,51 +251,48 @@ class TestCkProtection:
|
||||
self.session.add(self.user)
|
||||
self.session.flush()
|
||||
self.account = Account(
|
||||
username="CK保护账号",
|
||||
username="CK账号",
|
||||
password="p",
|
||||
email="ck@example.com",
|
||||
email_password="ep",
|
||||
cookie="valid-ck-1",
|
||||
)
|
||||
self.session.add(self.account)
|
||||
self.session.flush()
|
||||
|
||||
# ① 显示失败、但持有有效 CK —— 生产事故就是删到了这种
|
||||
self.holds_ck_shows_failed = self._add(
|
||||
status="success", attempt_status="failed", cookie="valid-ck-1"
|
||||
# ① 历史:显示失败、但账号仍持有有效 CK(本次改造前会被当成垃圾删掉)
|
||||
self.failed_history = self._add(status="failed", attempt_status="failed")
|
||||
# ② 历史:成功
|
||||
self.success_history = self._add(status="success", attempt_status="success")
|
||||
# ③ 在途:批次正拿着它,不可删
|
||||
self.in_flight = self._add(
|
||||
status="pending", attempt_status="running", attempt_batch_id="live-batch"
|
||||
)
|
||||
# ② 成功且持有 CK
|
||||
self.holds_ck_shows_success = self._add(
|
||||
status="success", attempt_status="success", cookie="valid-ck-2"
|
||||
)
|
||||
# ③ 真垃圾:没有任何 CK
|
||||
self.no_ck = self._add(status="failed", attempt_status="failed", cookie="")
|
||||
|
||||
# 用两个账号,避免同一账号多条记录干扰「账号是否还有 CK」的判断
|
||||
self.other = Account(
|
||||
username="CK保护账号2",
|
||||
username="CK账号2",
|
||||
password="p",
|
||||
email="ck2@example.com",
|
||||
email_password="ep",
|
||||
cookie="valid-ck-2",
|
||||
)
|
||||
self.session.add(self.other)
|
||||
self.session.flush()
|
||||
self.holds_ck_other = self._add(
|
||||
status="success",
|
||||
attempt_status="failed",
|
||||
cookie="valid-ck-3",
|
||||
account_id=self.other.id,
|
||||
self.other_history = self._add(
|
||||
status="success", attempt_status="failed", account_id=self.other.id
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def _add(self, *, status, attempt_status, cookie, account_id=None):
|
||||
def _add(
|
||||
self, *, status, attempt_status, account_id=None, attempt_batch_id="ck-batch"
|
||||
):
|
||||
task = LoginTask(
|
||||
batch_id="ck-batch",
|
||||
account_id=account_id or self.account.id,
|
||||
created_by=self.user.id,
|
||||
status=status,
|
||||
cookie=cookie,
|
||||
attempt_status=attempt_status,
|
||||
attempt_batch_id="ck-batch",
|
||||
attempt_batch_id=attempt_batch_id,
|
||||
message="x",
|
||||
)
|
||||
self.session.add(task)
|
||||
@@ -318,65 +314,75 @@ class TestCkProtection:
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def test_bulk_delete_skips_ck_holding_records(self):
|
||||
"""「清空失败」绝不能删掉持有 CK 的记录,哪怕它显示为失败。"""
|
||||
def _account_cookie(self, account_id: int) -> str:
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(
|
||||
status="failed", all_matching=True, db=db, current=self.user
|
||||
)
|
||||
return db.query(Account).filter(Account.id == account_id).one().cookie
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
assert self._exists(self.holds_ck_shows_failed), (
|
||||
"显示失败但持有 CK 的记录被删了!"
|
||||
)
|
||||
assert self._exists(self.holds_ck_other), "另一个账号的 CK 记录被删了!"
|
||||
assert not self._exists(self.no_ck), "无 CK 的历史记录应被删除"
|
||||
assert res["deleted"] == 1
|
||||
assert res["skipped"] >= 2
|
||||
|
||||
def test_bulk_delete_reports_skipped_count(self):
|
||||
def test_bulk_delete_removes_history_even_when_account_holds_cookie(self):
|
||||
"""核心验收:删掉全部历史,账号的 CK 一点不受影响。"""
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(all_matching=True, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
# 3 条持有 CK(含另一个账号)、1 条无 CK
|
||||
assert res["deleted"] == 1
|
||||
assert res["skipped"] == 3
|
||||
assert "持有 CK" in res["message"]
|
||||
|
||||
def test_explicit_ids_cannot_delete_ck_holder(self):
|
||||
"""即使用户手动勾选,持有 CK 的也不能删。"""
|
||||
assert not self._exists(self.failed_history)
|
||||
assert not self._exists(self.success_history)
|
||||
assert not self._exists(self.other_history)
|
||||
# 在途那条被跳过
|
||||
assert self._exists(self.in_flight)
|
||||
assert res["deleted"] == 3
|
||||
assert res["skipped"] == 1
|
||||
# 账号的 CK 仍在 —— 这就是解耦要的效果
|
||||
assert self._account_cookie(self.account.id) == "valid-ck-1"
|
||||
assert self._account_cookie(self.other.id) == "valid-ck-2"
|
||||
|
||||
def test_bulk_delete_reports_in_flight_skipped_count(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(all_matching=True, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
assert res["deleted"] == 3
|
||||
assert res["skipped"] == 1
|
||||
assert "在途" in res["message"]
|
||||
|
||||
def test_explicit_ids_can_delete_cookie_history_but_not_in_flight(self):
|
||||
"""用户手动勾选时,历史可删,在途仍不可删。"""
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_tasks(
|
||||
task_ids=f"{self.holds_ck_shows_failed},{self.no_ck}",
|
||||
task_ids=f"{self.failed_history},{self.in_flight}",
|
||||
db=db,
|
||||
current=self.user,
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
assert self._exists(self.holds_ck_shows_failed)
|
||||
assert not self._exists(self.no_ck)
|
||||
assert not self._exists(self.failed_history)
|
||||
assert self._exists(self.in_flight)
|
||||
assert res["deleted"] == 1
|
||||
assert res["skipped"] == 1
|
||||
assert self._account_cookie(self.account.id) == "valid-ck-1"
|
||||
|
||||
def test_single_delete_refuses_ck_holder(self):
|
||||
def test_single_delete_refuses_in_flight_record(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
with pytest.raises(Exception) as exc:
|
||||
delete_task(self.holds_ck_shows_success, db=db, current=self.user)
|
||||
delete_task(self.in_flight, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
assert "持有 CK" in str(exc.value)
|
||||
assert self._exists(self.holds_ck_shows_success)
|
||||
assert "正在被批次处理" in str(exc.value)
|
||||
assert self._exists(self.in_flight)
|
||||
|
||||
def test_single_delete_allows_empty_cookie(self):
|
||||
def test_single_delete_allows_cookie_history(self):
|
||||
db = self._fresh()
|
||||
try:
|
||||
res = delete_task(self.no_ck, db=db, current=self.user)
|
||||
res = delete_task(self.success_history, db=db, current=self.user)
|
||||
finally:
|
||||
db.close()
|
||||
assert res["success"] is True
|
||||
assert not self._exists(self.no_ck)
|
||||
assert not self._exists(self.success_history)
|
||||
assert self._account_cookie(self.account.id) == "valid-ck-1"
|
||||
|
||||
Reference in New Issue
Block a user