feat(backend): 整章重写 AI 反问澄清预检端点 rewrite/clarify(WFW-9 M2)
新增 POST /projects/{id}/chapters/{no}/rewrite/clarify(非流式 JSON)→ ClarifyDecision,
镜像 refine 侧 clarify:404 缺项目 → _build_rewrite_clarify_input(章级意见 + prior_draft
有界摘录 3000 字,不整章入 LLM)→ run_clarify(clarify_rewrite_spec, analyst 档) → 末尾
commit 落 usage_ledger → 返回决策。只读不写业务表(不变量 #3);日志只记 feedback_len(脱敏);
畸形 parsed 经 run_clarify 韧性回退 need_clarification=false(非 500)。新增 RewriteClarifyRequest
(feedback 必给、prior_draft 可选)。6 端点用例覆盖含糊/明确/404/fail-open/422/摘录有界不泄漏。
This commit is contained in:
@@ -10,7 +10,14 @@ import uuid
|
||||
import httpx
|
||||
import pytest
|
||||
from cryptography.fernet import Fernet
|
||||
from fakes_projects import FakeChapterRepo, FakeProjectRepo, FakeWriterGateway
|
||||
from fakes_projects import (
|
||||
FakeChapterRepo,
|
||||
FakeProjectRepo,
|
||||
FakeReviewGateway,
|
||||
FakeSession,
|
||||
FakeWriterGateway,
|
||||
)
|
||||
from fastapi import FastAPI
|
||||
from ww_api.services.credentials import STUB_OWNER_ID
|
||||
from ww_core.domain.project_repo import ProjectView
|
||||
from ww_core.domain.repositories import (
|
||||
@@ -361,3 +368,191 @@ async def test_get_draft_404_when_no_draft() -> None:
|
||||
resp = await client.get(f"/projects/{pid}/chapters/9/draft")
|
||||
assert resp.status_code == 404
|
||||
assert resp.json()["error"]["code"] == ErrorCode.NOT_FOUND
|
||||
|
||||
|
||||
# ---- POST /rewrite/clarify(整章重写预检澄清,WFW-9 M2)----
|
||||
|
||||
# 摘录上界外的哨兵:喂超长 prior_draft,断言超界正文不进网关 input(不喂全章、不泄漏)。
|
||||
_LEAK_SENTINEL = "泄漏哨兵段落禁止外泄"
|
||||
|
||||
|
||||
def _need_clarification_decision() -> object:
|
||||
from ww_agents import ClarifyDecision, ClarifyOption, ClarifyQuestion
|
||||
|
||||
return ClarifyDecision(
|
||||
need_clarification=True,
|
||||
questions=[
|
||||
ClarifyQuestion(
|
||||
question="你说本章节奏太慢,是想往哪个方向重写整章?",
|
||||
options=[
|
||||
ClarifyOption(label="砍支线压缩篇幅", value="删掉次要支线,压缩铺陈直入主线"),
|
||||
ClarifyOption(label="提前引爆冲突", value="把本章高潮冲突提前到前半段"),
|
||||
],
|
||||
allow_free_text=True,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def _clear_rewrite_decision() -> object:
|
||||
from ww_agents import ClarifyDecision
|
||||
|
||||
return ClarifyDecision(
|
||||
need_clarification=False,
|
||||
verification="我会把本章高潮提前、删掉开头回忆插叙,直接进冲突,对吗?",
|
||||
)
|
||||
|
||||
|
||||
def _clarify_app(
|
||||
*,
|
||||
project_repo: FakeProjectRepo,
|
||||
session: FakeSession | None = None,
|
||||
clarify_gateway: object | None = None,
|
||||
) -> FastAPI:
|
||||
"""构造只挂 rewrite/clarify 端点依赖的 app(project_repo + session + clarify 网关)。"""
|
||||
import os
|
||||
|
||||
os.environ.setdefault("CREDENTIAL_ENC_KEY", Fernet.generate_key().decode())
|
||||
from ww_api.main import create_app
|
||||
from ww_api.services.project_deps import get_clarify_gateway, get_project_repo
|
||||
from ww_db import get_session
|
||||
|
||||
app = create_app()
|
||||
app.dependency_overrides[get_project_repo] = lambda: project_repo
|
||||
app.dependency_overrides[get_session] = lambda: session or FakeSession()
|
||||
if clarify_gateway is not None:
|
||||
app.dependency_overrides[get_clarify_gateway] = lambda: clarify_gateway
|
||||
return app
|
||||
|
||||
|
||||
def _clarify_client(app: FastAPI) -> httpx.AsyncClient:
|
||||
transport = httpx.ASGITransport(app=app, raise_app_exceptions=False)
|
||||
return httpx.AsyncClient(transport=transport, base_url="http://test")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_vague_feedback_asks_one_question_and_commits() -> None:
|
||||
# (a) 含糊章级意见 → need_clarification True + 恰 1 问;analyst 档 + 末尾 commit 落账。
|
||||
project_repo = FakeProjectRepo()
|
||||
pid = _seed_project(project_repo)
|
||||
session = FakeSession()
|
||||
gateway = FakeReviewGateway(parsed=_need_clarification_decision())
|
||||
app = _clarify_app(project_repo=project_repo, session=session, clarify_gateway=gateway)
|
||||
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{pid}/chapters/3/rewrite/clarify",
|
||||
json={"feedback": "节奏太慢", "prior_draft": "本章开头……"},
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["need_clarification"] is True
|
||||
assert len(body["questions"]) == 1
|
||||
assert body["questions"][0]["options"][0]["label"] == "砍支线压缩篇幅"
|
||||
assert body["questions"][0]["allow_free_text"] is True
|
||||
# 不变量 #2:analyst 档;意见折进网关 input;末尾一次 commit(网关 usage_ledger 落库)。
|
||||
assert gateway.requests[0].tier == "analyst"
|
||||
assert "节奏太慢" in str(gateway.requests[0].input)
|
||||
assert session.commits == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_clear_feedback_proceeds_read_only() -> None:
|
||||
# (b) 明确意见 → need_clarification False + verification;只读、无回滚。
|
||||
project_repo = FakeProjectRepo()
|
||||
pid = _seed_project(project_repo)
|
||||
session = FakeSession()
|
||||
gateway = FakeReviewGateway(parsed=_clear_rewrite_decision())
|
||||
app = _clarify_app(project_repo=project_repo, session=session, clarify_gateway=gateway)
|
||||
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{pid}/chapters/3/rewrite/clarify",
|
||||
json={"feedback": "把本章高潮提前到前半段,删掉开头回忆插叙。"},
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["need_clarification"] is False
|
||||
assert body["questions"] == []
|
||||
assert body["verification"] is not None
|
||||
# 只读不写业务表:仅一次 commit 落网关 usage,无回滚(不变量 #3)。
|
||||
assert session.commits == 1
|
||||
assert session.rollbacks == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_unknown_project_404() -> None:
|
||||
# (c) 项目不存在 → 404(触网关前 fail-fast)。
|
||||
app = _clarify_app(
|
||||
project_repo=FakeProjectRepo(),
|
||||
clarify_gateway=FakeReviewGateway(parsed=_clear_rewrite_decision()),
|
||||
)
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{uuid.uuid4()}/chapters/1/rewrite/clarify",
|
||||
json={"feedback": "节奏太慢"},
|
||||
)
|
||||
assert resp.status_code == 404
|
||||
assert resp.json()["error"]["code"] == ErrorCode.NOT_FOUND
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_fails_open_on_wrong_parsed() -> None:
|
||||
# (d) 网关返回非 ClarifyDecision → 端点仍 200 + need_clarification False(韧性回退,非 500)。
|
||||
from ww_agents import WorldGenResult
|
||||
|
||||
project_repo = FakeProjectRepo()
|
||||
pid = _seed_project(project_repo)
|
||||
session = FakeSession()
|
||||
gateway = FakeReviewGateway(parsed=WorldGenResult(entities=[]))
|
||||
app = _clarify_app(project_repo=project_repo, session=session, clarify_gateway=gateway)
|
||||
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{pid}/chapters/3/rewrite/clarify",
|
||||
json={"feedback": "重写一遍"},
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["need_clarification"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_empty_feedback_422() -> None:
|
||||
project_repo = FakeProjectRepo()
|
||||
pid = _seed_project(project_repo)
|
||||
app = _clarify_app(
|
||||
project_repo=project_repo,
|
||||
clarify_gateway=FakeReviewGateway(parsed=_clear_rewrite_decision()),
|
||||
)
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{pid}/chapters/1/rewrite/clarify", json={"feedback": ""}
|
||||
)
|
||||
assert resp.status_code == 422
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rewrite_clarify_bounds_excerpt_and_never_leaks_manuscript() -> None:
|
||||
# 有界摘录:超长 prior_draft 的越界正文不进网关 input,响应也绝不回灌正文(脱敏)。
|
||||
project_repo = FakeProjectRepo()
|
||||
pid = _seed_project(project_repo)
|
||||
session = FakeSession()
|
||||
gateway = FakeReviewGateway(parsed=_clear_rewrite_decision())
|
||||
app = _clarify_app(project_repo=project_repo, session=session, clarify_gateway=gateway)
|
||||
|
||||
# 哨兵放在远超摘录上界处(10k 填充 + 哨兵),确保被截断在摘录外。
|
||||
prior_draft = ("正" * 10_000) + _LEAK_SENTINEL
|
||||
|
||||
async with _clarify_client(app) as client:
|
||||
resp = await client.post(
|
||||
f"/projects/{pid}/chapters/3/rewrite/clarify",
|
||||
json={"feedback": "重写本章", "prior_draft": prior_draft},
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
# 越界正文既不进 LLM 输入,也不出现在响应体(正文只喂有界摘录、绝不回灌)。
|
||||
assert _LEAK_SENTINEL not in str(gateway.requests[0].input)
|
||||
assert _LEAK_SENTINEL not in resp.text
|
||||
|
||||
Reference in New Issue
Block a user