feat: complete workflow recovery and safe administration

This commit is contained in:
abaishu0730 committed 2026-10-21 12:30:11 +08:00
1 parent 1148aa6f14
commit 69583e8c33
46 files changed
+756 -120

No files matched your search

+13 -3
View File
@@ -38,8 +38,16 @@ function invalidateArchive(order){
order.archiveGeneration=(order.archiveGeneration||1)+1;
order.archive=null;
}
function archiveContext(order,task){
return {generation:order.archiveGeneration||1,sourceHash:order.sourceHash,
outputFingerprint:digest([order.event,order.ruleSnapshot]),taskId:task.id,taskVersion:task.version};
}
function archiveContextMatches(store,order,expected){
const task=store.get('execution-tasks',expected.taskId);
return !!order&&!!task&&task.orderId===order.id&&digest(archiveContext(order,task))===digest(expected);
}
function synchronizeTasks(store,order){
for(const task of store.all('execution-tasks').filter(task=>task.orderId===order.id)){
for(const task of store.all('execution-tasks').filter(task=>task.orderId===order.id&&!task.supersededBy&&!task.revisionIgnored)){
task.status=order.status;
task.step=order.archive?'complete':'identify';
task.error=null;
@@ -82,7 +90,9 @@ function apply(store,id,input,actor){
for(const item of result.items){
const order=store.get('orders',item.id);
const proposed=projection(store,order,rule);
if(proposed.archiveChanged){
// A pending intent freezes its original rule metadata. Until a receipt has
// committed, a publication migration starts a new generation for safe retry.
if(proposed.archiveChanged||!order.archive&&order.ruleSnapshot?.version!==rule.version){
invalidateArchive(order);
order.status='identified';
}
@@ -100,4 +110,4 @@ function apply(store,id,input,actor){
return result;
});
}
module.exports={preview,apply,frozen,validate,digest,invalidateArchive};
module.exports={preview,apply,frozen,validate,digest,invalidateArchive,synchronizeTasks,archiveContext,archiveContextMatches};
+14 -5
View File
@@ -139,13 +139,22 @@ class RuleStore{
if(frozen){order.status='blocked';order.blockedByBatchId=frozen.id;order.error='本接收时段的类别已冻结,新增订单须负责人核验,不能混入已冻结邮件。';this.put('orders',order);this.recordIssue('late:'+order.id,{component:'rules',error:order.error,batchId:frozen.id,orderIds:[order.id]});}
this.put('orders',order);this.audit(actor,'识别订单任务',id);return order;
});}
recordArchive(id,evidence,actor){return this.transaction(()=>{
recordArchive(id,evidence,actor,expected){return this.transaction(()=>{
if(!this.capabilities.automaticArchive)fail('自动归档接入待联调。',409);
const order=this.get('orders',id);if(!order)fail('订单任务不存在。',404);
if(!['identified','preparing'].includes(order.status))fail('当前状态不允许记录归档结果。',409);
const window=upcomingWindow(this.clock(),this.schedule(),this.calendar());
if(window&&Date.parse(order.event.receivedAt)<Date.parse(window.start))fail('该订单已超出当前时段,不再自动归档或补发。',409);
const stale=expected&&!require('./migrations.cjs').archiveContextMatches(this,order,expected);
if(!stale){
if(!['identified','preparing'].includes(order.status))fail('当前状态不允许记录归档结果。',409);
const window=upcomingWindow(this.clock(),this.schedule(),this.calendar());
if(window&&Date.parse(order.event.receivedAt)<Date.parse(window.start))fail('该订单已超出当前时段,不再自动归档或补发。',409);
}
if(!evidence||!validDate(evidence.archivedAt)||!Array.isArray(evidence.files)||!evidence.files.length||evidence.files.some(f=>!f||typeof f.name!=='string'||!f.name||/[\\/\x00-\x1f]/.test(f.name)||typeof f.path!=='string'||!path.win32.isAbsolute(f.path)||! /^[a-f0-9]{64}$/i.test(f.sha256)||!Number.isSafeInteger(f.size)||f.size<1||!['pdf','notice'].includes(f.kind)))fail('归档证据不完整。');
if(stale){
// Keep the old operation's receipt under its original context. It cannot
// satisfy a newer generation or change that generation's processing state.
order.archiveHistory=[...(order.archiveHistory||[]),{...copy(expected),evidence:copy(evidence),stale:true}];
this.put('orders',order);this.audit(actor,'保留过期归档回执',id+' generation '+expected.generation);return null;
}
if(order.status==='identified')order.status=transition('order',order.status,'prepare');
order.status=transition('order',order.status,'archive');order.archive=copy(evidence);
if(order.ruleSnapshot.attachPdf&&!evidence.files.some(f=>f.kind==='pdf')){order.status='blocked';order.error='缺少本类别要求的电子流 PDF。';}
@@ -170,7 +179,7 @@ class RuleStore{
order.event=result.event;order.ruleSnapshot=copy(rule);order.categoryId=rule.categoryId;order.trace=result;
require('./migrations.cjs').invalidateArchive(order);order.status=this.providersReady()?'identified':'blocked';
order.error=order.status==='blocked'?'待办接入、PDF 导出和自动归档待联调。':null;order.updatedAt=this.now();
this.put('orders',order);this.audit(actor,'应用当前规则重新识别',id+':'+input.note.trim());return order;
this.put('orders',order);require('./migrations.cjs').synchronizeTasks(this,order);this.audit(actor,'应用当前规则重新识别',id+':'+input.note.trim());return order;
});}
discoveryBlockers(cutoff,allowed=()=>true){
const blockers=(this.deliveryBlockers?.(allowed)||[]).map(id=>'连接 '+id+' 本地执行线索尚未交付。');