fix(qa): 实施 4 组设计型 QA 项——规则删除/codex 角色/大纲卷过滤/文风回炉锚点

#5 规则 DELETE + id:暴露 RuleView.id(PK);新增 DELETE /projects/{id}/rules/{rule_id}
  (项目/规则不存在→404,成功→204,按 (id,project_id) 限定);rule_repo 加
  list_for_project/delete;RulesPage 每条加删除(乐观删+回滚+toast)。assemble 侧
  RuleView(缓存前缀)不动,列表另立 RuleListItemView。
#7 codex 角色 relations:写侧本已持久化、读端点 _existing_characters 硬编码 []。
  加 _relations_from_jsonb 解析 {name,kind,note},CodexPage 渲染关系 chip。
#8 角色入库幂等:SqlCharacterWriteRepo.create 改 (project_id,name) app 层 upsert——
  重复入库改更新而非插入;不加 UNIQUE/迁移(线上已有重复行会让约束迁移失败)。
#1 大纲卷过滤:GET /outline 支持可选 ?volume(无参=全部,向后兼容);OutlineEditor
  加「查看:全部/卷N」筛选,与生成目标卷解耦。
H3/#9 文风回炉锚点:StyleDriftSegment 加 text(逐字命中段),style.md 指示审稿输出;
  前端按内容锚点定位回炉目标(idx 仅排序),命中失败 → 提示「无法定位该段」而非
  静默 no-op。style golden fixture 已重生成。

契约变更已 pnpm gen:api(RuleView.id / DELETE rules / outline ?volume)。无迁移
(alembic 无漂移)。门禁绿:ruff/mypy(210)/alembic/pytest 760 · 前端 tsc/lint/vitest 329。
This commit is contained in:
Yaojia Wang
2026-06-25 12:53:03 +02:00
parent e60eff7aa1
commit bf39f50b2f
33 changed files with 907 additions and 99 deletions

View File

@@ -19,7 +19,7 @@ ledger否则 usage 静默丢失,同 draft/review 纪律)。无凭据 →
from __future__ import annotations
import uuid
from typing import Annotated
from typing import Annotated, Any
from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession
@@ -33,8 +33,9 @@ from ww_agents import (
from ww_core.domain import (
CharacterWriteRepo,
ProjectRepo,
RuleWriteRepo,
)
from ww_core.domain.repositories import MemoryRepos, RulesRepo
from ww_core.domain.repositories import MemoryRepos
from ww_core.orchestrator import (
precheck_generated_cards,
run_character_gen,
@@ -71,7 +72,7 @@ from ww_api.services.project_deps import (
get_memory_repos,
get_precheck_gateway,
get_project_repo,
get_rules_read_repo,
get_rule_write_repo,
get_skill_registry,
get_worldbuilder_gateway,
)
@@ -84,7 +85,7 @@ skills_router = APIRouter(prefix="/skills", tags=["skills"])
ProjectRepoDep = Annotated[ProjectRepo, Depends(get_project_repo)]
MemoryReposDep = Annotated[MemoryRepos, Depends(get_memory_repos)]
CharacterWriteRepoDep = Annotated[CharacterWriteRepo, Depends(get_character_write_repo)]
RulesReadRepoDep = Annotated[RulesRepo, Depends(get_rules_read_repo)]
RuleRepoDep = Annotated[RuleWriteRepo, Depends(get_rule_write_repo)]
SkillRegistryDep = Annotated[SkillRegistry, Depends(get_skill_registry)]
WorldGatewayDep = Annotated[Gateway, Depends(get_worldbuilder_gateway)]
CharacterGatewayDep = Annotated[Gateway, Depends(get_character_gen_gateway)]
@@ -111,10 +112,28 @@ def _render_characters_context(cards: list[CharacterCard]) -> str:
return "\n".join(f"- {c.name}{c.role}{''.join(c.traits) or '(未列)'}" for c in cards)
def _relations_from_jsonb(raw: list[Any]) -> list[CharacterRelation]:
"""`characters.relations` JSONB list每条 {name, kind, note?})→ schema CharacterRelation。
跳过缺 name/kind 的脏条目(历史/外部数据不可信,守输入校验边界)。
"""
out: list[CharacterRelation] = []
for item in raw or []:
if not isinstance(item, dict):
continue
name = item.get("name")
kind = item.get("kind")
if not name or not kind:
continue
out.append(CharacterRelation(name=str(name), kind=str(kind), note=item.get("note")))
return out
async def _existing_characters(memory: MemoryRepos, project_id: uuid.UUID) -> list[CharacterCard]:
"""把已有角色读侧视图转成 `CharacterCard`(喂防雷同 / precheck
"""把已有角色读侧视图转成 `CharacterCard`(喂防雷同 / precheck / 设定库读端点)。
DB JSONB dict 列 → schema list/str 反向解包(与写侧形变互逆;缺则空/占位)。
`relations` 从 JSONB list 还原(设定库 Codex 需展示关系网precheck 不读此字段,无害)。
"""
views = await memory.character.list_for_project(project_id)
cards: list[CharacterCard] = []
@@ -133,7 +152,7 @@ async def _existing_characters(memory: MemoryRepos, project_id: uuid.UUID) -> li
arc=arc or "",
speech_tics=tics,
tags=list(v.tags or []),
relations=[],
relations=_relations_from_jsonb(list(v.relations or [])),
)
)
return cards
@@ -377,14 +396,16 @@ async def list_world_entities(
@router.get("/{project_id}/rules")
async def list_rules(
project_id: uuid.UUID,
repo: RulesReadRepoDep,
repo: RuleRepoDep,
project_repo: ProjectRepoDep,
) -> RuleListResponse:
"""规则列表(按读侧顺序)。规则页用。项目不存在 → 404QA MEDIUM此前返误导性空 200"""
"""规则列表(带 id供前端删除 handle。项目不存在 → 404QA MEDIUM此前返误导性空 200"""
if await project_repo.get(STUB_OWNER_ID, project_id) is None:
raise AppError(ErrorCode.NOT_FOUND, f"project not found: {project_id}")
rules = await repo.all_for_project(project_id)
return RuleListResponse(rules=[RuleView(level=r.level, content=r.content) for r in rules])
rules = await repo.list_for_project(project_id)
return RuleListResponse(
rules=[RuleView(id=r.id, level=r.level, content=r.content) for r in rules]
)
@skills_router.get("")

View File

@@ -143,10 +143,12 @@ async def get_outline(
request: Request,
project_repo: ProjectRepoDep,
outline_repo: OutlineReadRepoDep,
volume: int | None = None,
) -> OutlineResponse:
"""读取已持久化的大纲(逐章,按 chapter_no 升序)。
项目不存在 → 404项目存在但尚无大纲 → 200 空列表(非 404页面初次访问的常态)。
可选 `?volume=N`:只返回该卷的章节(前端按卷切换);不传 → 全部章节(向后兼容)。
项目不存在 → 404项目存在但尚无大纲或该卷无章节→ 200 空列表(非 404
DB `outline.beats` 是 JSONB `{"beats": [...]}` → 解包成裸 `list[str]`(与 POST 响应同形,
前端 OpenAPI 类型对齐)。读侧复用 C5 assemble 的 `OutlineRepo`,不写库。
"""
@@ -157,6 +159,8 @@ async def get_outline(
raise AppError(ErrorCode.NOT_FOUND, f"project not found: {project_id}")
views = await outline_repo.list_for_project(project_id)
if volume is not None:
views = [v for v in views if v.volume == volume]
chapters = [
OutlineChapterView(
no=view.chapter_no,
@@ -171,6 +175,7 @@ async def get_outline(
"outline_read",
project_id=str(project_id),
request_id=request_id,
volume=volume,
chapter_count=len(chapters),
)
return OutlineResponse(chapters=chapters)

View File

@@ -15,7 +15,7 @@ from __future__ import annotations
import uuid
from typing import Annotated
from fastapi import APIRouter, Depends, Request
from fastapi import APIRouter, Depends, Request, Response
from sqlalchemy.ext.asyncio import AsyncSession
from ww_core.domain import RuleWriteRepo
from ww_core.domain.project_repo import ProjectRepo
@@ -60,4 +60,34 @@ async def create_rule(
request_id=request_id,
level=body.level,
)
return RuleView(level=view.level, content=view.content)
return RuleView(id=view.id, level=view.level, content=view.content)
@router.delete("/{project_id}/rules/{rule_id}", status_code=204)
async def delete_rule(
project_id: uuid.UUID,
rule_id: uuid.UUID,
request: Request,
repo: RuleWriteRepoDep,
project_repo: ProjectRepoDep,
session: SessionDep,
) -> Response:
"""删除一条本作品规则204。项目不存在 → 404规则不存在 / 不属于该项目 → 404。
删规则是**作者显式动作**(不变量 #3规则增删不经 AI 静默写库。repo.delete 只 flush
端点提交;删不到行(未知 id / 跨项目)→ 不提交、抛 404。
"""
request_id = getattr(request.state, "request_id", None)
if await project_repo.get(STUB_OWNER_ID, project_id) is None:
raise AppError(ErrorCode.NOT_FOUND, f"project not found: {project_id}")
deleted = await repo.delete(project_id, rule_id)
if not deleted:
raise AppError(ErrorCode.NOT_FOUND, f"rule not found: {rule_id}")
await session.commit()
log.info(
"rule_deleted",
project_id=str(project_id),
rule_id=str(rule_id),
request_id=request_id,
)
return Response(status_code=204)

View File

@@ -7,6 +7,7 @@ snake_case前端经 OpenAPI 生成 TS 类型消费。改字段 → 前端必
from __future__ import annotations
import uuid
from typing import Annotated, Literal
from pydantic import BaseModel, Field, StringConstraints
@@ -25,7 +26,11 @@ class RuleCreateRequest(BaseModel):
class RuleView(BaseModel):
"""规则视图创建后回显snake_case"""
"""规则视图(创建后回显 + 列表项snake_case
`id` 是该规则行的稳定主键——前端规则页用它作删除 handleDELETE /rules/{rule_id})。
"""
id: uuid.UUID
level: str
content: str