Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
fix(gradio): update async handling in tests to use await for consistency
myhlolibe08d8c8a445
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.pyindex a44dbc65..dea6d899 100644--- a/mineru/kit/gradio/app.py+++ b/mineru/kit/gradio/app.py@@ -7,12 +7,15 @@ import htmlimport jsonimport osimport time-from contextlib import aclosing+import uuid+from contextlib import suppressfrom pathlib import Pathfrom collections.abc import Callablefrom typing import Anyfrom urllib.parse import quote+from loguru import logger+from ...errors import MineruErrorfrom ...filetypes import (FLASH_ONLY_PARSE_EXTENSIONS,@@ -34,6 +37,7 @@ from .client import (V1ServerCapabilities,)from .epub_preview import register_epub_preview_resources+from .conversion import ConversionRun, SessionConversions, await_task_completion, run_sync_outputfrom .i18n import MESSAGES, localized_text, preview_placeholder, translationsfrom .page_range import effective_page_range as _effective_page_rangefrom .page_range import pdf_page_metadata, validate_max_pages@@ -45,9 +49,7 @@ from .status import (STATUS_PREPARING_REQUEST,STATUS_PROCESSING_OUTPUT,STATUS_QUEUED_LOCALLY,- StatusPanelState,status_html as _status_html,- stream_status_updates,)_DOWNLOAD_FORMATS: tuple[tuple[str, str], ...] = (@@ -391,8 +393,7 @@ def build_gradio_app())# 等待限制放在生成器内部,使其他会话也能立即显示本地排队状态。- conversion_slot = asyncio.Semaphore(1)- session_tasks: dict[str, set[asyncio.Task[tuple[Any, ...]]]] = {}+ conversions = SessionConversions()with gr.Blocks() as demo:gr.HTML(@@ -571,6 +572,12 @@ def build_gradio_app(artifact_state = gr.State(value=None)active_run_id = gr.Textbox(value="", visible=False)+ conversion_ticket = gr.Textbox(value="", visible=False)+ conversion_receipt = gr.Textbox(value="", visible=False)+ conversion_cancel = gr.Textbox(value="", visible=False)+ status_snapshot = gr.Textbox(value="", visible=False)+ status_poll = gr.Timer(value=1.0, active=False)+ api_conversion_button = gr.Button(visible=False)download_files = {name: gr.File(visible=False, interactive=False) for name, _label in _DOWNLOAD_FORMATS}download_requests = {name: gr.Textbox(value="", visible=False) for name, _label in _DOWNLOAD_FORMATS}download_receipts = {name: gr.Textbox(value="", visible=False) for name, _label in _DOWNLOAD_FORMATS}@@ -693,18 +700,6 @@ def build_gradio_app(arguments = ", ".join(json.dumps(value) for value in (action, _DOWNLOAD_FORMATS, format_name, label))return f"(...args) => ({download_script})({arguments}, ...args)"- def reset_download_ui() -> tuple[Any, ...]:- """立即显示准备阶段并重置下载组件,为转换提供可靠串联的完成事件。"""- count = len(_DOWNLOAD_FORMATS)- return (- _status_html(STATUS_PREPARING_REQUEST),- "",- *((None,) * count),- *(("",) * count * 2),- *_download_updates(gr, interactive=False)[1:],- "",- )-download_reset_outputs = [active_run_id,*download_files.values(),@@ -740,15 +735,14 @@ def build_gradio_app()async def cancel_session_conversion(request: object | None = None) -> None:- """主动回收当前会话任务,确保关闭异步生成器时也能完成资源清理。"""- session_hash = getattr(request, "session_hash", None)- tasks = session_tasks.pop(session_hash, set())- for task in tasks:- task.cancel()- if tasks:- await asyncio.gather(*tasks, return_exceptions=True)+ """取消会话任务,并等待同步输出退出后回收后台工作。"""+ task = conversions.cancel(getattr(request, "session_hash", "") or "")+ if task is not None:+ with suppress(asyncio.CancelledError, Exception):+ await await_task_completion(task)cancel_session_conversion.__annotations__["request"] = gr.Request+# 显式注册幂等加载事件,初始化前端状态和自定义文案。demo.load(fn=None, js=app_js, **private_event_kwargs)preview_outputs = [@@ -806,15 +800,15 @@ def build_gradio_app(**private_event_kwargs,)- async def convert_handler(+ async def execute_conversion(+ run: ConversionRun,file_path: str | None,tier_position: int | float,raw_page_range: str,- force_ocr: bool = False,- request: object | None = None,- ) -> Any:- """执行单文件 V1 解析并流式更新状态、HTML 和 Structured Content JSON。"""- # 开始或失败只重置结果与下载;预览内容、显隐和浏览位置保持不变。+ force_ocr: bool,+ request: object | None,+ ) -> tuple[Any, ...]:+ """执行一次转换,只返回完整终态;进度写入独立的会话快照。"""reset_result = (_status_html(_DEFAULT_STATUS),"",@@ -823,123 +817,125 @@ def build_gradio_app(*_download_updates(gr, interactive=False),"",)++ def failure(message: str) -> tuple[Any, ...]:+ """把输入或输出错误收敛成可轮询的失败终态。"""+ run.publish(f"Failed: {message}")+ return (run.state.render(), *reset_result[1:])+if not file_path:- yield reset_result- return- state = StatusPanelState()- state.append(STATUS_PREPARING_REQUEST)- yield (state.render(), *reset_result[1:])+ return failure("No input file")source_path = Path(file_path).resolve()if not source_path.is_file():- yield (_status_html("Failed: input file does not exist"), *reset_result[1:])- return+ return failure("input file does not exist")suffix = _file_suffix(source_path)if suffix not in PARSEABLE_EXTENSIONS:- yield (_status_html(f"Failed: unsupported file type '.{suffix}'"), *reset_result[1:])- return+ return failure(f"unsupported file type '.{suffix}'")try:selected_tier = _tier_for_position(tier_position, tier_choices)except ValueError as exc:- yield (_status_html(f"Failed: {exc}"), *reset_result[1:])- return+ return failure(str(exc))if suffix in FLASH_ONLY_PARSE_EXTENSIONS:- # 提交端独立约束有效档位,避免事件 API 或前端残留值绕过 Flash 锁定。if "flash" not in tier_choices:- message = "Failed: tier_unavailable: 该格式仅支持 Flash,当前服务不可用"- yield (_status_html(message), *reset_result[1:])- return+ return failure("tier_unavailable: 该格式仅支持 Flash,当前服务不可用")selected_tier = "flash"- status_queue: asyncio.Queue[tuple[str, float]] = asyncio.Queue()loop = asyncio.get_running_loop()def emit(message: str) -> None:- """记录通知时刻,避免队列消费延迟被误算为服务端解析耗时。"""- loop.call_soon_threadsafe(status_queue.put_nowait, (message, time.monotonic()))+ """在通知发生时记录时间,再由事件循环串行更新会话状态。"""+ at = time.monotonic()+ loop.call_soon_threadsafe(lambda: run.publish(message, at=at))async def run_conversion() -> tuple[Any, ...]:- """在单任务槽内解析和整理结果;取消或失败均自动释放等待位置。"""- if conversion_slot.locked():- emit(STATUS_QUEUED_LOCALLY)- async with conversion_slot:- emit(STATUS_PREPARING_REQUEST)- page_text = await asyncio.to_thread(_effective_page_range, source_path, raw_page_range, max_pages=max_pages)+ """实际同步工作退出前一直持有执行槽,避免取消后与新任务重叠。"""+ if conversions.slot.locked():+ run.publish(STATUS_QUEUED_LOCALLY)+ async with conversions.slot:+ run.publish(STATUS_PREPARING_REQUEST)+ page_text = await run_sync_output(_effective_page_range, source_path, raw_page_range, max_pages=max_pages)result = await client.parse_file(source_path,tier=selected_tier,page_range=page_text,- # 再次检查源文件类型,避免事件 API 或隐藏控件残留值强制处理非 PDF。ocr_mode="ocr" if suffix in PDF_EXTENSIONS and force_ocr else "auto",status_callback=emit,)- emit(STATUS_PROCESSING_OUTPUT)- artifacts = await asyncio.to_thread(- persist_parse_result,- result,- source_path,- output_root=output_root,- page_range=page_text,+ # 先消费同一轮已发出的解析通知,保持阶段和解析耗时的顺序。+ await asyncio.sleep(0)+ run.publish(STATUS_PROCESSING_OUTPUT)+ output_started = time.monotonic()+ artifacts = await run_sync_output(+ persist_parse_result, result, source_path, output_root=output_root, page_range=page_text)- rendered_html = await asyncio.to_thread(- render_html_preview,- artifacts,- public_base_url=_gradio_public_base_url(request),+ rendered_html = await run_sync_output(+ render_html_preview, artifacts, public_base_url=_gradio_public_base_url(request))- # 直接复用下载所用的 Structured Content 文件,避免展示成 Middle JSON 或重复渲染。- structured_json = await asyncio.to_thread(artifacts.structured_content_path.read_text, encoding="utf-8")+ structured_json = await run_sync_output(artifacts.structured_content_path.read_text, encoding="utf-8")preview_path = artifacts.layout_pdf_path or artifacts.origin_pdf_pathgeneric_html = "" if preview_path else preview_placeholder("result_ready")show_image_preview = suffix in IMAGE_EXTENSIONS and preview_path is Noneresult_preview_updates = (_pdf_preview_update(gr, str(preview_path) if preview_path else None),- gr.update(value=str(source_path) if show_image_preview else None, visible=show_image_preview),+ gr.update(value=str(artifacts.source_path) if show_image_preview else None, visible=show_image_preview),gr.update(value="", visible=False),gr.update(value=generic_html, visible=bool(generic_html)),)if _is_office(source_path) or suffix in {"ofd", "epub"} or suffix in HTML_EXTENSIONS | MHTML_EXTENSIONS:- # Office/OFD/EPUB/HTML 源预览已在上传时挂载,成功后保留原内容和浏览位置。result_preview_updates = tuple(gr.skip() for _ in range(4))+ run.artifacts = artifacts.as_state()+ logger.info(+ "WebUI output ready run_id={} artifacts={} elapsed={:.3f}s html_bytes={} json_bytes={} ready_at={:.3f}",+ run.run_id,+ artifacts.root.name,+ time.monotonic() - output_started,+ len(rendered_html.encode("utf-8")),+ len(structured_json.encode("utf-8")),+ time.time(),+ )return (rendered_html,*result_preview_updates,- artifacts.as_state(),+ run.artifacts,*_download_updates(gr, interactive=True, run_id=artifacts.root.name),structured_json,)task = asyncio.create_task(run_conversion())- session_hash = getattr(request, "session_hash", None)- if session_hash:- session_tasks.setdefault(session_hash, set()).add(task)-- def forget_task(done_task: asyncio.Task[tuple[Any, ...]]) -> None:- """任务结束即释放会话索引,即使前端已丢弃生成器也不保留任务引用。"""- tasks = session_tasks.get(session_hash)- if tasks is not None:- tasks.discard(done_task)- if not tasks:- session_tasks.pop(session_hash, None)-- task.add_done_callback(forget_task)+ run.task = tasktry:- async with aclosing(stream_status_updates(task, status_queue, state)) as updates:- async for status in updates:- # 动画只更新状态卡片,避免反复重建预览和清空结果组件。- yield (status, *(gr.skip() for _ in reset_result[1:]))- result_outputs = await task- state.append(STATUS_COMPLETED)- yield (state.render(), *result_outputs)+ result_outputs = await asyncio.shield(task)+ if run.cancelled:+ return tuple(gr.skip() for _ in reset_result)+ run.publish(STATUS_COMPLETED)+ return (run.state.render(), *result_outputs)except asyncio.CancelledError:- # 会话重置后静默结束旧流,避免把取消异常或旧状态写回新界面。- return+ run.cancel()+ with suppress(asyncio.CancelledError, Exception):+ await await_task_completion(task)+ return tuple(gr.skip() for _ in reset_result)except Exception as exc:+ logger.exception("WebUI conversion failed run_id={}", run.run_id)message = f"{exc.code}: {exc}" if isinstance(exc, MineruError) else str(exc)- state.append(f"Failed: {message}")- yield (state.render(), *reset_result[1:])+ return failure(message)finally:- # 清除、换文件或断开流时仅取消本地等待,不发送远端取消请求。- if not task.done():- task.cancel()- await asyncio.gather(task, return_exceptions=True)+ run.task = None++ async def convert_handler(+ file_path: str | None,+ tier_position: int | float,+ raw_page_range: str,+ force_ocr: bool = False,+ request: object | None = None,+ ) -> tuple[Any, ...]:+ """保留公开转换 API 的四个输入和原生组件输出,普通响应一次返回结果。"""+ session = getattr(request, "session_hash", None) or uuid.uuid4().hex+ run = conversions.start(session, uuid.uuid4().hex)+ assert run is not None+ try:+ return await execute_conversion(run, file_path, tier_position, raw_page_range, force_ocr, request)+ finally:+ if not getattr(request, "session_hash", None):+ conversions.cancel(session, run.run_id)convert_outputs = [status_panel,@@ -961,28 +957,175 @@ def build_gradio_app("concurrency_limit": None,"api_visibility": "public" if enable_api else "private",}- # 重置与转换必须显式串联;独立 click 监听可能在转换完成后才执行重置。- begin_conversion = convert_button.click(- fn=reset_download_ui,+ # 公开 API 仍由 Gradio 原生组件序列化,浏览器通过私有回执校验后才应用结果。+ convert_event = api_conversion_button.click(+ fn=convert_handler,+ inputs=[input_file, tier, page_range, force_ocr],+ outputs=convert_outputs,+ **event_kwargs,+ )+ conversion_script = _resource_text("gradio_conversion.js")++ def conversion_js(action: str) -> str:+ """绑定自有任务协调脚本,不访问 Gradio 的内部状态或差分缓存。"""+ return f"(...args) => ({conversion_script})({json.dumps(action)}, ...args)"++ def read_conversion_status(ticket: str, request: object | None = None) -> str:+ """普通请求返回完整快照;没有有效任务时不创建新的轮询状态。"""+ try:+ run_id = json.loads(ticket)["run_id"]+ except (ValueError, TypeError, KeyError):+ return ""+ run = conversions.current(getattr(request, "session_hash", "") or "", run_id)+ return run.snapshot if run is not None else ""++ async def cancel_ui_conversion(ticket: str, request: object | None = None) -> None:+ """只取消浏览器刚刚撤销的那次任务,避免晚到请求误伤新任务。"""+ try:+ identity = json.loads(ticket)+ run_id = uuid.UUID(identity["run_id"]).hex+ revision = identity["revision"]+ if type(revision) is not int or revision <= 0:+ return+ except (ValueError, TypeError, KeyError, AttributeError):+ return+ session = getattr(request, "session_hash", "") or ""+ # 取消可能先于队列中的提交到达,记住修订号以拒绝尚未开始的旧请求。+ conversions.revisions[session] = max(conversions.revisions.get(session, 0), revision)+ task = conversions.cancel(session, run_id)+ if task is not None:+ with suppress(asyncio.CancelledError, Exception):+ await await_task_completion(task)++ async def unload_session(request: object | None = None) -> None:+ """标签页关闭后释放会话快照,并等待剩余同步任务退出。"""+ await cancel_session_conversion(request)+ conversions.revisions.pop(getattr(request, "session_hash", "") or "", None)++ async def convert_ui(+ file_path: str | None,+ tier_position: int | float,+ raw_page_range: str,+ force_ocr: bool,+ ticket: str,+ request: object | None = None,+ ) -> str:+ """用完整回执传递浏览器结果,使迟到响应无法直接覆盖可见组件。"""+ from gradio.data_classes import FileData++ try:+ identity = json.loads(ticket)+ run_id = uuid.UUID(identity["run_id"]).hex+ revision = identity["revision"]+ if type(revision) is not int or revision <= 0:+ return ""+ except (ValueError, TypeError, KeyError, AttributeError):+ return ""+ session = getattr(request, "session_hash", "") or ""+ run = conversions.start(session, run_id, revision=revision)+ if run is None:+ return ""+ result = await execute_conversion(run, file_path, tier_position, raw_page_range, force_ocr, request)+ if conversions.current(session, run_id) is not run:+ return ""+ values = list(result)+ # 文件来自输出根目录的不可变任务目录,使用现有 allowed_paths 文件路由。+ for index in (2, 3):+ update = values[index]+ if isinstance(update, dict) and update.get("value"):+ path = Path(update["value"]).resolve()+ path.relative_to(output_root.resolve())+ file_url = f"{_gradio_public_base_url(request)}/gradio_api/file={quote(path.as_posix(), safe='/')}"+ values[index] = {+ **update,+ "value": FileData(path=str(path), url=file_url, orig_name=path.name).model_dump(mode="json"),+ }+ return json.dumps(+ {+ "run_id": run_id,+ "sequence": run.state.phase_id,+ "ready_at": time.time(),+ "outputs": values[:6] + values[7:],+ },+ ensure_ascii=False,+ )++ for callback in (read_conversion_status, cancel_ui_conversion, unload_session, convert_ui):+ callback.__annotations__["request"] = gr.Request+ # 纯前端开始事件通过票据 change 发起转换,兼容旧版 Gradio 的 then 限制。+ convert_button.click(+ fn=None,inputs=[],- outputs=begin_conversion_outputs,+ outputs=[*begin_conversion_outputs, conversion_ticket, status_poll, html_output, json_output],js=(f"() => {{ ({pdf_preview_js('begin')})(); "- f"return [{json.dumps(_status_html(STATUS_PREPARING_REQUEST))}, ...({download_js('reset')})()]; }}"+ f"const ticket = ({conversion_js('begin')})(); "+ f"return [{json.dumps(_status_html(STATUS_PREPARING_REQUEST))}, "+ f"...({download_js('reset')})(), ...ticket, '', '']; }}"),**private_event_kwargs,)- convert_event = begin_conversion.then(- fn=convert_handler,- inputs=[input_file, tier, page_range, force_ocr],- outputs=convert_outputs,- **event_kwargs,+ ui_convert_event = conversion_ticket.change(+ fn=convert_ui,+ inputs=[input_file, tier, page_range, force_ocr, conversion_ticket],+ outputs=conversion_receipt,+ queue=True,+ concurrency_limit=None,+ trigger_mode="multiple",+ show_progress="hidden",+ api_visibility="private",+ )+ status_poll.tick(+ fn=read_conversion_status,+ inputs=conversion_ticket,+ outputs=status_snapshot,+ trigger_mode="always_last",+ show_progress="hidden",+ **private_event_kwargs,)+ status_snapshot.change(+ fn=None,+ inputs=status_snapshot,+ outputs=[status_panel, status_poll],+ js=conversion_js("status"),+ **private_event_kwargs,+ )+ ui_result_outputs = [*convert_outputs[:6], *convert_outputs[7:], status_poll]+ conversion_receipt.change(+ fn=None,+ inputs=[conversion_receipt, input_file, pdf_viewer_entry],+ outputs=[*ui_result_outputs, pdf_viewer],+ js=(+ "(receipt, source, viewer) => { "+ f"const values = ({conversion_js('result')})(receipt); "+ "const pdf = values[2]; "+ f"const preview = pdf?.__type__ === 'update' && 'value' in pdf ? ({pdf_preview_js('result')})("+ "pdf.value, source, viewer, values[6]) : {__type__: 'update'}; "+ "return [...values, preview]; }"+ ),+ **private_event_kwargs,+ )+ gr.on(+ triggers=[input_file.change, clear_button.click],+ fn=None,+ inputs=[],+ outputs=[conversion_ticket, conversion_cancel, status_poll],+ js=conversion_js("cancel"),+ **private_event_kwargs,+ )+ conversion_cancel.change(+ fn=cancel_ui_conversion,+ inputs=conversion_cancel,+ outputs=[],+ trigger_mode="multiple",+ **private_event_kwargs,+ )+ demo.unload(unload_session)source_preview_event = input_file.change(fn=update_file_preview,inputs=input_file,outputs=preview_outputs,- cancels=[convert_event],+ cancels=[convert_event, ui_convert_event],# 示例切换也会触发文件变更,预览准备期间保持状态卡片可见。show_progress="hidden",**private_event_kwargs,@@ -1002,7 +1145,6 @@ def build_gradio_app(js=pdf_preview_js("result"),**private_event_kwargs,)- input_file.change(fn=cancel_session_conversion, inputs=[], outputs=[], **private_event_kwargs)reset_outputs = [status_panel,@@ -1031,8 +1173,22 @@ def build_gradio_app("",)- clear_button.click(fn=reset_ui, inputs=[], outputs=reset_outputs, cancels=[convert_event], **private_event_kwargs)- clear_button.click(fn=cancel_session_conversion, inputs=[], outputs=[], **private_event_kwargs)+ clear_button.click(+ fn=reset_ui, inputs=[], outputs=reset_outputs, cancels=[convert_event, ui_convert_event], **private_event_kwargs+ )++ def session_download_handler(format_name: str) -> Callable[..., tuple[Any, str]]:+ """下载时读取当前会话的素材路径,公开 API 的 State 仍可单独使用。"""+ renderer = _download_handler(format_name, output_root)++ def handler(state: dict[str, Any] | None, token: str, request: object | None = None) -> tuple[Any, str]:+ """使用服务端当前任务核对下载标识,拒绝过期下载请求。"""+ run = conversions.runs.get(getattr(request, "session_hash", "") or "")+ if run is not None:+ state = run.artifacts if not run.cancelled else None+ return renderer(state, token, request)++ return handlerfor format_name, label in _DOWNLOAD_FORMATS:begin_download = download_buttons[format_name].click(@@ -1050,7 +1206,7 @@ def build_gradio_app(js=download_js("busy", format_name, label),**private_event_kwargs,)- download_handler = _download_handler(format_name, output_root)+ download_handler = session_download_handler(format_name)download_handler.__annotations__["request"] = gr.Requestprepare_download = begin_download.then(fn=download_handler,diff --git a/mineru/kit/gradio/artifacts.py b/mineru/kit/gradio/artifacts.pyindex c4d0dfef..1f6d519d 100644--- a/mineru/kit/gradio/artifacts.py+++ b/mineru/kit/gradio/artifacts.py@@ -6,8 +6,11 @@ import htmlimport jsonimport reimport shutil+import timeimport uuidimport zipfile+from collections.abc import Iterator+from contextlib import contextmanagerfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom tempfile import TemporaryDirectory@@ -40,6 +43,18 @@ from ...types import BlockBase, ImagePayloadBlock, MiddleJsonDownloadFormat = Literal["markdown", "json", "html", "docx", "latex", "epub", "pdf"]+@contextmanager+def _output_stage(stage: str, artifacts: RunArtifacts) -> Iterator[None]:+ """按素材目录标识记录输出子阶段耗时,便于与转换及浏览器回执关联。"""+ started = time.monotonic()+ try:+ yield+ finally:+ logger.info(+ "WebUI output stage={} artifacts={} elapsed={:.3f}s", stage, artifacts.root.name, time.monotonic() - started+ )++@dataclassclass RunArtifacts:"""保存一次 Gradio 解析所产生的稳定文件路径。"""@@ -211,11 +226,12 @@ def persist_parse_result(encoding="utf-8",)- origin_pdf, page_indices = _prepare_origin_pdf(- artifacts.source_path,- page_range=page_range,- output_path=artifacts.root / "origin.pdf",- )+ with _output_stage("origin_pdf", artifacts):+ origin_pdf, page_indices = _prepare_origin_pdf(+ artifacts.source_path,+ page_range=page_range,+ output_path=artifacts.root / "origin.pdf",+ )artifacts.origin_pdf_path = origin_pdfartifacts.page_indices = tuple(page_indices)@@ -227,29 +243,32 @@ def persist_parse_result(asset_root=source_path.parent,)try:- materialized = _materialize_middle_json(result.middle_json, image_context)- artifacts.middle_json_path.write_text(ParseResult(middle_json=materialized).to_json(), encoding="utf-8")- markdown = cast(- str,- render(- materialized,- RenderFormat.MARKDOWN,- options=MarkdownRenderOptions(),- ),- )- artifacts.markdown_path.write_text(markdown, encoding="utf-8")- structured = cast(- dict[str, Any],- render(- materialized,- RenderFormat.STRUCTURED_CONTENT,- options=StructuredContentRenderOptions(),- ),- )- artifacts.structured_content_path.write_text(- json.dumps(structured, ensure_ascii=False, indent=2),- encoding="utf-8",- )+ with _output_stage("materialize", artifacts):+ materialized = _materialize_middle_json(result.middle_json, image_context)+ artifacts.middle_json_path.write_text(ParseResult(middle_json=materialized).to_json(), encoding="utf-8")+ with _output_stage("markdown", artifacts):+ markdown = cast(+ str,+ render(+ materialized,+ RenderFormat.MARKDOWN,+ options=MarkdownRenderOptions(),+ ),+ )+ artifacts.markdown_path.write_text(markdown, encoding="utf-8")+ with _output_stage("structured_json", artifacts):+ structured = cast(+ dict[str, Any],+ render(+ materialized,+ RenderFormat.STRUCTURED_CONTENT,+ options=StructuredContentRenderOptions(),+ ),+ )+ artifacts.structured_content_path.write_text(+ json.dumps(structured, ensure_ascii=False, indent=2),+ encoding="utf-8",+ )finally:_close_image_context(image_context)@@ -258,12 +277,13 @@ def persist_parse_result(layout_path = artifacts.root / "layout.pdf"try:- draw_layout_overlay(- result.middle_json,- origin_pdf,- layout_path,- page_indices=artifacts.page_indices,- )+ with _output_stage("layout_pdf", artifacts):+ draw_layout_overlay(+ result.middle_json,+ origin_pdf,+ layout_path,+ page_indices=artifacts.page_indices,+ )except Exception as exc:logger.warning("Skipping Gradio layout overlay for {}: {}", artifacts.stem, exc)else:@@ -274,15 +294,16 @@ def persist_parse_result(def render_html_preview(artifacts: RunArtifacts, *, public_base_url: str) -> str:"""用独立 iframe 展示公共 HTML renderer 的完整输出,保留样式及公式脚本。"""- value = _render_html(artifacts, public_base_url=public_base_url)- # srcdoc 默认继承外层页面的基准地址,显式指定自身才能让章节锚点在预览内跳转。- value = value.replace("<head>", '<head>\n<base href="about:srcdoc">', 1)- value = _prepare_preview_links(value)- return (- '<iframe class="mineru-rendered-html-frame" title="Markdown preview" '- 'sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox" '- f'srcdoc="{html.escape(value, quote=True)}"></iframe>'- )+ with _output_stage("html_preview", artifacts):+ value = _render_html(artifacts, public_base_url=public_base_url)+ # srcdoc 默认继承外层页面的基准地址,显式指定自身才能让章节锚点在预览内跳转。+ value = value.replace("<head>", '<head>\n<base href="about:srcdoc">', 1)+ value = _prepare_preview_links(value)+ return (+ '<iframe class="mineru-rendered-html-frame" title="Markdown preview" '+ 'sandbox="allow-scripts allow-popups allow-popups-to-escape-sandbox" '+ f'srcdoc="{html.escape(value, quote=True)}"></iframe>'+ )def _prepare_preview_links(document: str) -> str:diff --git a/mineru/kit/gradio/conversion.py b/mineru/kit/gradio/conversion.pynew file mode 100644index 00000000..108b4cb4--- /dev/null+++ b/mineru/kit/gradio/conversion.py@@ -0,0 +1,116 @@+"""WebUI 会话任务、完整状态快照和同步输出工作的取消边界。"""++from __future__ import annotations++import asyncio+import json+from collections.abc import Callable+from dataclasses import dataclass, field+from typing import Any, ParamSpec, TypeVar++from .status import STATUS_COMPLETED, STATUS_PREPARING_REQUEST, StatusPanelState++P = ParamSpec("P")+T = TypeVar("T")+++@dataclass+class ConversionRun:+ """只保留当前任务的状态和素材路径,不在会话缓存中持有解析结果。"""++ run_id: str+ state: StatusPanelState = field(init=False)+ snapshot: str = ""+ cancelled: bool = False+ terminal: bool = False+ task: asyncio.Task[tuple[Any, ...]] | None = None+ artifacts: dict[str, Any] | None = None++ def __post_init__(self) -> None:+ """使用浏览器或 API 分配的唯一标识初始化准备阶段。"""+ self.state = StatusPanelState(run_id=self.run_id)+ self.publish(STATUS_PREPARING_REQUEST)++ def publish(self, message: str, *, at: float | None = None) -> None:+ """仅真实阶段变化生成新快照,终态拒绝迟到的后台通知。"""+ if self.cancelled or self.terminal or not self.state.append(message, at=at):+ return+ self.terminal = message == STATUS_COMPLETED or message.startswith("Failed:")+ self.snapshot = json.dumps(+ {+ "run_id": self.run_id,+ "sequence": self.state.phase_id,+ "terminal": self.terminal,+ "html": self.state.render(),+ },+ ensure_ascii=False,+ )++ def cancel(self) -> asyncio.Task[tuple[Any, ...]] | None:+ """立即失效界面回执,实际任务在同步工作退出后释放执行槽。"""+ self.cancelled = True+ self.artifacts = None+ if self.task is not None and not self.task.done():+ self.task.cancel()+ return self.task+++class SessionConversions:+ """按 Gradio 会话隔离任务,并让所有实际解析共享一个执行槽。"""++ def __init__(self) -> None:+ """在构建 WebUI 时创建注册表,不在模块导入期间分配运行资源。"""+ self.runs: dict[str, ConversionRun] = {}+ self.revisions: dict[str, int] = {}+ self.slot = asyncio.Semaphore(1)++ def start(self, session: str, run_id: str, *, revision: int | None = None) -> ConversionRun | None:+ """拒绝晚到的旧提交,替换会话任务时先取消其后台工作。"""+ if revision is not None:+ if revision <= self.revisions.get(session, 0):+ return None+ self.revisions[session] = revision+ previous = self.runs.get(session)+ if previous is not None:+ previous.cancel()+ run = ConversionRun(run_id)+ self.runs[session] = run+ return run++ def current(self, session: str, run_id: str) -> ConversionRun | None:+ """只能读取当前会话仍有效的指定任务,避免跨任务状态混入。"""+ run = self.runs.get(session)+ return run if run is not None and run.run_id == run_id and not run.cancelled else None++ def cancel(self, session: str, run_id: str | None = None) -> asyncio.Task[tuple[Any, ...]] | None:+ """按回执身份取消任务,迟到的文件切换请求不能取消新任务。"""+ run = self.runs.get(session)+ if run is None or (run_id is not None and run.run_id != run_id):+ return None+ self.runs.pop(session, None)+ return run.cancel()+++async def await_task_completion(task: asyncio.Task[T]) -> T:+ """收到取消后仍等待实际工作退出,再传播取消以保持资源和槽位有效。"""+ cancelled = False+ while not task.done():+ try:+ await asyncio.shield(task)+ except asyncio.CancelledError:+ cancelled = True+ except Exception:+ break+ if cancelled:+ if not task.cancelled():+ task.exception()+ raise asyncio.CancelledError+ return task.result()+++async def run_sync_output(function: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:+ """同步输出线程不可强制取消,必须等它退出后才能释放外层执行槽。"""+ return await await_task_completion(asyncio.create_task(asyncio.to_thread(function, *args, **kwargs)))+++__all__ = ["ConversionRun", "SessionConversions", "await_task_completion", "run_sync_output"]diff --git a/mineru/kit/gradio/status.py b/mineru/kit/gradio/status.pyindex 3c10f01e..2bd6ebdd 100644--- a/mineru/kit/gradio/status.py+++ b/mineru/kit/gradio/status.py@@ -1,14 +1,12 @@-"""Gradio 步骤卡片、单次任务计时与流式状态等待。"""+"""Gradio 步骤卡片与单次任务的真实阶段计时。"""from __future__ import annotations-import asyncioimport timeimport uuid-from collections.abc import AsyncIterator, Callable+from collections.abc import Callablefrom dataclasses import dataclass, fieldfrom decimal import ROUND_HALF_UP, Decimal-from typing import Anyfrom .i18n import localized_message, localized_text as _localized_text@@ -63,17 +61,13 @@ class StatusPanelState:self.processing_elapsed = 0.0self.message = messageself.phase_id += 1+ self.sequence += 1# 本地排队结束后可以重新准备请求,高亮必须与实际阶段一致。self.step_index = _MESSAGE_STEPS.get(message, self.step_index)if message.startswith("Failed:"):self.step_index = len(_STEPS) - 1return True- def snapshot(self) -> str:- """为周期同步生成新序号,确保静态阶段也能重新应用到浏览器。"""- self.sequence += 1- return self.render()-def render(self) -> str:"""按 3.4.5 的两列卡片结构渲染当前状态,并转义外部错误文本。"""now = self.clock()@@ -131,44 +125,6 @@ def status_html(message: str = DEFAULT_STATUS) -> str:return state.render()-async def stream_status_updates(- task: asyncio.Task[Any],- events: asyncio.Queue[tuple[str, float]],- state: StatusPanelState,-) -> AsyncIterator[str]:- """阶段变化立即发送,长任务每秒同步快照;浏览器独立绘制动画。"""- loop = asyncio.get_running_loop()- next_refresh = loop.time() + 1.0- while True:- while not events.empty():- message, at = events.get_nowait()- if state.append(message, at=at):- next_refresh = loop.time() + 1.0- yield state.snapshot()- if task.done():- return- waiter = asyncio.create_task(events.get())- updated_html: str | None = None- try:- done, _ = await asyncio.wait(- {task, waiter}, timeout=max(0.0, next_refresh - loop.time()), return_when=asyncio.FIRST_COMPLETED- )- if waiter in done:- message, at = waiter.result()- if state.append(message, at=at):- updated_html = state.snapshot()- if updated_html is None and not task.done() and loop.time() >= next_refresh:- updated_html = state.snapshot()- finally:- if not waiter.done():- waiter.cancel()- await asyncio.gather(waiter, return_exceptions=True)- # 向 Gradio 交回控制权前已回收临时 waiter,避免暂停在 yield 时残留后台等待。- if updated_html is not None:- next_refresh = loop.time() + 1.0- yield updated_html--__all__ = ["DEFAULT_STATUS","STATUS_CHECKING_SERVER",@@ -182,5 +138,4 @@ __all__ = ["STATUS_SUBMITTING_TASK","StatusPanelState","status_html",- "stream_status_updates",]diff --git a/mineru/resources/gradio_conversion.js b/mineru/resources/gradio_conversion.jsnew file mode 100644index 00000000..54744099--- /dev/null+++ b/mineru/resources/gradio_conversion.js@@ -0,0 +1,72 @@+// 普通响应携带完整快照;任务身份与序号检查先于任何可见组件更新。+(action, ...args) => {+ const state = window.__mineruConversion ??= { revision: 0, runId: "", sequence: 0, terminal: false, applied: false };+ // 使用 Gradio 的空更新保留当前组件,且本脚本的响应不经过生成器差分。+ const skip = () => ({ __type__: "update" });+ const timer = (active) => ({ __type__: "update", active });+ // 容忍已清除的空回执,格式异常也不能破坏当前任务。+ const parse = (value) => {+ try { return JSON.parse(value || "null"); } catch { return null; }+ };+ // 每个任务最多保留一个预览加载监听器,换文件或重新提交立即回收。+ const stopPreviewLog = () => {+ if (state.previewLoad) document.removeEventListener("load", state.previewLoad, true);+ state.previewLoad = null;+ };++ if (action === "begin") {+ stopPreviewLog();+ state.runId = crypto.randomUUID().replaceAll("-", "");+ state.revision += 1;+ state.sequence = 0;+ state.terminal = false;+ state.applied = false;+ window.__mineruStatusPanel?.showPreparing();+ return [JSON.stringify({ run_id: state.runId, revision: state.revision }), timer(true)];+ }+ if (action === "cancel") {+ stopPreviewLog();+ const previous = state.runId ? JSON.stringify({ run_id: state.runId, revision: state.revision }) : "";+ state.runId = "";+ state.terminal = true;+ state.applied = false;+ return ["", previous, timer(false)];+ }+ if (action === "status") {+ const snapshot = parse(args[0]);+ if (!snapshot || !state.runId || snapshot.run_id !== state.runId || state.terminal+ || !Number.isInteger(snapshot.sequence) || snapshot.sequence <= state.sequence) return [skip(), skip()];+ state.sequence = snapshot.sequence;+ state.terminal = Boolean(snapshot.terminal);+ return [snapshot.html, timer(!state.terminal)];+ }+ if (action === "result") {+ const receipt = parse(args[0]);+ // 返回 15 个可见/文件输出和 Timer;服务端的 artifact State 不经过浏览器写入。+ if (!receipt || !state.runId || receipt.run_id !== state.runId || state.applied+ || !Array.isArray(receipt.outputs) || receipt.outputs.length !== 15+ || !Number.isInteger(receipt.sequence) || receipt.sequence < state.sequence) {+ return Array.from({ length: 16 }, skip);+ }+ state.sequence = receipt.sequence;+ state.terminal = true;+ state.applied = true;+ console.info("[MinerU WebUI] result received", JSON.stringify({+ run_id: receipt.run_id, ready_at: receipt.ready_at, received_at: Date.now() / 1000,+ }));+ stopPreviewLog();+ // 在框架写入 iframe 之前捕获 load,避免快速 srcdoc 在下一动画帧前已经加载。+ if (receipt.outputs[1]) {+ state.previewLoad = (event) => {+ if (!event.target?.matches?.("iframe.mineru-rendered-html-frame")) return;+ stopPreviewLog();+ if (state.runId === receipt.run_id) console.info("[MinerU WebUI] preview loaded", JSON.stringify({+ run_id: receipt.run_id, loaded_at: Date.now() / 1000,+ }));+ };+ document.addEventListener("load", state.previewLoad, true);+ }+ return [...receipt.outputs, timer(false)];+ }+ return [];+}diff --git a/tests/unittest/test_kit_gradio.py b/tests/unittest/test_kit_gradio.pyindex 6b5228f9..65e387a8 100644--- a/tests/unittest/test_kit_gradio.py+++ b/tests/unittest/test_kit_gradio.py@@ -649,7 +649,7 @@ def test_gradio_ocr_reaches_analysis_through_real_v1_jobs(monkeypatch: pytest.Modemo = build_gradio_app(client, capabilities, output_root=tmp_path / "output", enable_example=False)handler = next(fn.fn for fn in demo.fns.values() if fn.name == "convert_handler")for enabled in (True, False, True):- updates = [update async for update in handler(str(source), 0, "", enabled)]+ updates = [await handler(str(source), 0, "", enabled)]state = updates[-1][6]assert state is not None, updates[-1][0]payload = json.loads(Path(state["middle_json_path"]).read_text())@@ -1101,7 +1101,7 @@ def test_gradio_non_pdf_ignores_hidden_force_ocr(tmp_path: Path, suffix: str) ->async def convert() -> None:"""消费完整转换事件,以等待参数进入 client。"""- updates = [update async for update in handler(str(source), 0, "1-3", True)]+ updates = [await handler(str(source), 0, "1-3", True)]assert "stop after request" in updates[-1][0]asyncio.run(convert())@@ -1232,7 +1232,7 @@ def test_gradio_conversion_enforces_file_type_tier(tmp_path: Path, extension: stasync def convert() -> None:"""模拟直接调用事件,并携带不应影响非 PDF 的选页与 OCR 残留值。"""- updates = [update async for update in handler(str(source), 1, "bad range", True)]+ updates = [await handler(str(source), 1, "bad range", True)]assert "stop after request" in updates[-1][0]asyncio.run(convert())@@ -1254,7 +1254,7 @@ def test_gradio_flash_only_input_requires_available_flash(tmp_path: Path, tiers:async def convert() -> list[tuple[Any, ...]]:"""收集合法位置的错误响应,验证不是滑杆越界触发的拒绝。"""- return [update async for update in handler(str(source), 0, "")]+ return [await handler(str(source), 0, "")]updates = asyncio.run(convert())assert "tier_unavailable" in updates[-1][0]@@ -1308,8 +1308,8 @@ def test_gradio_conversion_forwards_page_range_and_enables_fresh_downloads(return ParseResult(middle_json=_middle_json(with_image=False))async def collect_updates(handler: Any) -> list[tuple[Any, ...]]:- """收集 Gradio 异步生成器的全部增量输出。"""- return [update async for update in handler(str(source), position, " 1 ", force_ocr)]+ """收集 Gradio 普通转换请求的完整输出。"""+ return [await handler(str(source), position, " 1 ", force_ocr)]capabilities = V1ServerCapabilities("http://127.0.0.1:1", tiers, ("zip",), ("file_id",))client = FakeClient()@@ -1327,7 +1327,7 @@ def test_gradio_conversion_forwards_page_range_and_enables_fresh_downloads(assert updates[-1][7] == Path(updates[-1][6]["root"]).nameassert updates[-1][6] is not Noneassert all(update["interactive"] is True for update in updates[-1][8:15])- assert all(update["interactive"] is False for update in updates[0][8:15])+ assert len(updates) == 1def test_gradio_epub_conversion_preserves_source_preview(tmp_path: Path) -> None:@@ -1350,7 +1350,7 @@ def test_gradio_epub_conversion_preserves_source_preview(tmp_path: Path) -> Noneasync def collect() -> list[tuple[Any, ...]]:"""收集 EPUB 转换的状态和最终结果更新。"""- return [update async for update in handler(str(source), 0, "")]+ return [await handler(str(source), 0, "")]updates = asyncio.run(collect())final = updates[-1]@@ -1379,7 +1379,7 @@ def test_gradio_conversion_rejects_invalid_tier_position(tmp_path: Path, tiers:async def collect_updates() -> list[tuple[Any, ...]]:"""收集非法滑块位置对应的错误输出。"""- return [update async for update in handler(str(source), position, "")]+ return [await handler(str(source), position, "")]updates = asyncio.run(collect_updates())assert "Failed: Invalid tier slider position" in updates[-1][0]@@ -1401,8 +1401,8 @@ def test_gradio_conversion_failure_clears_previous_downloads(tmp_path: Path) ->raise V1ArtifactError("boom", code="parse_failed")async def final_update(handler: Any) -> tuple[Any, ...]:- """返回 Gradio 异步生成器的最后一次更新。"""- updates = [update async for update in handler(str(source), 0, "")]+ """返回 Gradio 普通转换请求的最终结果。"""+ updates = [await handler(str(source), 0, "")]return updates[-1]capabilities = V1ServerCapabilities("http://127.0.0.1:1", ("flash",), ("zip",), ("file_id",))@@ -1459,44 +1459,52 @@ def test_gradio_local_queue_cancellation_releases_slot_and_keeps_sessions_isolatassert len(cancellation_events) >= 2assert all(handler_fn._id in dependency["cancels"] for dependency in cancellation_events)- async def advance_until(stream: Any, marker: str) -> tuple[Any, ...]:- """推进生成器直到目标阶段,并给测试死锁设置明确超时。"""- while True:- update = await asyncio.wait_for(anext(stream), timeout=2)- if marker in update[0]:- return update-+ ui = next(fn.fn for fn in demo.fns.values() if fn.name == "convert_ui")+ poll = next(fn.fn for fn in demo.fns.values() if fn.name == "read_conversion_status")+ cancel_session = next(fn.fn for fn in demo.fns.values() if fn.name == "cancel_ui_conversion")requests = [SimpleNamespace(session_hash=f"session-{index}") for index in range(3)]+ tickets = [json.dumps({"run_id": f"{index + 1:032x}", "revision": 1}) for index in range(3)]++ async def advance_until(index: int, marker: str) -> dict[str, Any]:+ """轮询独立快照,不消费或阻塞转换请求。"""+ for _ in range(600):+ snapshot = json.loads(poll(tickets[index], requests[index]) or "{}")+ if marker in snapshot.get("html", ""):+ return snapshot+ await asyncio.sleep(0.005)+ raise AssertionError(f"未出现阶段:{marker}")+first, second, third = [- handler_fn.fn(str(source), 0, "", request=request) for source, request in zip(sources, requests)+ asyncio.create_task(ui(str(source), 0, "", False, ticket, request))+ for source, ticket, request in zip(sources, tickets, requests)]- cancel_session = next(fn.fn for fn in demo.fns.values() if fn.name == "cancel_session_conversion")- await advance_until(first, "Processing on server")- queued = await advance_until(second, "Queued locally")- assert all(value == {"__type__": "update"} for value in queued[1:])- assert calls == [sources[0]]- if explicit_session_cancel:- await cancel_session(requests[1])- with pytest.raises(StopAsyncIteration):- await anext(second)- else:- await second.aclose()- await advance_until(third, "Queued locally")- if explicit_session_cancel:- await cancel_session(requests[0])- with pytest.raises(StopAsyncIteration):- await anext(first)- else:- await first.aclose()- await advance_until(third, "Processing on server")- assert canceled == [sources[0]]- assert calls == [sources[0], sources[2]]- finish.set()- updates = [update async for update in third]- final = updates[-1]- assert "Completed (" in final[0]- assert final[6]["stem"] == sources[2].stem- assert all(item["interactive"] is True for item in final[8:15])+ try:+ await advance_until(0, "Processing on server")+ queued = await advance_until(1, "Queued locally")+ assert queued["terminal"] is False+ assert calls == [sources[0]]+ if explicit_session_cancel:+ await cancel_session(tickets[1], requests[1])+ else:+ second.cancel()+ assert await second == ""+ await advance_until(2, "Queued locally")+ if explicit_session_cancel:+ await cancel_session(tickets[0], requests[0])+ else:+ first.cancel()+ assert await first == ""+ await advance_until(2, "Processing on server")+ assert canceled == [sources[0]]+ assert calls == [sources[0], sources[2]]+ finish.set()+ final = json.loads(await third)["outputs"]+ assert "Completed (" in final[0]+ assert final[6].startswith(sources[2].stem)+ assert all(item["interactive"] is True for item in final[7:14])+ finally:+ finish.set()+ await asyncio.gather(first, second, third, return_exceptions=True)asyncio.run(scenario())@@ -1527,7 +1535,7 @@ def test_gradio_output_failure_stops_timer_and_allows_next_conversion(tmp_path:async def scenario() -> None:"""连续运行两次,验证异常分支没有泄漏执行槽。"""for _ in range(2):- updates = [update async for update in handler(str(source), 0, "")]+ updates = [await handler(str(source), 0, "")]assert "Failed: output disk unavailable" in updates[-1][0]assert "is-error" in updates[-1][0]assert updates[-1][6] is None@@ -1616,7 +1624,7 @@ def test_shared_layout_failure_keeps_other_gradio_artifacts(tmp_path: Path, monkassert "layout.pdf" not in archive.namelist()-def test_gradio_slow_precheck_streams_heartbeats_and_queues_other_sessions(+def test_gradio_slow_precheck_keeps_polling_available_and_queues_other_sessions(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:"""页码预检阻塞时仍同步状态,其他会话先显示排队而不是堵在预检前。"""@@ -1637,28 +1645,32 @@ def test_gradio_slow_precheck_streams_heartbeats_and_queues_other_sessions(client = SimpleNamespace(parse_file=AsyncMock(side_effect=V1ArtifactError("test finished")))capabilities = V1ServerCapabilities("http://127.0.0.1:1", ("flash",), ("zip",), ("file_id",))demo = build_gradio_app(client, capabilities, output_root=tmp_path / "output", enable_example=False)- handler = next(fn.fn for fn in demo.fns.values() if fn.name == "convert_handler")+ handler = next(fn.fn for fn in demo.fns.values() if fn.name == "convert_ui")+ poll = next(fn.fn for fn in demo.fns.values() if fn.name == "read_conversion_status")async def scenario() -> None:- """同时推进两个会话,验证预检时的心跳和执行槽等待。"""- first = handler(str(source), 0, "")- second = handler(str(source), 0, "")+ """预检线程阻塞时仍可独立查询准备和排队状态。"""+ requests = [SimpleNamespace(session_hash=f"s{i}") for i in range(2)]+ tickets = [json.dumps({"run_id": f"{i + 1:032x}", "revision": 1}) for i in range(2)]+ first, second = [+ asyncio.create_task(handler(str(source), 0, "", False, ticket, request))+ for ticket, request in zip(tickets, requests)+ ]try:- await anext(first)- heartbeat = await asyncio.wait_for(anext(first), 2)- assert started.is_set()- assert "Preparing request" in heartbeat[0]- assert 'data-mineru-status-seq="1"' in heartbeat[0]- assert all(value == {"__type__": "update"} for value in heartbeat[1:])- await anext(second)- queued = await asyncio.wait_for(anext(second), 0.5)- assert "Queued locally" in queued[0]+ for _ in range(600):+ if started.is_set() and "Queued locally" in poll(tickets[1], requests[1]):+ break+ await asyncio.sleep(0.005)+ assert started.is_set() and "Queued locally" in poll(tickets[1], requests[1])+ initial = poll(tickets[0], requests[0])+ assert "Preparing request" in initial+ await asyncio.sleep(0.03)+ assert poll(tickets[0], requests[0]) == initialrelease.set()- assert "test finished" in [item async for item in first][-1][0]- assert "test finished" in [item async for item in second][-1][0]+ assert "test finished" in (await first)+ assert "test finished" in (await second)finally:release.set()- await first.aclose()- await second.aclose()+ await asyncio.gather(first, second, return_exceptions=True)asyncio.run(scenario())diff --git a/tests/unittest/test_kit_gradio_conversion.cjs b/tests/unittest/test_kit_gradio_conversion.cjsnew file mode 100644index 00000000..5f25da6c--- /dev/null+++ b/tests/unittest/test_kit_gradio_conversion.cjs@@ -0,0 +1,56 @@+// 验证实际脚本处理乱序回执,不依赖 Gradio 私有实现。+const assert = require('node:assert/strict');+const fs = require('node:fs');+const vm = require('node:vm');+const path = require('node:path');+const crypto = require('node:crypto');+const window = {};+const listeners = new Set();+const logs = [];+const document = {+ addEventListener(type, fn, capture) { assert.equal(type, 'load'); assert.equal(capture, true); listeners.add(fn); },+ removeEventListener(type, fn, capture) { assert.equal(type, 'load'); assert.equal(capture, true); listeners.delete(fn); },+};+const invoke = vm.runInNewContext(`(${fs.readFileSync(path.join(__dirname, '../../mineru/resources/gradio_conversion.js'), 'utf8')})`, {+ window, crypto, console: { info(...args) { logs.push(args); } }, document,+});+// 跨 VM 对象通过 JSON 比较,忽略执行上下文的原型差异。+const equal = (a, b) => assert.equal(JSON.stringify(a), JSON.stringify(b));+const skipped = (values) => values.every(value => JSON.stringify(value) === '{"__type__":"update"}');+const status = (id, sequence, terminal = false) => JSON.stringify({ run_id: id, sequence, terminal, html: `stage-${sequence}` });+const result = (id, sequence, final = 'Completed') => JSON.stringify({ run_id: id, sequence, outputs: Array(15).fill(final) });+for (let index = 0; index < 150; index++) {+ const [ticket, timer] = invoke('begin');+ const first = JSON.parse(ticket);+ assert.equal(timer.active, true);+ equal(invoke('status', status(first.run_id, 1)), ['stage-1', { __type__: 'update', active: true }]);+ assert.ok(skipped(invoke('status', status(first.run_id, 1))));+ assert.ok(skipped(invoke('status', status(first.run_id, 0))));+ const cancelled = invoke('cancel');+ assert.equal(cancelled[1], ticket);+ assert.equal(cancelled[2].active, false);+ assert.ok(skipped(invoke('result', result(first.run_id, 3))));+ const second = JSON.parse(invoke('begin')[0]);+ assert.ok(second.revision > first.revision && second.run_id !== first.run_id);+ assert.ok(skipped(invoke('status', status(first.run_id, 99, true))));+ assert.ok(skipped(invoke('result', result(first.run_id, 99))));+ assert.equal(invoke('status', status(second.run_id, 5, true))[1].active, false);+ assert.ok(skipped(invoke('status', status(second.run_id, 6))));+ // 终态快照先到时,同序号完整结果仍须被应用一次。+ const final = invoke('result', result(second.run_id, 5, index % 2 ? 'Failed' : 'Completed'));+ assert.equal(final.length, 16);+ assert.equal(final[15].active, false);+ assert.equal(listeners.size, 1);+ // 其他 iframe 的加载不能提前移除监听;真实预览加载只记录一次。+ for (const fn of listeners) fn({ target: { matches: () => false } });+ assert.equal(listeners.size, 1);+ for (const fn of listeners) fn({ target: { matches: () => true } });+ assert.equal(listeners.size, 0);+ assert.equal(JSON.parse(logs.at(-1)[1]).run_id, second.run_id);+ assert.ok(skipped(invoke('result', result(second.run_id, 5))));+ const third = JSON.parse(invoke('begin')[0]);+ assert.equal(invoke('result', result(third.run_id, 4))[15].active, false);+ assert.ok(skipped(invoke('status', status(third.run_id, 3))));+ assert.ok(skipped(invoke('status', status(third.run_id, 4, true))));+}+console.log('150 lifecycle iterations passed');diff --git a/tests/unittest/test_kit_gradio_conversion.py b/tests/unittest/test_kit_gradio_conversion.pynew file mode 100644index 00000000..8f27a6dd--- /dev/null+++ b/tests/unittest/test_kit_gradio_conversion.py@@ -0,0 +1,170 @@+"""完整响应、任务隔离和不可取消输出线程的回归测试。"""++from __future__ import annotations++import asyncio+import inspect+import json+import shutil+import subprocess+import threading+from pathlib import Path+from types import SimpleNamespace+from typing import Any+from unittest.mock import AsyncMock+from urllib.parse import unquote++import pytest++from mineru.kit.gradio import app as gradio_app+from mineru.kit.gradio.client import V1ServerCapabilities+from mineru.parser.base import ParseResult+from tests.unittest.test_kit_gradio import _middle_json, _pdf_bytes+++def _application(tmp_path: Path, client: Any) -> Any:+ """使用真实组件注册转换和轮询事件,仅替换解析服务。"""+ return gradio_app.build_gradio_app(+ client,+ V1ServerCapabilities("http://unused.test", ("flash", "basic"), ("zip",), ("file_id",)),+ output_root=tmp_path / "output",+ enable_example=False,+ )+++def _callback(app: Any, name: str) -> Any:+ """按名称读取事件,避免依赖组件数量和内部编号。"""+ return next(fn.fn for fn in app.fns.values() if fn.name == name)+++def _ticket(index: int) -> str:+ """生成有序浏览器提交,用于模拟响应乱序和取消先到。"""+ return json.dumps({"run_id": f"{index:032x}", "revision": index})+++def test_frontend_conversion_receipts() -> None:+ """执行真实前端脚本,覆盖迟到状态、清除、失败和重复回执。"""+ node = shutil.which("node")+ if node is None:+ pytest.skip("node unavailable")+ result = subprocess.run([node, str(Path(__file__).with_suffix(".cjs"))], capture_output=True, text=True, timeout=15)+ assert result.returncode == 0, result.stdout + result.stderr+++def test_conversion_and_poll_are_full_non_generator_responses(tmp_path: Path) -> None:+ """通过 Gradio process_api 反复序列化,确认结果不会创建生成器差分缓存。"""+ from gradio import Request+ from gradio.data_classes import FileData+ from gradio.state_holder import SessionState+ from gradio.utils import get_upload_folder++ source = tmp_path / "中文 #1.pdf"+ source.write_bytes(_pdf_bytes())+ client = SimpleNamespace(parse_file=AsyncMock(return_value=ParseResult(middle_json=_middle_json(with_image=False))))+ app = _application(tmp_path, client)+ ui = next(fn for fn in app.fns.values() if fn.name == "convert_ui")+ poll = next(fn for fn in app.fns.values() if fn.name == "read_conversion_status")+ public = next(fn for fn in app.fns.values() if fn.name == "convert_handler")+ assert all(inspect.iscoroutinefunction(fn.fn) for fn in (ui, public))+ assert all(not inspect.isasyncgenfunction(fn.fn) for fn in (ui, public, poll))+ assert poll.queue is False and len(public.inputs) == 4 and len(public.outputs) == 16+ # 模拟已上传的文件,保留 Gradio 实际输入预处理和输出序列化。+ uploaded = Path(get_upload_folder()) / tmp_path.name / source.name+ uploaded.parent.mkdir(parents=True, exist_ok=True)+ shutil.copyfile(source, uploaded)+ file_input = FileData(path=str(uploaded)).model_dump()+ request = Request(session_hash="serial", headers={"host": "localhost:7860"})+ session = SessionState(app)++ async def scenario() -> None:+ """连续成功和失败仍只返回完整回执,并保持终态及下载所有权。"""+ for index in range(1, 31):+ failed = index % 5 == 0+ client.parse_file.side_effect = RuntimeError("controlled failure") if failed else None+ response = await app.process_api(+ ui,+ [file_input, index % 2, "", False, _ticket(index)],+ state=session,+ request=request,+ session_hash="serial",+ event_id=str(index),+ )+ assert response["is_generating"] is False and response["iterator"] is None+ receipt = json.loads(response["data"][0])+ assert receipt["run_id"] == f"{index:032x}"+ assert len(receipt["outputs"]) == 15+ status = await app.process_api(poll, [_ticket(index)], state=session, request=request, session_hash="serial")+ snapshot = json.loads(status["data"][0])+ assert snapshot["terminal"] and snapshot["sequence"] == receipt["sequence"]+ if failed:+ assert "Failed:" in receipt["outputs"][0]+ else:+ file = receipt["outputs"][2]["value"]+ assert file["meta"]["_type"] == "gradio.FileData"+ assert unquote(file["url"]).endswith(file["path"]) and Path(file["path"]).is_file()+ downloader = next(fn.fn for fn in app.fns.values() if fn.name == "handler")+ path, download_receipt = downloader(None, json.dumps({"run_id": receipt["outputs"][6]}), request)+ assert path and not json.loads(download_receipt)["error"]+ assert not app.pending_diff_streams++ asyncio.run(scenario())+++def test_cancelled_sync_output_keeps_slot_until_thread_exits(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """输出线程被取消后继续持槽,旧回执失效且新任务只能在线程退出后开始。"""+ source = tmp_path / "source.pdf"+ source.write_bytes(_pdf_bytes())+ started, release = threading.Event(), threading.Event()+ persist = gradio_app.persist_parse_result+ client = SimpleNamespace(parse_file=AsyncMock(return_value=ParseResult(middle_json=_middle_json(with_image=False))))+ calls = 0++ def slow_output(*args: Any, **kwargs: Any) -> Any:+ """第一次输出停在同步线程中,取消也不能提前释放该工作。"""+ nonlocal calls+ calls += 1+ if calls == 1:+ started.set()+ assert release.wait(10)+ return persist(*args, **kwargs)++ monkeypatch.setattr(gradio_app, "persist_parse_result", slow_output)+ app = _application(tmp_path, client)+ convert, poll, cancel = (_callback(app, name) for name in ("convert_ui", "read_conversion_status", "cancel_ui_conversion"))+ request = SimpleNamespace(session_hash="same-session")++ async def scenario() -> None:+ """多次取消不能使同步工作逃逸到执行槽外,也不能取消较新的任务。"""+ first = asyncio.create_task(convert(str(source), 0, "", False, _ticket(1), request))+ pending: list[asyncio.Task[Any]] = [first]+ try:+ assert await asyncio.to_thread(started.wait, 3)+ assert "Preparing outputs" in poll(_ticket(1), request)+ cancellation = asyncio.create_task(cancel(_ticket(1), request))+ pending.append(cancellation)+ await asyncio.sleep(0.01)+ assert poll(_ticket(1), request) == "" and not first.done()+ second = asyncio.create_task(convert(str(source), 0, "", False, _ticket(2), request))+ pending.append(second)+ await asyncio.sleep(0.01)+ assert "Queued locally" in poll(_ticket(2), request)+ # 模拟 Gradio 取消与显式取消同时到达。+ first.cancel()+ await cancel(_ticket(1), request)+ await asyncio.sleep(0.01)+ assert client.parse_file.await_count == 1 and not first.done()+ release.set()+ assert await first == ""+ await cancellation+ result = json.loads(await second)+ assert result["run_id"] == f"{2:032x}" and "Completed" in result["outputs"][0]+ assert client.parse_file.await_count == 2+ # 取消先于排队提交到达时,旧提交不能重新建立状态。+ await cancel(_ticket(3), request)+ assert await convert(str(source), 0, "", False, _ticket(3), request) == ""+ assert client.parse_file.await_count == 2+ finally:+ release.set()+ await asyncio.gather(*pending, return_exceptions=True)++ asyncio.run(scenario())diff --git a/tests/unittest/test_kit_gradio_download.py b/tests/unittest/test_kit_gradio_download.pyindex 389fac86..c19bf973 100644--- a/tests/unittest/test_kit_gradio_download.py+++ b/tests/unittest/test_kit_gradio_download.py@@ -111,22 +111,22 @@ def test_download_event_chain_and_pdf_transport(tmp_path: Path) -> None:assert reset()[2]["visible"] is Falseassert reset()[7] == ""conversion = next(fn for fn in app.fns.values() if fn.name == "convert_handler")- dependency = next(dep for dep in app.config["dependencies"] if dep["id"] == conversion._id)- # 转换必须等待真正的重置完成事件,纯 JS 事件的 then 在支持版本中不会可靠触发。- reset_event = app.fns[dependency["trigger_after"]]- assert reset_event.name == "reset_download_ui"- assert len(reset_event.fn()) == len(reset_event.outputs)- assert reset_event.outputs[0].__class__.__name__ == "HTML"- assert "Preparing request..." in reset_event.fn()[0]- reset_dependency = next(dep for dep in app.config["dependencies"] if dep["id"] == reset_event._id)+ # 浏览器通过票据变化启动普通请求;公开 API 仍保留四个输入与原生文件输出。+ assert len(conversion.inputs) == 4+ ui_conversion = next(fn for fn in app.fns.values() if fn.name == "convert_ui")+ ticket = ui_conversion.inputs[-1]+ reset_dependency = next(+ dep for dep in app.config["dependencies"] if ticket._id in dep["outputs"] and '"begin"' in (dep["js"] or "")+ )+ assert reset_dependency["backend_fn"] is Falseassert "Preparing request..." in reset_dependency["js"]upload = next(block for block in app.blocks.values() if "mineru-upload-file" in (block.elem_classes or []))reset_on_upload = next(depfor dep in app.config["dependencies"]- if (upload._id, "change") in dep["targets"] and dep["outputs"] == [output._id for output in reset_event.outputs[1:]]+ if (upload._id, "change") in dep["targets"] and dep["outputs"] == reset_dependency["outputs"][1:-4])- assert reset_event.outputs[0]._id not in reset_on_upload["outputs"]+ assert reset_dependency["outputs"][0] not in reset_on_upload["outputs"]assert reset_on_upload["js"] and reset_on_upload["backend_fn"] is Falsehandlers = [fn for fn in app.fns.values() if fn.name == "handler"]assert len(handlers) == 7diff --git a/tests/unittest/test_kit_gradio_i18n.py b/tests/unittest/test_kit_gradio_i18n.pyindex a14ce599..e7cc8a80 100644--- a/tests/unittest/test_kit_gradio_i18n.py+++ b/tests/unittest/test_kit_gradio_i18n.py@@ -90,7 +90,7 @@ def test_conversion_preserves_all_preview_components_until_success(monkeypatch: pytest.MonkeyPatch,outcome: str,) -> None:- """暂停真实生成器,确认首帧、处理状态和失败都不改变已有四种预览。"""+ """暂停普通转换请求,确认处理期间不发送预览更新且失败保留已有四种预览。"""from mineru.kit.gradio import app as app_modulesource = tmp_path / "document.csv"@@ -126,15 +126,14 @@ def test_conversion_preserves_all_preview_components_until_success(updates: list[tuple[object, ...]] = []async def collect() -> None:- """独立消费生成器,避免测试本身阻挡状态通知和任务启动。"""- async for update in handler(str(source), 0, ""):- updates.append(update)+ """独立等待普通转换请求,避免阻挡后台阶段通知。"""+ updates.append(await handler(str(source), 0, ""))consumer = asyncio.create_task(collect())try:await asyncio.wait_for(started.wait(), timeout=3)- assert updates- assert all(update[2:6] == ({"__type__": "update"},) * 4 for update in updates)+ assert not updates+ assert not consumer.done()finish.set()await asyncio.wait_for(consumer, timeout=10)finally:diff --git a/tests/unittest/test_kit_gradio_json.py b/tests/unittest/test_kit_gradio_json.pyindex 2b0b0a86..210dba91 100644--- a/tests/unittest/test_kit_gradio_json.py+++ b/tests/unittest/test_kit_gradio_json.py@@ -38,7 +38,7 @@ def test_json_view_matches_saved_and_downloaded_structured_content(tmp_path: Patasync def collect() -> list[tuple]:"""完整消费转换流,检查 JSON 不随状态动画重复下发。"""- return [update async for update in conversion.fn(str(source), 0, "")]+ return [await conversion.fn(str(source), 0, "")]updates = asyncio.run(collect())state = updates[-1][6]@@ -73,18 +73,18 @@ def test_json_view_clears_on_file_change_clear_and_failed_conversion(tmp_path: Pasync def scenario() -> None:"""按成功、失败、重试顺序验证 JSON 的原子更新和清理。"""- first = [update async for update in conversion.fn(str(source), 0, "")]+ first = [await conversion.fn(str(source), 0, "")]assert "hello-0" in first[-1][-1]client.parse_file.side_effect = RuntimeError("test failure")- failure = [update async for update in conversion.fn(str(source), 0, "")]+ failure = [await conversion.fn(str(source), 0, "")]assert failure[0][-1] == failure[-1][-1] == ""assert "Failed:" in failure[-1][0]changed = _middle_json(with_image=False)changed.pages[0].blocks[0].content[0].content = "更新后的结果"client.parse_file.side_effect = Noneclient.parse_file.return_value = ParseResult(middle_json=changed)- retry = [update async for update in conversion.fn(str(source), 0, "")]- assert retry[0][-1] == ""+ retry = [await conversion.fn(str(source), 0, "")]+ assert len(retry) == 1assert "更新后的结果" in retry[-1][-1] and "hello-0" not in retry[-1][-1]asyncio.run(scenario())diff --git a/tests/unittest/test_kit_gradio_office_preview.py b/tests/unittest/test_kit_gradio_office_preview.pyindex 1362dd86..7e1b4299 100644--- a/tests/unittest/test_kit_gradio_office_preview.py+++ b/tests/unittest/test_kit_gradio_office_preview.py@@ -126,7 +126,7 @@ def test_office_conversion_never_replaces_uploaded_preview(async def collect() -> list[tuple[object, ...]]:"""消费完整生成器并检查每一次前端预览更新。"""- return [update async for update in handler(str(source), 0, "")]+ return [await handler(str(source), 0, "")]updates = asyncio.run(collect())assert updates and all(update[2:6] == ({"__type__": "update"},) * 4 for update in updates)diff --git a/tests/unittest/test_kit_gradio_page_range.py b/tests/unittest/test_kit_gradio_page_range.pyindex 4359c01b..0e57bb05 100644--- a/tests/unittest/test_kit_gradio_page_range.py+++ b/tests/unittest/test_kit_gradio_page_range.py@@ -269,7 +269,7 @@ def test_invalid_or_oversized_range_is_rejected_before_submission(tmp_path: Pathasync def collect() -> list[tuple[object, ...]]:"""收集错误事件并确认没有启动实际解析。"""- return [update async for update in handler(str(source), 1, raw)]+ return [await handler(str(source), 1, raw)]updates = asyncio.run(collect())assert "page_range_invalid" in str(updates[-1][0])diff --git a/tests/unittest/test_kit_gradio_source_preview.py b/tests/unittest/test_kit_gradio_source_preview.pyindex 9221fdc3..957c5bc0 100644--- a/tests/unittest/test_kit_gradio_source_preview.py+++ b/tests/unittest/test_kit_gradio_source_preview.py@@ -514,7 +514,7 @@ def test_parse_preserves_source_preview(tmp_path: Path, name: str, fail: bool) -async def collect() -> list:"""收集真实解析事件的完整输出序列。"""- return [value async for value in convert.fn(str(source), 0, "")]+ return [await convert.fn(str(source), 0, "")]final = asyncio.run(collect())[-1]assert final[2:6] == ({"__type__": "update"},) * 4diff --git a/tests/unittest/test_kit_gradio_status.py b/tests/unittest/test_kit_gradio_status.pyindex 50bbc70a..ecbc39d0 100644--- a/tests/unittest/test_kit_gradio_status.py+++ b/tests/unittest/test_kit_gradio_status.py@@ -1,10 +1,11 @@"""步骤卡片的计时、状态生命周期与安全文本回归。"""-import asyncio-from contextlib import aclosing+import jsonimport pytest+from mineru.kit.gradio.conversion import ConversionRun, SessionConversions+from mineru.kit.gradio.status import (STATUS_COMPLETED,STATUS_DOWNLOADING_RESULT,@@ -14,7 +15,6 @@ from mineru.kit.gradio.status import (STATUS_QUEUED_ON_SERVER,StatusPanelState,status_html,- stream_status_updates,)@@ -101,79 +101,39 @@ def test_failure_stops_timer_and_renders_escaped_error(error: str) -> None:assert "<script>" not in rendered-def test_status_stream_waits_for_stage_changes_and_cleans_up_waiter() -> None:- """验证排队和解析期间均无服务端计时帧,真实阶段仍及时更新。"""-- async def scenario() -> None:- """运行可手动推进时钟的解析等待场景。"""- now = [1.0]- state = StatusPanelState(clock=lambda: now[0])- events: asyncio.Queue[tuple[str, float]] = asyncio.Queue()- events.put_nowait((STATUS_QUEUED_ON_SERVER, 1.0))- task = asyncio.create_task(asyncio.Event().wait())- baseline = set(asyncio.all_tasks())- async with aclosing(stream_status_updates(task, events, state)) as stream:- assert "Queued on server" in await anext(stream)- now[0] = 2.3- next_update = asyncio.create_task(anext(stream))- await asyncio.sleep(0.03)- assert not next_update.done()- events.put_nowait((STATUS_PROCESSING_ON_SERVER, 2.3))- assert "(0.00s)" in await asyncio.wait_for(next_update, timeout=1)- next_update = asyncio.create_task(anext(stream))- await asyncio.sleep(0.03)- assert not next_update.done()- events.put_nowait((STATUS_DOWNLOADING_RESULT, 2.6))- assert "Task completed, downloading result" in await asyncio.wait_for(next_update, timeout=1)- assert set(asyncio.all_tasks()) == baseline- assert set(asyncio.all_tasks()) == baseline- task.cancel()- await asyncio.gather(task, return_exceptions=True)-- asyncio.run(scenario())--[email protected]("message", ["Preparing request...", STATUS_QUEUED_ON_SERVER, STATUS_PROCESSING_ON_SERVER])-def test_status_heartbeat_survives_duplicate_notifications(message: str) -> None:- """重复阶段通知不能饿死每秒快照,心跳也不能改变任务或阶段身份。"""-- async def scenario() -> None:- """持续发送同阶段通知并检查周期快照及即时阶段切换。"""- state = StatusPanelState()- state.append(message)- identity = (state.run_id, state.phase_id, state._processing_started)- events: asyncio.Queue[tuple[str, float]] = asyncio.Queue()- finish = asyncio.Event()- task = asyncio.create_task(finish.wait())-- async def repeat() -> None:- """模拟每次查询都返回相同阶段。"""- while not finish.is_set():- events.put_nowait((message, state.clock()))- await asyncio.sleep(0.05)-- sender = asyncio.create_task(repeat())- try:- async with aclosing(stream_status_updates(task, events, state)) as stream:- started = asyncio.get_running_loop().time()- first = await asyncio.wait_for(anext(stream), 2)- assert 0.9 <= asyncio.get_running_loop().time() - started < 2- assert 'data-mineru-status-seq="1"' in first- assert identity == (state.run_id, state.phase_id, state._processing_started)- sender.cancel()- await asyncio.gather(sender, return_exceptions=True)- events.put_nowait((STATUS_PROCESSING_OUTPUT, state.clock()))- update = await asyncio.wait_for(anext(stream), 0.5)- assert "Preparing outputs" in update- assert 'data-mineru-status-seq="2"' in update- finish.set()- assert [item async for item in stream] == []- finally:- finish.set()- sender.cancel()- await asyncio.gather(task, sender, return_exceptions=True)-- asyncio.run(scenario())+def test_complete_snapshots_stay_stable_and_reject_late_notifications() -> None:+ """重复轮询不改变内容,失败或完成后拒绝迟到阶段。"""+ run = ConversionRun("run-1")+ run.publish(STATUS_PROCESSING_ON_SERVER, at=1.0)+ snapshot = run.snapshot+ for _ in range(150):+ run.publish(STATUS_PROCESSING_ON_SERVER, at=2.0)+ assert run.snapshot == snapshot+ run.publish(STATUS_DOWNLOADING_RESULT, at=3.0)+ run.publish(STATUS_COMPLETED, at=4.0)+ final = json.loads(run.snapshot)+ assert final["run_id"] == "run-1" and final["terminal"] is True+ assert "Completed (2.00s)" in final["html"]+ assert final["sequence"] > json.loads(snapshot)["sequence"]+ run.publish(STATUS_PROCESSING_OUTPUT, at=5.0)+ assert json.loads(run.snapshot) == final+++def test_session_run_identity_and_revision_reject_stale_submissions() -> None:+ """同会话旧任务失效,其他会话保留自己的状态,旧修订无法重新启动。"""+ sessions = SessionConversions()+ old = sessions.start("a", "old", revision=1)+ other = sessions.start("b", "other", revision=1)+ current = sessions.start("a", "new", revision=2)+ assert old.cancelled and sessions.current("a", "old") is None+ assert sessions.current("a", "new") is current+ assert sessions.current("b", "other") is other+ assert sessions.start("a", "old", revision=1) is None+ sessions.cancel("a", "old")+ assert sessions.current("a", "new") is current+ sessions.cancel("a", "new")+ assert sessions.current("a", "new") is None+ assert sessions.current("b", "other") is otherdef test_local_queue_can_return_to_preparation() -> None: