175 lines
5.6 KiB
Python
175 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""v1.0.46 回归:超收护栏(实收远超剩余应收 → 转人工,不自动分摊)
|
||
|
||
现场事故(2026-11-05 17:45):南充菜一箱 24kg(48 斤)被 AI 认成「小葱」
|
||
(把握 82%),名单里小葱两单只剩 3.5 斤。同名分摊分支按剩余量把 0.5+3.0
|
||
斤自动入账,超收 44.5 斤只打了一句提示 —— 错账就这么进了系统。
|
||
|
||
本文件验证:
|
||
① 实收 > 剩余合计 × over_receipt_ratio(默认3) → 不提交,转人工(warn 有提示)
|
||
② 实收在倍数以内(如 2 倍)→ 照旧自动分摊提交(护栏别拦正常分摊)
|
||
③ over_receipt_ratio=0 → 关闭护栏,回到旧行为
|
||
④ 单行超收仍由原有 auto_over_pct 30% 闸门拦(不回归)
|
||
|
||
不需要秤/摄像头/网络。
|
||
"""
|
||
import sys
|
||
import io
|
||
import os
|
||
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
||
from core.flow import FlowEngine
|
||
|
||
CFG = {
|
||
'flow': {'min_scale_kg': 0.05, 'stable_ms': 400, 'settle_ms': 650,
|
||
'stable_delta_kg': 0.03, 'done_timeout_s': 12,
|
||
'done_lift_off_kg': 0.2, 'confirm_before_submit': True,
|
||
'auto_submit': True, 'same_name_auto': True,
|
||
'merge_same_name': True, 'over_receipt_ratio': 3,
|
||
'weight_pick_pct': 40},
|
||
'scale': {'simulate': True},
|
||
'camera': {'simulate': True},
|
||
'vision': {'min_confidence': 60, 'auto_over_pct': 30,
|
||
'same_kind_guard': False},
|
||
'server': {'mock': True},
|
||
'noweigh': {'picking_allow_weight_unit': True},
|
||
}
|
||
|
||
EVENTS = []
|
||
|
||
|
||
def emit(ev, p=None):
|
||
EVENTS.append((ev, p))
|
||
|
||
|
||
eng = FlowEngine(CFG, emit)
|
||
eng.refresh_pending = lambda initial=False: None
|
||
eng.logged_in = True
|
||
eng._running = True
|
||
eng.server_config = {'weigh_match_tolerance_mode': 'percent',
|
||
'weigh_match_tolerance_value': 50}
|
||
|
||
|
||
class FakeJournal:
|
||
def begin(self, *a, **k):
|
||
return {'op': 'op'}
|
||
|
||
def update(self, *a, **k):
|
||
pass
|
||
|
||
def conclude(self, *a, **k):
|
||
pass
|
||
|
||
def has_unresolved(self):
|
||
return False
|
||
|
||
|
||
class FakeRecog:
|
||
def __init__(self, name='小葱', conf=82, alt='小白菜'):
|
||
self.name, self.conf, self.alt = name, conf, alt
|
||
|
||
def identify_ex(self, path, names, timeout=8):
|
||
return {'name': self.name, 'conf': self.conf, 'alt': self.alt,
|
||
'alt_conf': None}
|
||
|
||
|
||
def rows_xiaocong():
|
||
"""小葱两单,合计只剩 3.5 斤(与 17:45 事故一致)"""
|
||
return [
|
||
{'id': 'a1', 'item_name': '小葱', 'unit': '斤', 'required_qty': 0.5,
|
||
'order_code': 'ORD_...0004', 'sku_code': 'S1'},
|
||
{'id': 'a2', 'item_name': '小葱', 'unit': '斤', 'required_qty': 3.0,
|
||
'order_code': 'ORD_...0003', 'sku_code': 'S1'},
|
||
]
|
||
|
||
|
||
def reset(rows, kg, ratio=3):
|
||
CFG['flow']['over_receipt_ratio'] = ratio
|
||
EVENTS.clear()
|
||
eng.rows = [dict(r) for r in rows]
|
||
eng.state = 'IDLE'
|
||
eng._focus = None
|
||
eng._count_mode = False
|
||
eng._count_qty = 0.0
|
||
eng._frozen_kg = kg
|
||
eng._frozen_row = None
|
||
eng._aborted = False
|
||
eng._running = True
|
||
eng._last_ok = None
|
||
eng._plan = []
|
||
eng.journal = FakeJournal()
|
||
eng._cycle_conf = 82
|
||
eng._cycle_detected = ''
|
||
eng._cycle_recog_path = 'fake.jpg'
|
||
eng.recog = FakeRecog()
|
||
eng.submitted = []
|
||
eng._start_submit = lambda row, biz, src: (
|
||
eng.submitted.append((row['id'], biz, src)),
|
||
eng._set_state('SUBMIT'))
|
||
|
||
|
||
def last_focus():
|
||
for e, p in reversed(EVENTS):
|
||
if e == 'focus':
|
||
return p
|
||
return None
|
||
|
||
|
||
def manual_warn():
|
||
"""confirm_before_submit=true 时 _to_manual 走确认条:warn 挂在 focus 事件上"""
|
||
f = last_focus()
|
||
return (f or {}).get('warn')
|
||
|
||
|
||
ok = fail = 0
|
||
|
||
|
||
def check(name, cond, extra=''):
|
||
global ok, fail
|
||
if cond:
|
||
ok += 1
|
||
print(' ✓', name)
|
||
else:
|
||
fail += 1
|
||
print(' ✗', name, extra)
|
||
|
||
|
||
print('=== 1. 事故复现:48 斤 vs 剩余 3.5 斤 → 必须转人工,不能分摊入账 ===')
|
||
reset(rows_xiaocong(), kg=24.0) # 24kg = 48 斤
|
||
eng._run_recognition()
|
||
print(' state =', eng.state, '| submitted =', eng.submitted)
|
||
w = manual_warn()
|
||
print(' warn =', w)
|
||
check('没有自动提交', eng.submitted == [])
|
||
check('状态停在确认条 CONFIRM(等人点)', eng.state == 'CONFIRM')
|
||
check('提示里说明超出倍数', bool(w) and '倍' in str(w))
|
||
check('日志里有超收护栏提示', any('不自动分摊' in x for x in
|
||
[str(p) for e, p in EVENTS if e == 'log']))
|
||
|
||
print('=== 2. 倍数以内(实收 6 斤 = 剩余 3.5 斤的 1.7 倍)→ 照常分摊 ===')
|
||
reset(rows_xiaocong(), kg=3.0) # 3kg = 6 斤
|
||
eng._run_recognition()
|
||
print(' submitted =', eng.submitted, '| plan =', eng._plan)
|
||
check('自动提交了第一单', len(eng.submitted) == 1)
|
||
check('分摊队列有第二单', len(eng._plan) >= 1)
|
||
|
||
print('=== 3. over_receipt_ratio=0 → 护栏关闭(旧行为) ===')
|
||
reset(rows_xiaocong(), kg=24.0, ratio=0)
|
||
eng._run_recognition()
|
||
print(' submitted =', eng.submitted)
|
||
check('关闭后恢复自动分摊', len(eng.submitted) == 1)
|
||
|
||
print('=== 4. 单行超收仍被 auto_over_pct 拦住 ===')
|
||
reset([{'id': 'b1', 'item_name': '小葱', 'unit': '斤', 'required_qty': 3.5,
|
||
'order_code': 'PO1', 'sku_code': 'S1'}], kg=24.0)
|
||
eng._run_recognition()
|
||
print(' state =', eng.state, '| submitted =', eng.submitted)
|
||
check('单行超收不自动提交', eng.submitted == [])
|
||
check('停在确认条等人', eng.state == 'CONFIRM')
|
||
|
||
print()
|
||
print('通过 %d 项,失败 %d 项' % (ok, fail))
|
||
sys.exit(1 if fail else 0)
|