feat: 深度研究支持独立新报告与原文改写,并完善沙箱、模型管理与全文改写

新报告不再覆盖当前文件卡片,写入时打开沙箱;步骤条仅在改写原文时位于卡片上方。同时补齐改写本地回退、模型管理与会商第三步相关改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-21 21:46:04 +08:00
1 parent 11f928bc95
commit a5a9c952e8
63 files changed
+4113 -929

No files matched your search

@@ -24,9 +24,10 @@ import mimetypes
import re
import uuid
from datetime import UTC, datetime
from typing import Any
from typing import Any, Literal
from fastapi import APIRouter, HTTPException, Query, Request
from fastapi.encoders import jsonable_encoder
from fastapi.responses import FileResponse, StreamingResponse
from pydantic import BaseModel, Field
@@ -50,6 +51,15 @@ _POLL_INTERVAL_S = 0.8
_LIVE_WAIT_S = 0.2
_REPORT_IMAGE_REF_RE = re.compile(r"deep-research://([A-Za-z0-9._-]+)")
_INLINE_REPORT_IMAGE_RE = re.compile(r"!\[([^\]]*)\]\(deep-research://([A-Za-z0-9._-]+)\)")
# Reports produced by the research writer can contain either its in-progress
# ``[[source:id]]`` marker or the saved ``[来源:id]`` marker. A structural
# variant gets new source ids, so both forms must be rebased before the copied
# report is ever used as the new writer's input.
_REPORT_SOURCE_MARKER_RE = re.compile(
r"\[\[\s*source\s*[::]\s*([A-Za-z0-9_-]{1,128})\s*\]\]"
r"|\[\s*来源\s*[::]\s*([A-Za-z0-9_-]{1,128})\s*\]",
re.IGNORECASE,
)
_MAX_HTML_EXPORT_IMAGE_BYTES = 20 * 1024 * 1024
@@ -157,12 +167,22 @@ class FullReportRewriteRequest(BaseModel):
"""A full report rewrite initiated from the sandbox file header."""
instruction: str = Field(..., min_length=1, max_length=4_000)
operation: Literal["rewrite", "generate"] = "rewrite"
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
style: str | None = Field(default=None, max_length=64)
model_name: str | None = Field(default=None, alias="modelName", max_length=256)
model_config = {"populate_by_name": True}
class CreateReportVariantRequest(BaseModel):
"""Create an independent report before regenerating it with a new structure."""
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
model_config = {"populate_by_name": True}
class SourceSelectionRequest(BaseModel):
"""A user's explicit source-admission decision for follow-up chat.
@@ -302,6 +322,41 @@ def _report_hash(report: str) -> str:
return hashlib.sha256(report.encode("utf-8")).hexdigest()
def _instruction_with_report_outline(instruction: str, report_outline: str) -> str:
"""Make a selected report template an explicit rewrite constraint."""
normalized_instruction = instruction.strip()
normalized_outline = report_outline.strip()
if not normalized_outline:
return normalized_instruction
return "\n\n".join(
(
normalized_instruction,
"【报告结构要求】",
"请按以下 Markdown 章节结构重组全文;不适用的章节可以合并或省略,且不得为套用结构编造事实。",
normalized_outline,
)
)
def _rebase_report_source_markers(report: str, source_id_map: dict[str, str]) -> str:
"""Make inherited citations safe for a structurally regenerated report.
A report variant owns copied source rows with fresh ids. Its source set
contains *only* the parent's selected evidence. Keeping a marker for a
parent source that was not selected would make the next writer faithfully
repeat an id it cannot use, then fail the grounded-citation validation.
Rebase selected markers to the writer's canonical ``[[source:id]]`` form
and remove excluded markers before the model receives the source report.
"""
def replace(match: re.Match[str]) -> str:
original_id = match.group(1) or match.group(2)
cloned_id = source_id_map.get(original_id)
return f"[[source:{cloned_id}]]" if cloned_id else ""
return _REPORT_SOURCE_MARKER_RE.sub(replace, report)
def _report_visible_char_count(report: str) -> int:
return len("".join(report.split()))
@@ -500,8 +555,26 @@ async def list_sessions(
) -> dict[str, Any]:
user_id = await _require_user(request)
store = _get_session_store(request)
items = await store.list_by_user(user_id=user_id, status=status, limit=limit, offset=offset)
total = await store.count_by_user(user_id=user_id, status=status)
# Filter child reports before applying the public page window. Otherwise a
# page containing many variants can look empty even though parent research
# records still exist after it.
unfiltered = await store.list_by_user(
user_id=user_id,
status=status,
limit=100,
offset=0,
include_config=True,
)
# A generated report variant is a child file in its parent's conversation,
# not a separate research history entry. Older builds leaked these rows into
# the rail; filter them without deleting their durable reports.
visible = [
row
for row in unfiltered
if not bool((row.get("config_snapshot") or {}).get("report_variant"))
]
items = visible[offset : offset + limit]
total = len(visible)
return {
"items": [_session_to_response(r) for r in items],
"total": total,
@@ -520,6 +593,32 @@ async def get_session(session_id: str, request: Request) -> SessionResponse:
return _session_to_response(row, full=True)
@router.get("/sessions/{session_id}/report-variants")
async def list_report_variants(session_id: str, request: Request) -> dict[str, Any]:
"""Return durable child reports for rendering inside the parent history."""
user_id = await _require_user(request)
store = _get_session_store(request)
parent = await store.get(session_id, user_id=user_id)
if parent is None:
raise HTTPException(status_code=404, detail="Session not found")
rows = await store.list_by_user(user_id=user_id, limit=100, offset=0, include_config=True)
children = [
row
for row in rows
if bool((row.get("config_snapshot") or {}).get("report_variant"))
and str((row.get("config_snapshot") or {}).get("parent_session_id") or "") == session_id
]
# Stores list newest first; a conversation reads from oldest to newest.
children.reverse()
full_children: list[dict[str, Any]] = []
for child in children:
full = await store.get(str(child["id"]), user_id=user_id)
if full is not None:
full_children.append(full)
return {"items": [_session_to_response(row, full=True) for row in full_children]}
@router.patch("/sessions/{session_id}", response_model=SessionResponse)
async def update_session(session_id: str, body: UpdateSessionRequest, request: Request) -> SessionResponse:
user_id = await _require_user(request)
@@ -583,31 +682,51 @@ async def start_job(session_id: str, body: StartJobRequest, request: Request) ->
session = await session_store.get(session_id, user_id=user_id)
if session is None:
raise HTTPException(status_code=404, detail="Session not found")
if body.entry not in ("chat", "legacy"):
if body.entry not in ("chat", "legacy", "regenerate"):
raise HTTPException(status_code=422, detail=f"Unknown job entry '{body.entry}'")
config_dict = session.get("config_snapshot") or {}
session_config = dict(session.get("config_snapshot") or {})
is_report_variant = session_config.pop("report_variant", False) is True
# Parent linkage is persistence metadata, not a DeepResearchConfig field.
# Keep it on the child session, but never pass it to Pydantic or the runner.
parent_session_id = session_config.pop("parent_session_id", None)
config_dict = session_config
if body.entry == "chat":
# Chat entry requires a real collector-agent thread to harvest from.
if not session.get("runtime_thread_id"):
raise HTTPException(status_code=409, detail="Session has no collection thread to harvest")
if body.config:
# The report-config card is confirmed at write time; merge its
# choices over the frozen snapshot so the job (and later replays)
# see exactly what the user confirmed.
merged = {**config_dict, **body.config}
try:
merged_config = DeepResearchConfig.model_validate(merged).clamp()
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=422, detail=f"Invalid report config: {exc}") from None
config_dict = merged_config.model_dump()
await session_store.update(
session_id,
user_id=user_id,
config_snapshot=config_dict,
mode=merged_config.mode,
)
elif body.entry == "regenerate":
if not is_report_variant:
raise HTTPException(status_code=409, detail="Only an independent report variant can be regenerated")
selected_count = await _get_source_store(request).count_by_session(
session_id,
user_id=user_id,
selected=True,
)
if selected_count < 1:
raise HTTPException(status_code=409, detail="No selected source material is available for regeneration")
if body.entry in ("chat", "regenerate") and body.config:
# Both first-write and regenerate use the ordinary report engine. The
# latter swaps network collection for the already selected source pool.
merged = {**config_dict, **body.config}
try:
merged_config = DeepResearchConfig.model_validate(merged).clamp()
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=422, detail=f"Invalid report config: {exc}") from None
config_dict = merged_config.model_dump()
persisted_config = dict(config_dict)
if is_report_variant:
persisted_config["report_variant"] = True
if parent_session_id:
persisted_config["parent_session_id"] = parent_session_id
await session_store.update(
session_id,
user_id=user_id,
config_snapshot=persisted_config,
mode=merged_config.mode,
)
# Per-user active job limit (§19.4).
active = await session_store.count_active_by_user(user_id=user_id)
@@ -673,6 +792,85 @@ async def start_job(session_id: str, body: StartJobRequest, request: Request) ->
}
@router.post("/sessions/{session_id}/report-variants", response_model=SessionResponse)
async def create_report_variant(
session_id: str,
body: CreateReportVariantRequest,
request: Request,
) -> SessionResponse:
"""Fork a completed report so a structural regeneration never overwrites it.
The new session starts as a blank report with a cloned selected-evidence
pool. The browser then queues the ordinary report engine against it. The
parent stays independently visible and no stale parent Markdown can flash
or overwrite the new stream.
"""
user_id = await _require_user(request)
session_store = _get_session_store(request)
source_store = _get_source_store(request)
parent = await session_store.get(session_id, user_id=user_id)
if parent is None:
raise HTTPException(status_code=404, detail="Session not found")
report = str(parent.get("report_markdown") or "")
if parent.get("status") != "completed" or not report.strip():
raise HTTPException(status_code=409, detail="A completed report is required before regeneration")
outline = body.report_outline.strip() if body.report_outline else ""
config_snapshot = dict(parent.get("config_snapshot") or {})
# A structural regeneration writes through the same durable executor as a
# chat-collected report, which gives it a runtime thread for sandbox I/O.
# Keep an explicit marker so the frontend can seed the topic bubble for the
# otherwise-empty variant thread while reusing the normal report timeline.
config_snapshot["report_variant"] = True
config_snapshot["parent_session_id"] = session_id
if outline:
config_snapshot["custom_outline"] = outline
parent_title = str(parent.get("title") or parent.get("query") or "研究报告").strip()
variant_id = _new_id("drs")
variant_row = await session_store.create(
id=variant_id,
user_id=user_id,
title=f"{parent_title} · 新结构报告",
query=str(parent.get("query") or ""),
mode=str(parent.get("mode") or "basic"),
status="draft",
config_snapshot=config_snapshot,
)
try:
# Give the variant the same hidden thread-backed sandbox as an initial
# report. History can therefore reuse the normal message/timeline UI
# instead of falling into the one-off legacy summary layout.
await _ensure_chat_runtime_thread(request, variant_row, user_id)
source_id_map = await source_store.clone_selected_to_session(
session_id,
variant_id,
user_id=user_id,
)
updated = await session_store.update(
variant_id,
user_id=user_id,
report_markdown=None,
report_html=None,
source_count=len(source_id_map),
# Never copy ``lastJobId``: doing so would make the variant replay
# its parent's historical progress trace as if it were its own.
usage_snapshot={},
)
except Exception: # noqa: BLE001
# A half-created report would be confusing in history. Keep the
# original untouched and fail this request before any rewrite starts.
try:
await session_store.delete(variant_id, user_id=user_id)
except Exception: # noqa: BLE001
logger.exception("could not clean up incomplete report variant %s", variant_id)
logger.exception("could not clone deep-research report variant from %s", session_id)
raise HTTPException(status_code=500, detail="创建新结构报告失败,请稍后重试") from None
if updated is None:
raise HTTPException(status_code=500, detail="创建新结构报告失败,请稍后重试")
return _session_to_response(updated, full=True)
@router.post("/sessions/{session_id}/document-rewrite")
async def start_full_report_rewrite_job(
session_id: str,
@@ -696,25 +894,37 @@ async def start_full_report_rewrite_job(
if session.get("status") != "completed" or not report.strip():
raise HTTPException(status_code=409, detail="A completed report is required before full rewrite")
instruction = body.instruction.strip()
report_outline = body.report_outline.strip() if body.report_outline else ""
instruction = _instruction_with_report_outline(body.instruction, report_outline)
style = body.style.strip() if body.style else "formal_analysis"
sources = await source_store.list_by_session(
session_id,
user_id=user_id,
selected=True,
limit=32,
# Regeneration must see the same admitted evidence set as the source
# report. The prompt builder enforces its own total character budget.
limit=500,
include_content=True,
)
# Historical SQL-backed rows expose ``created_at`` as a datetime whereas
# older SQLite rows happened to expose strings. The durable job snapshot
# must be JSON-only on both backends; otherwise a completed historical
# report fails before it can queue a rewrite (and the browser misreports
# that 500 response as a CORS failure).
sources_snapshot = jsonable_encoder(sources)
request_payload = {
"kind": "full_report_rewrite",
"operation": body.operation,
"base_hash": _report_hash(report),
"instruction": instruction,
"report_outline": report_outline,
"style": style,
"model_name": body.model_name,
"source_ids": [str(source.get("id") or "") for source in sources],
}
snapshot = {
"kind": "full_report_rewrite",
"operation": body.operation,
"session_id": session_id,
"user_id": user_id,
"query": session.get("query", ""),
@@ -727,8 +937,9 @@ async def start_full_report_rewrite_job(
},
"config": session.get("config_snapshot") or {},
"report": report,
"sources": sources,
"sources": sources_snapshot,
"instruction": instruction,
"report_outline": report_outline,
"style": style,
"model_name": body.model_name,
"entry": "document_rewrite",
@@ -775,7 +986,9 @@ async def get_latest_full_report_rewrite_job(session_id: str, request: Request)
"job": _job_to_response(row).model_dump(),
"result": row.get("result_snapshot") or {},
"request": {
"operation": str(snapshot.get("operation") or "rewrite"),
"instruction": str(snapshot.get("instruction") or ""),
"reportOutline": str(snapshot.get("report_outline") or ""),
"style": str(snapshot.get("style") or "formal_analysis"),
"modelName": snapshot.get("model_name"),
},
@@ -1195,7 +1408,8 @@ async def stream_full_report_rewrite(
)
base_hash = _report_hash(report)
style = body.style.strip() if body.style else "formal_analysis"
instruction = body.instruction.strip()
report_outline = body.report_outline.strip() if body.report_outline else ""
instruction = _instruction_with_report_outline(body.instruction, report_outline)
rewrite_target = _make_rewrite_target(report, 0, len(report), "全文")
question = (
"请重写整篇研究报告。"
@@ -1335,6 +1549,7 @@ async def stream_full_report_rewrite(
)
return
version_id: str | None = None
version_snapshot_failed = False
if version_store is not None:
try:
version_id = f"drv_{uuid.uuid4().hex}"
@@ -1351,27 +1566,24 @@ async def stream_full_report_rewrite(
model_name=body.model_name,
)
except Exception: # noqa: BLE001
logger.exception("could not create report rewrite version for %s", session_id)
await session_store.replace_report_if_unchanged(
# A version snapshot is best effort. The rewritten
# report is already committed and must remain usable.
logger.exception(
"could not persist report rewrite version for %s; keeping rewritten report",
session_id,
user_id=user_id,
expected_report=rewritten,
report_markdown=report,
)
yield format_sse(
"document_rewrite",
{"type": "error", "message": "保存改写版本失败,报告已恢复原文。", "stage": "commit"},
)
return
try:
await version_store.prune_source(
user_id=user_id,
source_type="deep_research_report",
source_id=session_id,
source_path="report.md",
)
except Exception: # noqa: BLE001
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
version_id = None
version_snapshot_failed = True
else:
try:
await version_store.prune_source(
user_id=user_id,
source_type="deep_research_report",
source_id=session_id,
source_path="report.md",
)
except Exception: # noqa: BLE001
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
yield format_sse(
"document_rewrite",
{
@@ -1379,6 +1591,7 @@ async def stream_full_report_rewrite(
"content": rewritten,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
yield format_sse(
@@ -1387,6 +1600,7 @@ async def stream_full_report_rewrite(
"type": "done",
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
return
@@ -148,6 +148,12 @@ class ModelTestRequest(BaseModel):
model: ModelUpsertRequest
class ModelOrderRequest(BaseModel):
"""The complete ordered set of model stable names."""
names: list[str] = Field(min_length=1, max_length=500)
class ModelTestResponse(BaseModel):
success: bool
latency_ms: int
@@ -378,6 +384,36 @@ async def create_model_configuration(body: ModelUpsertRequest, request: Request)
return _to_admin_item(created)
@router.put("/order", response_model=list[ModelAdminItem])
async def reorder_model_configurations(body: ModelOrderRequest, request: Request) -> list[ModelAdminItem]:
"""Persist the configured model order.
The first item is intentionally the deployment default: public model
selectors preserve the ``config.yaml`` order and use their first option
when no model was explicitly chosen. Requiring a complete permutation
prevents a stale browser from accidentally dropping a newly added model.
"""
await _require_admin(request)
if len(body.names) != len(set(body.names)):
raise HTTPException(status_code=422, detail="Each model may appear only once in the order")
async with _MUTATION_LOCK:
path = _resolve_config_path()
raw_before, _, models = await asyncio.to_thread(_read_config_data, path)
configured_names = [str(item.get("name") or "") for item in models]
if len(body.names) != len(configured_names) or set(body.names) != set(configured_names):
raise HTTPException(
status_code=409,
detail="The model list changed. Refresh the page and try again.",
)
models_by_name = {str(item.get("name")): item for item in models}
ordered_models = [models_by_name[name] for name in body.names]
await _save_models(request, raw_before, ordered_models)
return [_to_admin_item(item) for item in ordered_models]
@router.put("/{model_name:path}", response_model=ModelAdminItem)
async def update_model_configuration(model_name: str, body: ModelUpsertRequest, request: Request) -> ModelAdminItem:
await _require_admin(request)
@@ -591,6 +591,18 @@ def _normalize_compact_shortcut_buttons(
route_params: list[AppearanceRouteParam] = []
for param in item.route_params or []:
if param.paramType == "path":
source = (param.source or "access_token").strip() or "access_token"
route_params.append(
AppearanceRouteParam(
key="",
paramType="path",
source=source,
customValue=(param.customValue or "").strip() if source == "other" else None,
)
)
continue
key = (param.key or "").strip()
if not key:
continue
@@ -144,6 +144,7 @@ class DocumentRewriteRequest(BaseModel):
instruction: str = Field(..., min_length=1, max_length=4_000)
style: str | None = Field(default=None)
model_name: str | None = Field(default=None, alias="modelName")
report_outline: str | None = Field(default=None, alias="reportOutline", max_length=20_000)
generate_images: bool = Field(default=False, alias="generateImages")
max_generated_images: int = Field(default=2, alias="maxGeneratedImages", ge=1, le=4)
expected_hash: str | None = Field(default=None, alias="expectedHash")
@@ -172,6 +173,22 @@ def _document_rewrite_key(path: Path) -> str:
return os.path.normcase(str(path.resolve()))
def _instruction_with_report_outline(instruction: str, report_outline: str | None) -> str:
"""Make an optional report template an explicit constraint for the rewrite model."""
normalized_instruction = instruction.strip()
normalized_outline = (report_outline or "").strip()
if not normalized_outline:
return normalized_instruction
return "\n\n".join(
(
normalized_instruction,
"【报告结构要求】",
"请按以下 Markdown 章节结构重组全文;不适用的章节可以合并或省略,且不得为套用结构编造事实。",
normalized_outline,
)
)
async def _reserve_document_rewrite(path: Path) -> bool:
"""Reserve a physical document until its SSE generator reaches a terminal state."""
key = _document_rewrite_key(path)
@@ -213,6 +230,7 @@ def _artifact_job_response(job: dict) -> dict:
"instruction": str(snapshot.get("instruction") or ""),
"style": str(snapshot.get("style") or "professional"),
"modelName": snapshot.get("model_name"),
"reportOutline": str(snapshot.get("report_outline") or ""),
"generateImages": bool(snapshot.get("generate_images")),
"maxGeneratedImages": int(snapshot.get("max_generated_images") or 2),
"originalContent": snapshot.get("original_content") or "",
@@ -383,6 +401,8 @@ async def start_durable_document_rewrite_job(
if job_store is None or dispatcher is None:
raise HTTPException(status_code=503, detail="改写任务服务暂不可用。")
scope = _artifact_rewrite_scope(body.thread_id, requested_path)
report_outline = body.report_outline.strip() if body.report_outline else ""
instruction = _instruction_with_report_outline(body.instruction, report_outline)
snapshot = {
"kind": "artifact_document_rewrite",
"session_id": scope,
@@ -391,7 +411,8 @@ async def start_durable_document_rewrite_job(
"path": requested_path,
"original_content": original,
"base_hash": base_hash,
"instruction": body.instruction.strip(),
"instruction": instruction,
"report_outline": report_outline,
"style": body.style or "professional",
"model_name": body.model_name,
"generate_images": body.generate_images,
@@ -401,7 +422,8 @@ async def start_durable_document_rewrite_job(
json.dumps(
{
"base_hash": base_hash,
"instruction": body.instruction.strip(),
"instruction": instruction,
"report_outline": report_outline,
"style": body.style or "professional",
"model_name": body.model_name,
"generate_images": body.generate_images,
@@ -433,13 +455,20 @@ async def start_durable_document_rewrite_job(
@router.get("/document-rewrite/jobs/latest")
@require_permission("threads", "read", owner_check=True)
@require_permission("threads", "read")
async def get_latest_durable_document_rewrite_job(
request: Request,
thread_id: str = Query(..., min_length=1),
path: str = Query(..., min_length=1),
) -> dict:
"""Hydrate the latest artifact rewrite after a page refresh."""
"""Hydrate the current user's latest artifact rewrite after a page refresh.
The durable-job store is already filtered by ``user_id`` below. Do not
additionally perform a thread-owner lookup here: a historical thread can
be displayed under a guest/SSO alias even though the current user has no
matching ``threads_meta`` owner row, and the redundant check would turn an
otherwise safe empty history into a 404.
"""
user_id = await get_current_user(request)
job_store = getattr(request.app.state, "deep_research_job_store", None)
if job_store is None:
@@ -454,14 +483,20 @@ async def get_latest_durable_document_rewrite_job(
@router.get("/document-rewrite/jobs")
@require_permission("threads", "read", owner_check=True)
@require_permission("threads", "read")
async def list_durable_document_rewrite_jobs(
request: Request,
thread_id: str = Query(..., min_length=1),
path: str = Query(..., min_length=1),
limit: int = Query(default=20, ge=1, le=20),
) -> dict:
"""Return recent persisted rewrite jobs for a single sandbox Markdown file."""
"""Return the current user's persisted rewrite jobs for one Markdown file.
``list_by_session(..., user_id=user_id)`` below is the access boundary for
this metadata-only recovery endpoint. It must return an empty list rather
than a thread-ownership 404 when a legacy thread's display login differs
from its persisted creator identity.
"""
user_id = await get_current_user(request)
job_store = getattr(request.app.state, "deep_research_job_store", None)
if job_store is None:
@@ -539,6 +574,8 @@ async def rewrite_document(
requested_path = body.path.strip()
user_id = await get_current_user(request)
version_store = getattr(request.app.state, "document_rewrite_version_store", None)
report_outline = body.report_outline.strip() if body.report_outline else ""
instruction = _instruction_with_report_outline(body.instruction, report_outline)
suffix = Path(requested_path).suffix.lower()
if suffix not in {".md", ".markdown"}:
raise HTTPException(status_code=400, detail="AI 全文改写仅支持 Markdown 文件。")
@@ -608,7 +645,7 @@ async def rewrite_document(
async for delta in _astream_rewrite_requirements(
model,
file_name=actual_path.name,
instruction=body.instruction.strip(),
instruction=instruction,
style_instruction=style_instruction,
original=original,
):
@@ -628,7 +665,7 @@ async def rewrite_document(
if not requirements_plan:
requirements_fallback = True
requirements_plan = _build_fallback_rewrite_requirements_plan(
instruction=body.instruction.strip(),
instruction=instruction,
style_instruction=style_instruction,
)
yield _sse(
@@ -640,7 +677,7 @@ async def rewrite_document(
HumanMessage(
content=(
f"文件名:{actual_path.name}\n"
f"改写要求:{body.instruction.strip()}\n"
f"改写要求:{instruction}\n"
f"风格要求:{style_instruction}\n\n"
"【已确认的改写执行计划开始】\n"
f"{requirements_plan}\n"
@@ -654,7 +691,7 @@ async def rewrite_document(
yield _sse(
"requirements_resolved",
{
"instruction": body.instruction.strip(),
"instruction": instruction,
"style": body.style or "professional",
"modelName": resolved_name or "system_default",
"plan": requirements_plan,
@@ -712,7 +749,7 @@ async def rewrite_document(
"comparison_ready",
build_document_rewrite_summary(
source_display_name=actual_path.name,
instruction=body.instruction.strip(),
instruction=instruction,
model_name=resolved_name or "system_default",
original=original,
rewritten=rewritten,
@@ -734,6 +771,7 @@ async def rewrite_document(
committed_hash = _content_hash(rewritten)
await asyncio.to_thread(_atomic_write_text, actual_path, rewritten)
version_id: str | None = None
version_snapshot_failed = False
if version_store is not None:
try:
version_id = f"drv_{uuid.uuid4().hex}"
@@ -746,40 +784,44 @@ async def rewrite_document(
original_content=original,
original_hash=base_hash,
committed_hash=committed_hash,
instruction=body.instruction.strip(),
instruction=instruction,
model_name=resolved_name,
)
except Exception:
# The file is never left changed without its promised undo
# snapshot. Roll back only when nobody touched it in the
# short persistence window.
current_after_write = await asyncio.to_thread(
actual_path.read_text,
encoding="utf-8",
)
if _content_hash(current_after_write) == committed_hash:
await asyncio.to_thread(_atomic_write_text, actual_path, original)
raise
try:
await version_store.prune_source(
user_id=user_id,
source_type="artifact",
source_id=body.thread_id,
source_path=requested_path,
)
except Exception: # noqa: BLE001
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
# A version snapshot enables undo but is not allowed to
# discard a successfully committed document on failure.
logger.exception(
"could not persist artifact rewrite version for %s; keeping rewritten file",
actual_path,
)
version_id = None
version_snapshot_failed = True
else:
try:
await version_store.prune_source(
user_id=user_id,
source_type="artifact",
source_id=body.thread_id,
source_path=requested_path,
)
except Exception: # noqa: BLE001
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
yield _sse(
"committed",
{
"content": rewritten,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
yield _sse(
"done",
{"committedHash": committed_hash, "versionId": version_id},
{
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
except asyncio.CancelledError:
raise