feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 02:58:28 +08:00
1 parent 84a881b554
commit 74270adc22
149 files changed
+20111 -3871

No files matched your search

@@ -1,9 +1,9 @@
"""
backend/tests/test_shipping_processing_projection.py
用途:验收会话级 processing 投影分组、OT 去重与任务—PDF 两层标题规则。
依赖:Flask/SQLite、create_order_extraction_run、project_conversation_processing。
逻辑:构造 queued / frozen-failed / completed;断言多 PDF 不占用首文件名、hash 去重计数。
???????? processing ?????OT ??????PDF ???????
???Flask/SQLite?create_order_extraction_run?project_conversation_processing?
????? queued / frozen-failed / completed???? PDF ????????hash ?????
"""
from __future__ import annotations
@@ -29,11 +29,11 @@ from tests.test_shipping_order_extraction_runs import _seed_run_input
def _ensure_run_order_task(*, run, intake, document) -> None:
"""
为投影测试物化最小 Candidate + OT,并分配 Run.user_task_no。
????????? Candidate + OT???? Run.user_task_no?
核心逻辑:对齐 test_shipping_conversation_processing_api 模式;
Candidate 前 Run 无持久化 OT 时不应出现 order_task 卡;
重识别继承 OT 时走受控换绑。
??????? test_shipping_conversation_processing_api ???
Candidate ? Run ???? OT ????? order_task ??
????? OT ???????
"""
candidate = ShippingOrderCandidate.query.filter_by(
order_extraction_run_id=int(run.id),
@@ -63,7 +63,7 @@ def _ensure_run_order_task(*, run, intake, document) -> None:
def test_project_conversation_processing_groups_and_dedupes_user_tasks(app):
"""同会话多 Run 投影:按状态分组;同 OT 只保留最新。"""
"""???? Run ?????????? OT ??????"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-A')
run_processing, _ = create_order_extraction_run(
@@ -135,10 +135,10 @@ def test_project_conversation_processing_groups_and_dedupes_user_tasks(app):
completed_nos = {row['user_task_no'] for row in projection.get('completed') or []}
assert run_processing.user_task_no in processing_nos
assert run_failed.user_task_no in needs_nos
# 未查看的 completed 作为成功通知展示在 completed 分组。
# ???? completed ????????? completed ???
assert run_done.user_task_no in completed_nos
# 关键断言:每个活跃 OT 只出现一次。
# ????????? OT ??????
all_nos = list(processing_nos | needs_nos | completed_nos)
assert len(all_nos) == len(set(all_nos)) == 3
@@ -148,7 +148,10 @@ def test_project_conversation_processing_groups_and_dedupes_user_tasks(app):
)
assert failed_task.get('status_label') == 'failed'
assert 'view_details' in (failed_task.get('available_actions') or [])
assert failed_task.get('primary_action')
primary = failed_task.get('primary_action')
assert primary
# ???cancel / view_details ???? CTA?BE-01??
assert primary not in {'cancel_task', 'cancel_run', 'view_details'}
done_task = next(
row for row in projection['completed']
@@ -158,8 +161,12 @@ def test_project_conversation_processing_groups_and_dedupes_user_tasks(app):
assert done_task.get('group') == 'completed'
def test_fingerprint_merge_prefers_newer_processing_over_older_failed(app):
"""BE-02:同指纹旧失败 + 新处理中,必须展示新 Run。"""
def test_same_attachment_fingerprint_across_ots_keeps_two_cards(app):
"""
BE-02(产品选择 A):不同 user_task_no 即使附件指纹相同,也各出一张卡。
断言:failed OT + processing OT 同 content_hash → 两张卡;total_tasks == 2。
"""
with app.app_context():
from app.models import FileAsset, ShippingOrderDocument, ShippingOrderIntake
from datetime import timedelta
@@ -227,13 +234,17 @@ def test_fingerprint_merge_prefers_newer_processing_over_older_failed(app):
conversation_id=session.session_id,
)
shown = projection['processing'] + projection['needs_action']
assert len(shown) == 1
assert shown[0]['user_task_no'] == newer.user_task_no
assert shown[0]['run_no'] == newer.run_no
shown_nos = {row['user_task_no'] for row in shown}
# 跨 OT 同指纹不得折叠:两张独立卡。
assert len(shown) == 2
assert shown_nos == {'OT-OLD-FAIL', 'OT-NEW-PROC'}
assert projection['summary']['total_tasks'] == 2
assert projection['summary']['needs_action'] == 1
assert projection['summary']['processing'] == 1
def test_completed_current_run_hidden_after_viewed(app):
"""UI-04:当前 completed 查看后从右侧收起。"""
"""UI-04??? completed ?????????"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-DONE')
run, _ = create_order_extraction_run(
@@ -262,10 +273,14 @@ def test_completed_current_run_hidden_after_viewed(app):
)
shown = after['processing'] + after['needs_action'] + list(after.get('completed') or [])
assert all(row['user_task_no'] != run.user_task_no for row in shown)
# ?????? completed ?? history?????????
history_nos = {row['user_task_no'] for row in (after.get('history') or [])}
assert run.user_task_no in history_nos
assert after['summary']['history'] >= 1
def _set_run_documents(run, documents: list[dict]) -> None:
"""覆盖 Run 快照 documents,便于断言任务—PDF 两层投影。"""
"""?? Run ?? documents????????PDF ?????"""
snapshot = dict(run.input_snapshot_json or {})
intakes = list(snapshot.get('intakes') or [])
if not intakes:
@@ -277,8 +292,8 @@ def _set_run_documents(run, documents: list[dict]) -> None:
run.request_snapshot_json = snapshot
def test_single_pdf_task_uses_filename_title(app):
"""单 PDF 任务:title 为文件名,file_count=1,display_kind=single。"""
def test_single_pdf_task_title_is_none_for_frontend_unified_label(app):
"""BE-05?? PDF ?? title ?? None?????? document_names / objects?"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-1PDF')
run, _ = create_order_extraction_run(
@@ -306,16 +321,18 @@ def test_single_pdf_task_uses_filename_title(app):
row for row in projection['processing'] + projection['needs_action']
if row['user_task_no'] == run.user_task_no
)
assert task['title'] == 'only.pdf'
# ???????????????????????????? title?
assert task['title'] is None
assert task['file_count'] == 1
assert task['display_kind'] == 'single_document_task'
assert task.get('document_names') == ['only.pdf']
doc_objects = [obj for obj in (task.get('objects') or []) if obj.get('kind') == 'document']
assert len(doc_objects) == 1
assert doc_objects[0]['filename'] == 'only.pdf'
def test_multi_pdf_task_does_not_use_first_filename_as_title(app):
"""多 PDF 任务:title 为空,file_count=2,objects 含两个当前 PDF。"""
"""? PDF ???title ???file_count=2?objects ????? PDF?"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-2PDF')
run, _ = create_order_extraction_run(
@@ -362,7 +379,11 @@ def test_multi_pdf_task_does_not_use_first_filename_as_title(app):
def test_partial_reparse_keeps_unaffected_pdf_from_source_run(app):
"""同 OT 局部重识别:新版 PDF 替换自身,旧 Run 中未重识别 PDF 继续有效。"""
"""
BE-03:同 OT 局部重识别——被替代 Run 不另开顶层卡;有效 PDF 仍折叠保留。
断言:同一 user_task_no 仅一张卡;total_tasks == 1;未重识别 PDF 来自源 Run。
"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-PARTIAL-REPARSE')
source, _ = create_order_extraction_run(
@@ -412,7 +433,7 @@ def test_partial_reparse_keeps_unaffected_pdf_from_source_run(app):
)
replacement.reparse_of_run_id = source.id
replacement.reparse_reason = 'field_mapping_error'
# 投影用例无 Case 标记:用同 content_hash 满足严格文档谱系校验。
# 共享 content_hash,确保同 OT 谱系可交叉识别 PDF 身份。
shared_hash = str(document.content_hash or 'hash-partial-shared')
document.content_hash = shared_hash
replacement_document.content_hash = shared_hash
@@ -437,13 +458,21 @@ def test_partial_reparse_keeps_unaffected_pdf_from_source_run(app):
user_id=user.id,
conversation_id=session.session_id,
)
task = next(
row for row in projection['processing'] + projection['needs_action']
if row['user_task_no'] == source.user_task_no
all_cards = (
projection['processing']
+ projection['needs_action']
+ list(projection.get('completed') or [])
+ list(projection.get('history') or [])
)
# BE-03:被替代 Run 不另开顶层卡;同 OT 仅一张。
matching = [row for row in all_cards if row['user_task_no'] == source.user_task_no]
assert len(matching) == 1
assert projection['summary']['total_tasks'] == 1
task = matching[0]
assert task['run_no'] == replacement.run_no
doc_objects = [obj for obj in task['objects'] if obj.get('kind') == 'document']
# 关键断言:任务仍是两份 PDF;每份 PDF 保留其当前生效版本所属 Run。
# 局部重识别后:有效 PDF 仍为 2;未触碰 PDF 保留源 Run 版本。
assert task['file_count'] == 2
assert task['title'] is None
assert task['display_kind'] == 'multi_document_task'
@@ -456,7 +485,7 @@ def test_partial_reparse_keeps_unaffected_pdf_from_source_run(app):
def test_same_filename_different_hash_counts_as_two_pdfs(app):
"""同名不同 hash:file_count=2,不得按文件名误合并。"""
"""???? hash?file_count=2???????????"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SAME-NAME')
run, _ = create_order_extraction_run(
@@ -496,7 +525,7 @@ def test_same_filename_different_hash_counts_as_two_pdfs(app):
def test_same_hash_different_document_id_counts_as_one_pdf(app):
"""同 hash 不同 document_id(重传):file_count=1。"""
"""? hash ?? document_id?????file_count=1?"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SAME-HASH')
run, _ = create_order_extraction_run(
@@ -535,14 +564,15 @@ def test_same_hash_different_document_id_counts_as_one_pdf(app):
)
assert task['file_count'] == 1
assert task['display_kind'] == 'single_document_task'
assert task['title'] == 'order.pdf'
# BE-05: title ?? None?? hash ???????????
assert task['title'] is None
doc_objects = [obj for obj in (task.get('objects') or []) if obj.get('kind') == 'document']
assert len(doc_objects) == 1
assert doc_objects[0]['document_id'] == 22
def test_sparse_snapshot_enriched_by_extraction_units(app):
"""快照仅 1 PDF 但 Unit 已拆 2 份时,投影须 multi + file_count=2。"""
"""??? 1 PDF ? Unit ?? 2 ?????? multi + file_count=2?"""
from app.models import (
FileAsset,
ShippingOrderDocument,
@@ -589,7 +619,7 @@ def test_sparse_snapshot_enriched_by_extraction_units(app):
keep_input_open=True,
)
_ensure_run_order_task(run=run, intake=intake, document=document)
# 故意只写 1 份进快照,模拟生产侧栏仍显示单文件名的脏数据。
# ???? 1 ???????????????????????
_set_run_documents(run, [{
'document_id': int(document.id),
'root_document_id': int(document.id),
@@ -630,7 +660,7 @@ def test_sparse_snapshot_enriched_by_extraction_units(app):
@pytest.mark.skip(reason='workflow ledger removed')
def test_sparse_snapshot_enriched_by_workflow_items(app):
"""快照仅 1 PDF 但同 Run 已有 2 个 WorkflowItem 时,投影须 multi。"""
"""??? 1 PDF ?? Run ?? 2 ? WorkflowItem ????? multi?"""
with app.app_context():
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SPARSE-W')
from app.models import FileAsset, ShippingOrderDocument
@@ -715,3 +745,95 @@ def test_sparse_snapshot_enriched_by_workflow_items(app):
assert task['display_kind'] == 'multi_document_task'
assert task['title'] is None
assert 'peer.pdf' in (task.get('document_names') or [])
def test_primary_action_falls_back_to_focus_task_never_cancel(app, monkeypatch):
"""BE-01: no business entry -> primary_action=focus_task; never cancel/view_details."""
with app.app_context():
from app.services.shipping_notice import conversation_processing_projection as proj_mod
from app.services.shipping_notice import order_extraction_workspace_projection as ws_mod
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-FOCUS')
run, _ = create_order_extraction_run(
user=user,
agent_id=agent.id,
conversation_id=session.session_id,
intake_nos=[intake.intake_no],
keep_input_open=True,
)
_ensure_run_order_task(run=run, intake=intake, document=document)
db.session.commit()
monkeypatch.setattr(
ws_mod,
'_run_available_actions',
lambda _run, _units: ['cancel_run', 'view_details'],
)
primary, actions = proj_mod._resolve_task_actions(run)
assert primary == 'focus_task'
assert 'focus_task' in actions
assert 'view_details' in actions
assert 'cancel_task' in actions or 'cancel_run' in actions
assert primary not in {'cancel_task', 'cancel_run', 'view_details'}
def test_summary_has_more_and_returned_metadata_when_truncated(app):
"""BE-04: after list truncate, summary keeps full counts + has_more/returned."""
with app.app_context():
user, agent, session, intake_a, document_a = _seed_run_input(suffix='PROJ-T0')
run_a, _ = create_order_extraction_run(
user=user,
agent_id=agent.id,
conversation_id=session.session_id,
intake_nos=[intake_a.intake_no],
keep_input_open=True,
)
_ensure_run_order_task(run=run_a, intake=intake_a, document=document_a)
run_a.status = 'processing'
run_a.phase = 'extracting'
db.session.commit()
for suffix, agent_code in (
('PROJ-T1', 'shipping_notice_proj_t1'),
('PROJ-T2', 'shipping_notice_proj_t2'),
('PROJ-T3', 'shipping_notice_proj_t3'),
):
_, _, _, intake_x, document_x = _seed_run_input(suffix=suffix, agent_code=agent_code)
intake_x.user_id = user.id
intake_x.conversation_id = session.session_id
db.session.commit()
run_x, _ = create_order_extraction_run(
user=user,
agent_id=agent.id,
conversation_id=session.session_id,
intake_nos=[intake_x.intake_no],
allow_parallel_conversation_runs=True,
keep_input_open=True,
)
_ensure_run_order_task(run=run_x, intake=intake_x, document=document_x)
run_x.status = 'processing'
run_x.phase = 'extracting'
db.session.commit()
projection = project_conversation_processing(
user_id=user.id,
conversation_id=session.session_id,
limit=2,
history_limit=1,
)
summary = projection['summary']
returned_active = (
len(projection['needs_action'])
+ len(projection['processing'])
+ len(projection.get('completed') or [])
)
# Assert: full counts stay ahead of returned lists; truncate flags match.
assert summary['total_tasks'] >= 4
assert summary['total_tasks'] >= returned_active
assert summary.get('returned', {}).get('needs_action') == len(projection['needs_action'])
assert summary.get('returned', {}).get('processing') == len(projection['processing'])
assert summary.get('returned', {}).get('completed') == len(projection.get('completed') or [])
assert summary.get('returned', {}).get('history') == len(projection.get('history') or [])
assert summary.get('has_more_active') is True
assert summary.get('truncated', {}).get('active') is True
assert returned_active == 2