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: