fix(shipping-notice): 订单附件选择取消数量截断改为全量入选,修复超预算静默丢弃

修复:
- 合格业务附件全量入选批次 1,有多少单解析多少单,不设数量截断;
  唯一生效的选择预算为 MAX_TOTAL_BYTES=30MB,累计超限转 Review
- 数量类预算字段(MAX_INITIAL_ATTACHMENTS / EXPANSION_BATCH_SIZE /
  MAX_TOTAL_ATTACHMENTS)仅写入快照作审计记录,不再参与选择
- 扩批同步按明细重算 pending 计数,防止投影层误报「预算停扩」
- 总量兜底:EML 每封 ≤20 附件、单次 ≤50 Intake、原始输入超 30MB 拒发
This commit is contained in:
hugo committed 2026-09-22 11:02:54 +08:00
1 parent 881863478e
commit 13c33d063c
6 files changed
+78 -26

No files matched your search

@@ -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:
@@ -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)