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

@@ -49,7 +49,12 @@ class StoredRouting:
class CredentialStore(Protocol):
"""凭据 + 档位路由的读写接口(按 owner_id 隔离)。"""
"""凭据 + 档位路由的读写接口(按 owner_id 隔离)。
写方法upsert/delete**只 flush 不 commit**——提交交调用方(端点/服务)经
`commit()` 统一一次,保证「多凭据一请求」的原子性(任一步失败整体回滚,不留半更新)。
无 session 句柄的服务侧调用方(如 token 刷新落库)则直接调 `commit()`。
"""
async def list_credentials(self, owner_id: uuid.UUID) -> list[StoredCredential]: ...
@@ -71,6 +76,8 @@ class CredentialStore(Protocol):
async def upsert_routing(self, routing: StoredRouting) -> None: ...
async def commit(self) -> None: ...
class ProviderProbe(Protocol):
"""最小连通探测:验证 Key + 返回能力矩阵。测试注入假探测,绝不联网。"""
@@ -156,7 +163,8 @@ class SqlCredentialStore:
existing.api_key_enc = api_key_enc
existing.auth_type = AUTH_TYPE_API_KEY
existing.oauth_enc = None
await self._session.commit()
# 仓储只 flush提交交调用方端点/服务)统一一次——保证多凭据一请求的原子性。
await self._session.flush()
async def upsert_oauth_credential(
self, owner_id: uuid.UUID, provider: str, oauth_enc: bytes
@@ -191,7 +199,8 @@ class SqlCredentialStore:
existing.api_key_enc = None
existing.auth_type = AUTH_TYPE_OAUTH
existing.oauth_enc = oauth_enc
await self._session.commit()
# 仓储只 flush提交交调用方统一一次。
await self._session.flush()
async def delete_credential(self, owner_id: uuid.UUID, provider: str) -> bool:
"""删除凭据行OAuth disconnect / 撤销)。返回是否删到行。"""
@@ -207,7 +216,8 @@ class SqlCredentialStore:
if existing is None:
return False
await self._session.delete(existing)
await self._session.commit()
# 仓储只 flush提交交调用方统一一次。
await self._session.flush()
return True
async def upsert_routing(self, routing: StoredRouting) -> None:
@@ -233,4 +243,9 @@ class SqlCredentialStore:
existing.provider = routing.provider
existing.model = routing.model
existing.fallback = routing.fallback
# 仓储只 flush提交交调用方统一一次。
await self._session.flush()
async def commit(self) -> None:
"""统一提交点:端点/服务侧在一组 flush 后调一次,落库所有挂起写入。"""
await self._session.commit()