feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验
- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
1 parent
95cdfe66b0
commit
d0fe084999
225 files changed
+25923
-6653
No files matched your search
@@ -2,8 +2,9 @@
|
||||
backend/app/jobs/handlers/shipping_document_layout.py
|
||||
|
||||
用途:独立执行单份 ShippingNoticeDocument 的 MinerU 布局解析任务。
|
||||
依赖:Layout lifecycle/runtime、FileAsset、通用 Job queue。
|
||||
逻辑:按 job attempt 领取版本 lease,读取对象存储调用 MinerU;收口时旧 lease 无权覆盖新版本。
|
||||
依赖:Layout lifecycle/runtime、FileAsset、通用 Job queue、audit_service、workflow_ledger(尽力引用)。
|
||||
逻辑:按 job attempt 领取版本 lease,读取对象存储调用 MinerU;收口时旧 lease 无权覆盖新版本;
|
||||
起止与异常轻量写统一审计(若能定位 WorkflowItem 则带软引用)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -26,6 +27,69 @@ from app.services.shipping_notice.document_layout.runtime import build_mineru_ta
|
||||
LAYOUT_JOB_ERROR = 'shipping_document_layout_provider_failed'
|
||||
|
||||
|
||||
def _layout_job_audit(
|
||||
*,
|
||||
event_type: str,
|
||||
job: Job,
|
||||
version: ShippingDocumentLayoutVersion | None,
|
||||
result: str = 'success',
|
||||
error_code: str | None = None,
|
||||
metadata: dict | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
版面 Job 起止/异常轻量审计;能定位 Item 时写入 workflow 软引用。
|
||||
|
||||
Args:
|
||||
event_type: 审计事件类型。
|
||||
job: 当前 Job。
|
||||
version: 布局版本(可空)。
|
||||
result/error_code/metadata: 结果与脱敏元数据。
|
||||
"""
|
||||
try:
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
from app.services.shipping_notice.document_layout.lifecycle import _conversation_id_for_document
|
||||
from app.services.shipping_notice.workflow_ledger_service import find_item_by_root
|
||||
|
||||
document_id = int(version.document_id) if version is not None else None
|
||||
workflow_id = None
|
||||
workflow_item_id = None
|
||||
conversation_id = None
|
||||
owner_user_id = None
|
||||
if document_id:
|
||||
conversation_id = _conversation_id_for_document(document_id)
|
||||
if conversation_id:
|
||||
item = find_item_by_root(
|
||||
conversation_id=conversation_id,
|
||||
root_document_id=document_id,
|
||||
)
|
||||
if item is not None:
|
||||
workflow_id = str(item.workflow_no)
|
||||
workflow_item_id = str(item.item_no)
|
||||
owner_user_id = item.owner_user_id
|
||||
append_shipping_audit_event(
|
||||
event_type=event_type,
|
||||
aggregate_type='shipping_document_layout_version',
|
||||
aggregate_id=str(getattr(version, 'id', None) or job.id),
|
||||
actor_type='system',
|
||||
actor_id=f'job:{job.id}',
|
||||
conversation_id=conversation_id,
|
||||
document_id=document_id,
|
||||
workflow_id=workflow_id,
|
||||
workflow_item_id=workflow_item_id,
|
||||
owner_user_id=owner_user_id,
|
||||
result=result,
|
||||
error_code=error_code,
|
||||
source='shipping_document_layout',
|
||||
metadata={
|
||||
'job_id': int(job.id),
|
||||
'attempt': int(job.attempts or 0),
|
||||
**(metadata or {}),
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@register_handler(JobType.SHIPPING_DOCUMENT_LAYOUT)
|
||||
def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
"""
|
||||
@@ -56,6 +120,12 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
finish_job(job.id, result={'status': 'stale', 'skipped': True}, owned_attempt=owned_attempt)
|
||||
return
|
||||
version, lease = claimed
|
||||
_layout_job_audit(
|
||||
event_type='document_layout_job_started',
|
||||
job=job,
|
||||
version=version,
|
||||
metadata={'lease': True},
|
||||
)
|
||||
from app.services.shipping_notice.order_extraction_progress import record_layout_document_progress
|
||||
|
||||
record_layout_document_progress(document_id=int(version.document_id), event_code='layout_parsing_started')
|
||||
@@ -79,6 +149,12 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
}
|
||||
if complete_layout_version(version.id, lease_token=lease, snapshot=snapshot):
|
||||
record_layout_document_progress(document_id=int(document.id), event_code='layout_parsing_completed')
|
||||
_layout_job_audit(
|
||||
event_type='document_layout_job_completed',
|
||||
job=job,
|
||||
version=version,
|
||||
metadata={'status': str(snapshot.get('status') or 'failed')},
|
||||
)
|
||||
finish_job(
|
||||
job.id, result={'status': str(snapshot.get('status') or 'failed')},
|
||||
message='文档版面解析完成', owned_attempt=owned_attempt,
|
||||
@@ -92,6 +168,14 @@ def run_shipping_document_layout(ctx: JobContext, payload: dict) -> None:
|
||||
ctx.logger.exception(
|
||||
'shipping_document_layout_failed job_id=%s version_id=%s', job.id, version.id,
|
||||
)
|
||||
_layout_job_audit(
|
||||
event_type='document_layout_job_failed',
|
||||
job=job,
|
||||
version=version,
|
||||
result='error',
|
||||
error_code=LAYOUT_JOB_ERROR,
|
||||
metadata={'error': str(exc)[:200]},
|
||||
)
|
||||
fail_job(job.id, error=LAYOUT_JOB_ERROR, retryable=True, owned_attempt=owned_attempt)
|
||||
sync_layout_after_job_failure(job.id)
|
||||
# 仅当重试已耗尽时版本才会转 failed;此时需要唤醒等待中的 Run,
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
backend/app/jobs/handlers/shipping_order_extraction.py
|
||||
|
||||
用途:消费订单抽取 Run,使用固定模板确定性解析并持久化可复核结果。
|
||||
依赖:Job queue、冻结 Run/Plan、确定性路由和解析、统一审计服务、版面排程。
|
||||
依赖:Job queue、冻结 Run/Plan、确定性路由和解析、统一审计服务、版面排程、
|
||||
ensure_workflow_items_for_run(物化后兜底建 Item)。
|
||||
逻辑:物化附件与排版面 Job 均在主 try 内执行,失败时同步收口 Run;每个 Unit 再确定性解析或标记人工复核;不调用 Dify、Provider 或 Runtime Binding。
|
||||
"""
|
||||
|
||||
@@ -35,6 +36,7 @@ from app.services.shipping_notice.order_extraction_run_service import (
|
||||
mark_order_extraction_persistence_started,
|
||||
order_extraction_layout_prerequisite_issues,
|
||||
materialize_order_extraction_run_inputs,
|
||||
ensure_workflow_items_for_run,
|
||||
progress_ready_extraction_unit,
|
||||
schedule_order_extraction_layouts,
|
||||
transition_order_extraction_run,
|
||||
@@ -534,6 +536,8 @@ def run_shipping_order_extraction(ctx: JobContext, payload: dict) -> None:
|
||||
# [STEP 1] 邮件附件先物化成 Document,再排布局 Job;发送阶段没有 PDF Document。
|
||||
# 必须落在本 try 内:MinerU 关闭/排程失败时 ValidationError 需同步收口 Run,避免 Job 重试耗尽前 UI 一直 queued。
|
||||
materialize_order_extraction_run_inputs(run, finalize_plan=False)
|
||||
# materialize 已 ensure;此处兜底保证 EML 物化路径与幂等重入均有 Item。
|
||||
ensure_workflow_items_for_run(run, stage_when_ready='extracting')
|
||||
schedule_order_extraction_layouts(run, created_by_id=run.user_id)
|
||||
|
||||
# [STEP 2] 订单解析绝不能把仍在生成或缺失的版面快照当作“没有订单表”。
|
||||
|
||||
@@ -75,7 +75,7 @@ def claim_next(*, queues: list[str] | None = None) -> dict[str, Any] | None:
|
||||
from app.services.knowledge_cleaning.queue_service import recover_stale_running_jobs
|
||||
|
||||
recover_stale_running_jobs()
|
||||
if 'shipping_notice_run' in allowed_types or 'shipping_notice_parse' in allowed_types:
|
||||
if allowed_types & QUEUE_KB_JOB_TYPES['shipping']:
|
||||
from app.services.shipping_notice.queue_service import recover_stale_shipping_notice_jobs
|
||||
|
||||
recover_stale_shipping_notice_jobs()
|
||||
|
||||
Reference in new issue
Block a user