diff --git a/backend/app/core/config/settings.py b/backend/app/core/config/settings.py index a0b72804..b34875cc 100644 --- a/backend/app/core/config/settings.py +++ b/backend/app/core/config/settings.py @@ -639,7 +639,9 @@ class Config: SHIPPING_NOTICE_PARSE_WORKER_TIMEOUT_SECONDS = int( os.getenv('SHIPPING_NOTICE_PARSE_WORKER_TIMEOUT_SECONDS', '300') ) - # 订单抽取发送后选择计划预算:首轮只挑最相关附件,后续批次按固定预算待扩展。 + # 订单抽取发送后选择计划预算:合格附件全量入选批次 1,有多少单解析多少单; + # 唯一生效的选择预算是 MAX_TOTAL_BYTES(超限转 Review)。数量类字段仅作快照 + # 审计记录,不再截断;总量由 EML 层(每封 20)与单次上传 intake 数兜底。 SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_ATTACHMENTS = int( os.getenv('SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_ATTACHMENTS', '2') ) @@ -650,7 +652,7 @@ class Config: os.getenv('SHIPPING_NOTICE_ORDER_EXTRACTION_EXPANSION_BATCH_SIZE', '2') ) SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_ATTACHMENTS = int( - os.getenv('SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_ATTACHMENTS', '6') + os.getenv('SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_ATTACHMENTS', '20') ) SHIPPING_NOTICE_PARSER_VERSION = os.getenv('SHIPPING_NOTICE_PARSER_VERSION', 'v6') SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED = os.getenv( diff --git a/backend/app/services/shipping_notice/attachment_selection.py b/backend/app/services/shipping_notice/attachment_selection.py index a13e7e13..0f3bed58 100644 --- a/backend/app/services/shipping_notice/attachment_selection.py +++ b/backend/app/services/shipping_notice/attachment_selection.py @@ -4,7 +4,8 @@ services/shipping_notice/attachment_selection.py 用途:基于 Intake 快览事实生成可审计的附件选择计划。 依赖:ShippingOrderIntake、标准库正则与哈希;只读取 structured_result_json,不回读 Document/布局结果。 逻辑:发送阶段调用本文件,把当前批次附件目录转换为“为何选择/为何跳过”的稳定计划; - 所有预算内附件先物化为同一 Run 的 Unit,Worker 再按并发预算执行,避免把排队误建模为扩批。 + 合格业务附件全量入选批次 1(有多少单解析多少单),仅字节预算超限转 Review; + 数量类预算字段仅作快照记录,不再截断,扩批链路不再产出。 """ from __future__ import annotations @@ -150,10 +151,11 @@ def build_attachment_selection_snapshot( Args: intakes: 当前发送批次的 Intake;必须已经通过上传快览。 - max_initial_attachments: 兼容旧快照的首轮附件字段;新计划不以它限制 Unit 创建。 + max_initial_attachments: 历史兼容字段,仅写入快照作审计记录,不参与选择。 max_initial_concurrency: Worker 最大并发,仅限制执行,不限制 Unit 创建。 - expansion_batch_size: 后续每轮扩展的附件上限。 - max_total_attachments: 单次邮件深度解析总附件预算上限。 + expansion_batch_size: 历史兼容字段,仅写入快照作审计记录,不参与选择。 + max_total_attachments: 历史兼容字段,仅写入快照作审计记录,不再按数量截断。 + max_total_bytes: 唯一生效的选择预算;累计超限的附件以 budget_total_bytes 转 Review。 Returns: 可写入 Run 快照的脱敏计划摘要,包含每个附件的 selected / skipped 结论与原因码。 @@ -227,19 +229,16 @@ def build_attachment_selection_snapshot( eligible_ordered = sorted(eligible, key=lambda item: (-int(item['score']), str(item['filename']))) byte_budget = max(0, int(max_total_bytes or 0)) planned_bytes = 0 - for index, row in enumerate(eligible_ordered): + for row in eligible_ordered: size = int(row.get('size') or 0) if byte_budget and planned_bytes + size > byte_budget: row['skip_reason_codes'] = ['budget_total_bytes'] continue - # [STEP 1] 同一发送批次内的预算内附件必须全部创建 Unit。执行并发由 - # max_initial_concurrency 在物化/Worker 层控制,不能借“首轮选择”延迟附件身份。 - if index < max_total_attachments: - row['selected'] = True - row['selection_batch'] = 1 - planned_bytes += size - continue - row['skip_reason_codes'] = ['budget_total_limit'] + # [STEP 1] 合格业务附件全量入选,不设数量截断:有多少单解析多少单。 + # 执行节奏由 max_initial_concurrency 与 Worker 运行时预算控制,扩批链路不再产出。 + row['selected'] = True + row['selection_batch'] = 1 + planned_bytes += size group_counts: dict[str, int] = defaultdict(int) for row in rows: diff --git a/backend/app/services/shipping_notice/order_extraction_run_service.py b/backend/app/services/shipping_notice/order_extraction_run_service.py index 10dba00f..537e72ce 100644 --- a/backend/app/services/shipping_notice/order_extraction_run_service.py +++ b/backend/app/services/shipping_notice/order_extraction_run_service.py @@ -571,7 +571,7 @@ def _require_owner_agent(*, user: User, agent_id: int) -> Agent: def _selection_budget_settings() -> dict[str, int]: - """读取订单抽取附件预算,统一生成首轮与后续扩展批次的选择计划。""" + """读取订单抽取附件预算。数量类字段仅作快照记录;唯一生效的选择预算是字节上限。""" max_initial_attachments = max( int(current_app.config.get('SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_ATTACHMENTS', 2) or 2), 1, @@ -1088,10 +1088,19 @@ def synchronize_order_extraction_expansion_snapshot( 1 for row in selection.get('items') or [] if isinstance(row, dict) and bool(row.get('selected')) ) - pending_batches = selection.get('pending_expansion_batches') - if isinstance(pending_batches, dict): - pending_batches.pop(str(normalized_batch), None) - pending_batches.pop(normalized_batch, None) + # [STEP] 激活批次后必须按明细重算扩批事实:pending 标量若停留在构建期数值, + # 投影层会在 Run 非完成态下把已全部处理的 Run 误报为「预算停扩」。 + pending_expansion: dict[int, int] = {} + for row in selection.get('items') or []: + if not isinstance(row, dict): + continue + planned_batch = int(row.get('planned_selection_batch') or 0) + if planned_batch > 1: + pending_expansion[planned_batch] = pending_expansion.get(planned_batch, 0) + 1 + selection['pending_expansion_attachment_count'] = sum(pending_expansion.values()) + selection['pending_expansion_batches'] = { + str(batch): count for batch, count in sorted(pending_expansion.items()) + } schema = snapshot.get('schema') if isinstance(snapshot.get('schema'), dict) else {} schema_version_id = int(schema.get('schema_version_id') or run.schema_version_id or 0) diff --git a/backend/tests/test_shipping_attachment_selection.py b/backend/tests/test_shipping_attachment_selection.py index 18d931da..b6bf4b58 100644 --- a/backend/tests/test_shipping_attachment_selection.py +++ b/backend/tests/test_shipping_attachment_selection.py @@ -166,6 +166,43 @@ def test_attachment_selection_snapshot_stops_at_total_byte_budget(): assert by_name['second.pdf']['skip_reason_codes'] == ['budget_total_bytes'] +def test_attachment_selection_snapshot_selects_all_eligible_attachments(): + """合格业务附件全量入选批次 1:数量类预算字段不得截断,有多少单解析多少单。""" + attachments = [ + { + 'attachment_key': f'po-{no:02d}.pdf', + 'filename': f'PO_45188381{no:02d}.pdf', + 'mime_type': 'application/pdf', + 'size': 48000, + 'content_hash': f'{no:02d}' * 32, + 'ready_for_extraction': True, + 'catalog_reason': 'ready_for_extraction', + } + for no in range(1, 13) + ] + snapshot = build_attachment_selection_snapshot( + intakes=[_preview_intake('SNI-ALL', attachments)], + max_total_attachments=6, + expansion_batch_size=2, + ) + + assert snapshot['selection_version'] == 'shipping_notice_attachment_selection.v1' + # 关键断言:即使数量预算字段为 6,12 份同分 PDF 也必须全部入选批次 1, + # 不产生 planned_selection_batch 或任何跳过原因码(本用例复现历史丢件形态)。 + assert snapshot['eligible_attachment_count'] == 12 + assert snapshot['selected_attachment_count'] == 12 + assert snapshot['planned_attachment_count'] == 12 + assert snapshot['pending_expansion_attachment_count'] == 0 + assert snapshot['pending_expansion_batches'] == {} + assert [item['filename'] for item in snapshot['items'] if item['selected']] == [ + f'PO_45188381{no:02d}.pdf' for no in range(1, 13) + ] + for item in snapshot['items']: + assert item['selection_batch'] == 1 + assert item['planned_selection_batch'] is None + assert item['skip_reason_codes'] == [] + + def test_attachment_selection_snapshot_excludes_inline_resources_from_counts(): """邮件签名图不属于用户业务附件,不能占用识别计划的附件数量或预算。""" snapshot = build_attachment_selection_snapshot(intakes=[ @@ -234,6 +271,7 @@ def test_serialize_order_extraction_run_includes_selection_plan_summary(monkeypa run_no='SNER-SELECTION', owner_agent_id=1, conversation_id='conv-selection', + client_submission_id=None, status='queued', phase='queued', schema_version_id=1, diff --git a/backend/tests/test_shipping_order_extraction_runs.py b/backend/tests/test_shipping_order_extraction_runs.py index 6248867c..28723930 100644 --- a/backend/tests/test_shipping_order_extraction_runs.py +++ b/backend/tests/test_shipping_order_extraction_runs.py @@ -1461,6 +1461,10 @@ def test_expansion_batch_syncs_materialized_document_into_frozen_run_before_crea assert run.input_snapshot_hash != old_hash assert plan.input_snapshot_hash == run.input_snapshot_hash assert [unit.document_id for unit in created] == [second_document.id] + # 关键断言:批次激活后扩批事实必须按明细重算归零,否则投影层会误报「预算停扩」。 + selection_after = run.input_snapshot_json['attachment_selection'] + assert selection_after['pending_expansion_attachment_count'] == 0 + assert selection_after['pending_expansion_batches'] == {} def test_run_submission_rejects_changed_frozen_mail_context(app): diff --git a/docs/features/shipping-notice/technical.md b/docs/features/shipping-notice/technical.md index d33d4e91..5f125a08 100644 --- a/docs/features/shipping-notice/technical.md +++ b/docs/features/shipping-notice/technical.md @@ -1586,7 +1586,7 @@ EML FileAsset -> ShippingNoticeIntake | 重复隔离 | 候选 A 重复只阻断 A;Intake 概览可为 `partial_duplicate` | | 重试幂等 | Document 使用内容与附件键去重;Candidate 使用 intake + candidate_key;Run 使用冻结 snapshot、Provider 结果缓存与建批幂等键回放 | | 解析器版本 | 默认 `SHIPPING_NOTICE_PARSER_VERSION=v6`(上传阶段仅落快览摘要;附件 Document/派生资产与布局任务延后到发送后的按需深度解析) | -| 发送后识别预算 | 一次发送只创建一个 Run;所有未超过总数量/字节预算的附件均在首轮写入 Run 快照、物化 Document、创建 Unit 并入队。`SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_CONCURRENCY=2` 只限制深度解析/Worker 的同时执行数;`MAX_INITIAL_ATTACHMENTS`、`EXPANSION_BATCH_SIZE` 为历史兼容配置,不再限制首轮 Unit 数量。超过 `MAX_TOTAL_ATTACHMENTS=6` 或 `MAX_TOTAL_BYTES=30MB` 的附件以稳定原因码进入 Review。历史扩批必须先将新 Document 同步进 Run/Plan 冻结快照并排 Layout,再创建 Unit;任一附件的字段缺失、候选状态或技术结果不得阻止兄弟附件执行。 | +| 发送后识别预算 | 一次发送只创建一个 Run;合格业务附件**全量**写入 Run 快照、物化 Document、创建 Unit 并入队——有多少单解析多少单,不设数量截断。`SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_CONCURRENCY=2` 只限制深度解析/Worker 的同时执行数。`MAX_INITIAL_ATTACHMENTS`、`EXPANSION_BATCH_SIZE`、`MAX_TOTAL_ATTACHMENTS` 为历史记录字段,写入快照作审计用途、不参与选择;唯一生效的选择预算是 `MAX_TOTAL_BYTES=30MB`(累计超限的附件以稳定原因码转 Review)。附件总量由上游兜底:EML 层每封最多 `SHIPPING_NOTICE_EML_MAX_ATTACHMENTS=20` 个附件、单次发送最多 50 个 Intake、原始输入超 30MB 整批拒发。历史扩批链路(按批激活)保留为不可达兜底,不再产出 planned 批次;任一附件的字节超限转 Review、字段缺失、候选状态或技术结果不得阻止兄弟附件执行。 | | 建单 | `ShippingTask.source_candidate_id` 已启用;满足自动建单门禁的 Candidate 分别创建 Task,待处理 Candidate 保留在原 Intake | 实现入口:上传快览由 `run_intake_parse` 直接写入 Intake 摘要;`persist_intake_documents` 仅保留给模板样本或发送后深度解析链路复用。完整订单抽取由 order_extraction_run_service、shipping_order_extraction Worker 与 persist_order_extraction_result 协作。 @@ -1928,14 +1928,14 @@ GET /api/admin/shipping-order-schema/published-contract # 订单抽取固定由 Intake Run + 本地确定性 Worker 执行;不依赖 Chatflow、Dify Workflow 或应用管理执行器。 -# 发送后附件选择与 Worker 资源预算:所有预算内附件立即入同一 Run; -# 实际深度解析并发固定为 2,整次最多 6 个、30MB 原始输入、5 分钟。 -# 任一预算停止均转人工复核,不能自动冻结。 -# 历史兼容字段;不再限制首轮 Unit 数量。 +# 发送后附件选择与 Worker 资源预算:合格附件全量入选同一 Run(有多少单解析多少单), +# 不设数量截断;唯一生效的选择预算是 30MB 字节(超限转 Review,不能自动冻结)。 +# 实际深度解析并发固定为 2,整次运行时预算 5 分钟。 +# 以下数量字段为历史记录用途(写入快照审计),不参与选择: # SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_ATTACHMENTS=2 # SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_INITIAL_CONCURRENCY=2 # SHIPPING_NOTICE_ORDER_EXTRACTION_EXPANSION_BATCH_SIZE=2 -# SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_ATTACHMENTS=6 +# SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_ATTACHMENTS=20 # SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_TOTAL_BYTES=31457280 # SHIPPING_NOTICE_ORDER_EXTRACTION_MAX_RUNTIME_SECONDS=300