fix(txn+security): 仓储改 flush + 启动校验/兜底 + job.error 脱敏 + SSE 异常硬化

P0-1 SqlCredentialStore/save_draft 由自提交改 flush,端点/服务统一 commit
  (新增 CredentialStore.commit() 统一提交点;token 刷新落库显式提交);
  补多凭据一请求中途失败整体回滚集成测试。
P0-2 启动校验 _fernet(enc_key) 快速失败 + catch-all Exception → ErrorEnvelope;
  credential_enc_key 改 SecretStr。
P0-3 run_job 异常分类:AppError 存 code+message,其余存通用文案不泄 str(exc)。
P0-4 评审/正文 SSE 失败先发 error 事件,尾部 commit 包 try/except。
P1-4 max_version 加 FOR UPDATE 行锁消除 TOCTOU。
P1-5 scan_overdue 谓词下推 + 批量 UPDATE RETURNING。
P1-10 移除 OAuth user_code 日志。
P2 provider_deps 改调网关 build_adapter;accept_service Committable Protocol;
  CORS 白名单收窄;request_id 安全字符集白名单;stdlib 日志接管;读端点 404 校验;
  httpx timeout;测试用合法 Fernet key;类型化响应模型(JobResponse/DimensionEntry/
  ReviewConflictView/selling_points)+路由 ErrorEnvelope responses(供 codegen)。
This commit is contained in:
Yaojia Wang
2026-06-21 19:32:24 +02:00
parent 2282d4fd24
commit 345cc73965
37 changed files with 737 additions and 115 deletions

View File

@@ -13,7 +13,7 @@ import uuid
from dataclasses import dataclass
from typing import Protocol
from sqlalchemy import func, select
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from ww_db.models import Chapter
@@ -48,7 +48,11 @@ class ChapterView:
class ChapterRepo(Protocol):
"""章节草稿读写 + 验收晋升接口(按 project_id 隔离)。"""
"""章节草稿读写 + 验收晋升接口(按 project_id 隔离)。
写方法save_draft/promote_to_accepted**只 flush 不 commit**——提交交调用方
(端点/验收事务)统一一次,保证原子性与全仓「仓储只 flush」契约一致。
"""
async def save_draft(
self, project_id: uuid.UUID, chapter_no: int, *, text: str, volume: int = 1
@@ -116,6 +120,7 @@ class SqlChapterRepo:
self, project_id: uuid.UUID, chapter_no: int, *, text: str, volume: int = 1
) -> ChapterDraftView:
# 唯一约束 (project_id, chapter_no, version);草稿版次固定 → 覆盖同一行,幂等。
# **只 flush 不 commit**:提交交调用方(端点)统一一次,与全仓「仓储只 flush」契约一致。
existing = await self._find_draft(project_id, chapter_no)
if existing is None:
row = Chapter(
@@ -127,12 +132,12 @@ class SqlChapterRepo:
version=DRAFT_VERSION,
)
self._s.add(row)
await self._s.commit()
await self._s.flush()
await self._s.refresh(row)
return _to_view(row)
existing.content = text
existing.status = DRAFT_STATUS
await self._s.commit()
await self._s.flush()
await self._s.refresh(existing)
return _to_view(existing)
@@ -143,13 +148,21 @@ class SqlChapterRepo:
return _to_view(row)
async def max_version(self, project_id: uuid.UUID, chapter_no: int) -> int:
result = await self._s.execute(
select(func.max(Chapter.version)).where(
Chapter.project_id == project_id,
Chapter.chapter_no == chapter_no,
# 消除 read-then-insert 的 TOCTOU 竞态:`SELECT version ... FOR UPDATE` 锁住该章
# 现有行PG 不允许聚合函数与 FOR UPDATE 并用,故选具体行加锁再 Python 取 max
# 并发验收同一章时,后到者阻塞到前者提交后才读到更新后的 max避免撞唯一约束抛 500
rows = (
await self._s.execute(
select(Chapter.version)
.where(
Chapter.project_id == project_id,
Chapter.chapter_no == chapter_no,
)
.with_for_update()
)
)
return result.scalar_one_or_none() or 0
).scalars()
versions = list(rows)
return max(versions) if versions else 0
async def promote_to_accepted(
self, project_id: uuid.UUID, chapter_no: int, *, content: str, volume: int = 1

View File

@@ -20,13 +20,14 @@ import uuid
from typing import Any, Protocol
from pydantic import BaseModel, Field
from sqlalchemy import select
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from ww_db.models import Foreshadow
from ww_core.domain.foreshadow_state import (
CLOSED,
OVERDUE,
ForeshadowStatus,
apply_overdue_scan,
)
from ww_core.domain.foreshadow_state import (
transition as apply_transition,
@@ -201,19 +202,24 @@ class SqlForeshadowLedgerRepo:
async def scan_overdue(
self, project_id: uuid.UUID, *, current_chapter: int
) -> list[ForeshadowLedgerView]:
rows = (
await self._s.execute(select(Foreshadow).where(Foreshadow.project_id == project_id))
).scalars()
changed: list[ForeshadowLedgerView] = []
for row in rows:
new = apply_overdue_scan(
ForeshadowStatus(row.status),
current_chapter=current_chapter,
expected_close_to=row.expected_close_to,
).value
if new != row.status:
row.status = new
changed.append(_to_view(row))
if changed:
await self._s.flush()
return changed
# 谓词下推 + 批量 UPDATE ... RETURNING不再拉全表到内存逐行过滤/更新P1-5
# 判据与 `foreshadow_state.is_overdue` 一致expected_close_to 非空且
# current_chapter > expected_close_to 且当前状态既非 CLOSED终态也非 OVERDUE
# (已逾期则无变更)——后两者排除使本批量更新等价于「只改真正发生状态翻转的行」。
stmt = (
update(Foreshadow)
.where(
Foreshadow.project_id == project_id,
Foreshadow.expected_close_to.is_not(None),
Foreshadow.expected_close_to < current_chapter,
Foreshadow.status.not_in([CLOSED.value, OVERDUE.value]),
)
.values(status=OVERDUE.value)
.returning(Foreshadow)
)
rows = (await self._s.execute(stmt)).scalars().all()
# 先把命中行物化为不可变 View脱离 ORM 身份),再 expire——避免 UPDATE...RETURNING
# 与 session 里已加载实例的状态不一致(后续读重取最新)。
views = [_to_view(row) for row in rows]
self._s.expire_all()
return views