refactor(shipping-inventory): 统一库存准备事实来源与门禁状态

This commit is contained in:
hugo committed 2026-09-04 20:23:00 +08:00
1 parent 761a06e21a
commit eb55f407ae
62 files changed
+4242 -1471

No files matched your search

@@ -37,7 +37,7 @@ from app.models import (
ShippingProcessGateAttempt,
ShippingNotice,
ShippingOrderTask,
User,
@@ -50,6 +50,7 @@ from app.services.shipping_notice.process_gate_contract import PROCESS_GATE_CODE
from app.services.shipping_notice.process_gate_service import (
ensure_execution_for_workflow_version,
ensure_process_gates_for_execution,
invalidate_downstream_gates,
@@ -75,7 +76,7 @@ def _seed_workflow_version(
"""
创建最小 User + ShippingNotice,供 Execution 绑定 review_case_id / task_id。
创建最小 User + ShippingOrderTask,供 Execution 绑定 review_case_id / task_id。
@@ -109,13 +110,13 @@ def _seed_workflow_version(
db.session.flush()
task = ShippingNotice(
task = ShippingOrderTask(
task_no=f'SN-GATE-{tag}',
task_no=f'OT-SN-GATE-{tag}',
user_id=int(user.id),
source_type='manual',
source_type='direct_pdf',
)
@@ -168,7 +169,32 @@ def test_ensure_execution_creates_all_contract_gates(gate_execution):
assert len(gates) == len(PROCESS_GATE_CODES)
assert {g.gate_code for g in gates} == set(PROCESS_GATE_CODES)
def test_existing_execution_gate_sequence_is_reconciled_to_current_contract(gate_execution):
"""既有 Execution 的 Gate 顺序必须跟随当前契约修正,不能继续沿用旧 sequence_no。"""
gates = {
gate.gate_code: gate
for gate in ShippingProcessGate.query.filter_by(execution_id=int(gate_execution.id)).all()
}
# 先移到不冲突的临时区间,再模拟旧版顺序。
for index, gate in enumerate(gates.values(), start=1):
gate.sequence_no = 100 + index
db.session.flush()
gates['task_creation'].sequence_no = 12
gates['order_freeze_readiness'].sequence_no = 10
gates['inventory_source_routing'].sequence_no = 11
gates['inventory_preparation'].sequence_no = 13
db.session.flush()
ensure_process_gates_for_execution(gate_execution)
db.session.commit()
assert gates['task_creation'].sequence_no == 10
assert gates['order_freeze_readiness'].sequence_no == 11
assert gates['inventory_source_routing'].sequence_no == 12
assert gates['inventory_preparation'].sequence_no == 13
assert set(gates) == set(PROCESS_GATE_CODES)
assert gate_execution.current_gate_code == 'document_materialization'
@@ -243,7 +269,7 @@ def test_auto_progress_queues_initial_inventory_rpa_once(gate_execution, monkeyp
def test_auto_progress_does_not_requeue_failed_inventory_rpa(gate_execution, monkeypatch):
"""已有终态 Attempt 后,库存 RPA 只允许人工重试,不能被自动无限重放。"""
from app.services.shipping_notice import process_gate_service
from app.models import ShippingNotice, ShippingTaskInventoryRpaAttempt
from app.models import ShippingOrderTask, ShippingTaskInventoryRpaAttempt
for prior_gate in ShippingProcessGate.query.filter(
ShippingProcessGate.execution_id == int(gate_execution.id),
@@ -255,7 +281,7 @@ def test_auto_progress_does_not_requeue_failed_inventory_rpa(gate_execution, mon
).one()
gate.status = 'blocked_retryable'
gate.next_action_code = 'retry_inventory_preparation'
task = db.session.get(ShippingNotice, int(gate_execution.task_id))
task = db.session.get(ShippingOrderTask, int(gate_execution.task_id))
db.session.add(ShippingTaskInventoryRpaAttempt(
task_no=task.task_no, source_code='TEST', attempt_no=1, source_run_no='PSR-FAILED',
status='failed', trigger_file_path='C:/trigger.json', execution_phase='trigger_written',
@@ -605,6 +631,59 @@ def test_business_sync_reuses_same_gate_active_attempt(app):
assert ShippingProcessGateAttempt.query.filter_by(gate_id=int(gate.id)).count() == 1
def test_business_sync_does_not_swallow_passed_when_gate_has_active_user_attempt(app):
"""活动 Attempt 不得吞掉 sync 的 passed 结论。
核心回归:真实任务 SN-20260831-9F25A698 / gate inventory_matching 曾因
同一秒存在管理员手动 override_line_inventory_source(running)而让
sync_fulfillment_gates_from_task 的 match_passed=True 被 active_attempt
短路 `return gate` 丢弃;随后手动动作失败把 gate 覆盖成 blocked_retryable,
右侧前端误显示「自动处理失败」。
契约:当业务事实判定为 passed 时,即使该 gate 上存在一个并发的、非本轮
inventory_preparation 自动轮询的 Attempt,sync 也必须把 passed 写入 gate
(完成该活动 Attempt 收口),而不是静默返回导致真相被覆盖。
"""
from app.services.shipping_notice.process_gate_service import complete_gate_attempt, mark_attempt_running
with app.app_context():
_user, review_case_id, task_id = _seed_workflow_version(suffix='passnotswallowed')
execution = ensure_execution_for_workflow_version(
review_case_id=review_case_id,
task_id=task_id,
)
# 模拟并发占用的用户手动 Attempt(非 inventory_preparation 自动轮询)。
user_attempt = queue_gate_attempt(
execution_no=execution.execution_no,
gate_code='inventory_matching',
trigger_type='user_manual',
actor_type='user',
actor_id=1,
idempotency_key='concurrent-user-override',
input_snapshot={'action_code': 'override_line_inventory_source'},
)
mark_attempt_running(attempt_id=int(user_attempt.id), worker_id='concurrent-user')
# 此时业务事实判定 passed(例如 match_status='fulfillable')。
gate = sync_gate_from_business_result(
execution_no=execution.execution_no,
gate_code='inventory_matching',
passed=True,
effective_result_json={
'findings': [],
'has_inventory_lines': True,
'match_status': 'fulfillable',
},
idempotency_key='match-passed-not-swallowed',
)
assert gate is not None
assert gate.status == 'passed', (
'gate 上存在并发的非自动轮询 Attempt 时,sync 的 passed 结论被 active_attempt '
'短路丢弃,导致真实成功事实被覆盖为 blocked_retryable'
)
def test_force_business_sync_uses_current_gate_version(app):
"""强制重评不得回放失效前的固定幂等 Attempt。"""
with app.app_context():