fix: retain capture evidence and control failed task recovery

This commit is contained in:
abaishu0730 committed 2026-10-26 11:00:11 +08:00
1 parent c19be938fc
commit 5bae83922c
17 files changed
+333 -35

No files matched your search

+39 -19
View File
@@ -89,7 +89,7 @@ function hydrateReadonlyDocument({tables,fieldsOnly=false}){
};
for(const element of document.querySelectorAll('[hiddenexpress]')){const expression=element.getAttribute('hiddenexpress')||'';if(!hiddenPredicates.has(expression))hiddenPredicates.set(expression,expression.trim()?predicate(expression):{known:true,value:false});}
const uncertainVisibility=element=>{for(let node=element;node;node=node.parentElement){const expression=node.getAttribute('hiddenexpress');if(expression!==null&&hiddenPredicates.get(expression)?.known===false)return true;}return false;};
const blocked=()=>({blocker:'明细多行表单的行数、字段或表格关系不明确,PDF 快照已阻断;可仅采集字段供核对。'});
const blocked=(reason='AMBIGUOUS_DETAIL_STRUCTURE',table,host,extra={})=>({blocker:'明细多行表单的行数、字段或表格关系不明确,PDF 快照已阻断;可仅采集字段供核对。',diagnostics:{reason,...(table?{tableName:table.TableName,backendRows:table.Rows.length}:{}),...(host?{dynamicArea:host.getAttribute('dynamicarea'),domRows:host.rows.length}:{}),...extra}});
const currentTaskScopeError=table=>{
if(!table.Rows.length||!table.Rows.some(row=>Object.keys(row).some(column=>/^TaskID$/i.test(column))))return null;
const readTask=row=>{const columns=Object.keys(row).filter(column=>/^TaskID$/i.test(column));if(columns.length!==1)return null;const value=row[columns[0]];if(typeof value==='string'&&value.trim())return value;if(typeof value==='number'&&Number.isSafeInteger(value))return String(value);return null;};
@@ -105,29 +105,40 @@ function hydrateReadonlyDocument({tables,fieldsOnly=false}){
const selector='[xdatabind],[data-xdatabind],[data-bind]',expandedPrefixes=new Set();
for(const host of form.querySelectorAll('table[dynamicarea]')){
const ownBindings=[...host.querySelectorAll(selector)].filter(visible);if(!ownBindings.length)continue;
const marker=/^\s*(0|[1-9]\d*)\s*,\s*1\s*$/.exec(host.getAttribute('dynamicarea')||'');if(!marker)return blocked();
const start=Number(marker[1]),hostRows=[...host.rows];if(!Number.isSafeInteger(start)||start>=hostRows.length||hostRows.some(row=>[...row.cells].some(cell=>cell.rowSpan!==1)))return blocked();
const marker=/^\s*(0|[1-9]\d*)\s*,\s*1\s*$/.exec(host.getAttribute('dynamicarea')||'');if(!marker)return blocked('UNSUPPORTED_DYNAMIC_RANGE',null,host);
const start=Number(marker[1]),hostRows=[...host.rows];if(!Number.isSafeInteger(start)||start>=hostRows.length||hostRows.some(row=>[...row.cells].some(cell=>cell.rowSpan!==1)))return blocked('INVALID_TEMPLATE_ROWS',null,host);
const template=hostRows[start];
if(ownBindings.some(element=>element.closest('table')!==host||element.closest('tr')!==template||element.parentElement?.closest(selector)))return blocked();
if(ownBindings.some(element=>element.closest('table')!==host||element.closest('tr')!==template||element.parentElement?.closest(selector)))return blocked('NESTED_OR_CROSS_ROW_BINDING',null,host);
const matches=tables.filter(table=>{const prefix=((table.DataSource?table.DataSource+':':'')+table.TableName).toLowerCase();return ownBindings.every(element=>{const key=binding(element).toLowerCase();return key.startsWith(prefix+'.')&&/^[\p{L}\p{N}_]+$/u.test(key.slice(prefix.length+1));});});
if(matches.length!==1)return blocked();const table=matches[0],prefix=((table.DataSource?table.DataSource+':':'')+table.TableName).toLowerCase();
const scopeError=currentTaskScopeError(table);if(scopeError)return {blocker:scopeError};
if(expandedPrefixes.has(prefix)||bindings.filter(element=>visible(element)&&binding(element)?.toLowerCase().startsWith(prefix+'.')).some(element=>element.closest('table')!==host))return blocked();
if(matches.length!==1)return blocked('AMBIGUOUS_TABLE_BINDING',null,host);const table=matches[0],prefix=((table.DataSource?table.DataSource+':':'')+table.TableName).toLowerCase();
const scopeError=currentTaskScopeError(table);if(scopeError)return {...blocked('TASK_SCOPE_MISMATCH',table,host),blocker:scopeError};
if(expandedPrefixes.has(prefix)||bindings.filter(element=>visible(element)&&binding(element)?.toLowerCase().startsWith(prefix+'.')).some(element=>element.closest('table')!==host))return blocked('CROSS_TABLE_BINDING',table,host);
expandedPrefixes.add(prefix);
const datasource=host.getAttribute('xdatasource');
if(datasource){
// Vendor WriteToTable comes from bound columns. DataSource/Filter describe
// lookup/import, not a predicate for persisted current-order display rows.
const entries=datasource.split(';').map(part=>part.trim()).filter(Boolean),names=entries.filter(part=>/^TableName:/i.test(part));if(names.length!==1||names[0].slice(names[0].indexOf(':')+1).trim().toLowerCase()!==table.TableName.toLowerCase()||entries.some(part=>!/^TableName:[\p{L}\p{N}_]+$/iu.test(part)&&!/^Filter:[\p{L}\p{N}_]+->[\p{L}\p{N}_:.]+$/iu.test(part)))return blocked();
const entries=datasource.split(';').map(part=>part.trim()).filter(Boolean),names=entries.filter(part=>/^TableName:/i.test(part));if(names.length!==1||names[0].slice(names[0].indexOf(':')+1).trim().toLowerCase()!==table.TableName.toLowerCase()||entries.some(part=>!/^TableName:[\p{L}\p{N}_]+$/iu.test(part)&&!/^Filter:[\p{L}\p{N}_]+->[\p{L}\p{N}_:.]+$/iu.test(part)))return blocked('UNSUPPORTED_DATASOURCE',table,host);
}
const groups=new Map();for(const element of ownBindings){const key=binding(element).toLowerCase(),list=groups.get(key)||[];list.push(element);groups.set(key,list);}
const datamap=host.getAttribute('datamap');
if(datamap){const mapped=new Set();for(const part of datamap.split(';').map(part=>part.trim()).filter(Boolean)){const pair=part.split('->');if(pair.length!==2||pair[1].toLowerCase()!==prefix+'.'+pair[0].toLowerCase()||mapped.has(pair[1].toLowerCase()))return blocked();mapped.add(pair[1].toLowerCase());}if([...groups.keys()].some(key=>!mapped.has(key))||[...mapped].some(key=>!groups.has(key)))return blocked();}
for(const controls of groups.values())if(controls.length>1){const type=controls[0].type;if(!['checkbox','radio'].includes(type)||controls.some(control=>control.type!==type||!control.hasAttribute('value')||!control.value.trim())||new Set(controls.map(control=>control.value)).size!==controls.length)return blocked();}
if(datamap){
const mapped=new Set(),allBindings=[...template.querySelectorAll(selector)];
for(const part of datamap.split(';').map(part=>part.trim()).filter(Boolean)){
const pair=part.split('->'),key=pair[1]?.toLowerCase(),column=pair[0]?.toLowerCase();
if(pair.length!==2||key!==prefix+'.'+column||mapped.has(key))return blocked('INVALID_DATAMAP',table,host);
mapped.add(key);
// A mapped hidden column is legitimate only when its actual same-table
// control and backend column prove the relationship. Never infer a column.
if(!groups.has(key)&&(!allBindings.some(element=>element.closest('table')===host&&binding(element)?.toLowerCase()===key&&!visible(element))||table.Rows.some(row=>Object.keys(row).filter(name=>name.toLowerCase()===column).length!==1)))return blocked('UNPROVEN_HIDDEN_MAPPING',table,host,{fieldPaths:[key]});
}
if([...groups.keys()].some(key=>!mapped.has(key)))return blocked('VISIBLE_MAPPING_MISSING',table,host,{fieldPaths:[...groups.keys()].filter(key=>!mapped.has(key))});
}
for(const controls of groups.values())if(controls.length>1){const type=controls[0].type;if(!['checkbox','radio'].includes(type)||controls.some(control=>control.type!==type||!control.hasAttribute('value')||!control.value.trim())||new Set(controls.map(control=>control.value)).size!==controls.length)return blocked('AMBIGUOUS_CHOICE_CONTROLS',table,host);}
const rows=[];
for(const [rowIndex,row]of table.Rows.entries()){
const assignments=new Map();
for(const [key,controls]of groups){const column=key.slice(prefix.length+1),columns=Object.keys(row).filter(name=>name.toLowerCase()===column);if(columns.length!==1)return blocked();const value=row[columns[0]],item={value,rowIndex};
for(const [key,controls]of groups){const column=key.slice(prefix.length+1),columns=Object.keys(row).filter(name=>name.toLowerCase()===column);if(columns.length!==1)return blocked('MISSING_BACKEND_FIELD',table,host,{fieldPaths:[key]});const value=row[columns[0]],item={value,rowIndex};
if(controls.length>1){let selected;if(value===null||value==='')selected=[];else if(Array.isArray(value)&&controls[0].type==='checkbox'&&value.every(option=>['string','number','boolean'].includes(typeof option)))selected=value.map(String);else if(['string','number','boolean'].includes(typeof value))selected=[String(value)];else return blocked();const choices=new Set(controls.map(control=>control.value));if(new Set(selected).size!==selected.length||selected.some(option=>!choices.has(option)))return blocked();for(const control of controls)assignments.set(control,{...item,choiceSelected:selected.includes(control.value)});
}else assignments.set(controls[0],item);
}
@@ -142,16 +153,17 @@ function hydrateReadonlyDocument({tables,fieldsOnly=false}){
const prefix=((table.DataSource?table.DataSource+':':'')+table.TableName).toLowerCase(),bound=bindings.filter(element=>visible(element)&&(binding(element)?.toLowerCase().startsWith(prefix+'.')||binding(element)?.toLowerCase().startsWith(prefix+'[')));
for(const [rowIndex,row]of table.Rows.entries())for(const [column,value]of Object.entries(row)){const key=prefix+'.'+column.toLowerCase(),items=values.get(key)||[];items.push({value,rowIndex});values.set(key,items);}
if(!bound.length)continue;
const scopeError=currentTaskScopeError(table);if(scopeError)return {blocker:scopeError};
const scopeError=currentTaskScopeError(table);if(scopeError)return {...blocked('TASK_SCOPE_MISMATCH',table),blocker:scopeError};
if(bound.every(element=>resolved.has(element)))continue;
if(table.Rows.length>1){
const rows=new Map(),keys=new Set(bound.map(element=>binding(element).toLowerCase()));let container,section;
for(const element of bound){const row=element.closest('tr'),host=element.closest('table'),cell=element.closest('td,th'),key=binding(element).toLowerCase(),items=values.get(key);if(!row||!host||row.closest('table')!==host||!cell||cell.rowSpan!==1||element.parentElement?.closest('[xdatabind],[data-xdatabind],[data-bind]')||!items||items.length!==table.Rows.length)return blocked();if(container&&(container!==host||section!==row.parentElement))return blocked();container=host;section=row.parentElement;const columns=rows.get(row)||new Map();if(columns.has(key))return blocked();columns.set(key,element);rows.set(row,columns);}
if(rows.size!==table.Rows.length||detailTables.has(container))return blocked();detailTables.add(container);
if(rows.size!==table.Rows.length||detailTables.has(container))return blocked('ROW_COUNT_MISMATCH',table,container,{domRows:rows.size});detailTables.add(container);
for(const [index,columns]of [...rows.values()].entries()){if(columns.size!==keys.size||[...keys].some(key=>!columns.has(key)))return blocked();for(const [key,element]of columns)resolved.set(element,values.get(key)[index]);}
}else for(const element of bound){const items=values.get(binding(element).toLowerCase());if(items?.length===1)resolved.set(element,items[0]);}
}
for(const element of bindings.filter(visible))if(!resolved.has(element))return {blocker:uncertainVisibility(element)?'字段所在 hiddenexpress 的可见性不能安全确认,PDF 快照已阻断;不能猜测隐藏缺失字段。':'可见字段缺少已核实的后端值,PDF 快照已阻断。'};
const missing=bindings.filter(element=>visible(element)&&!resolved.has(element));
if(missing.length){const uncertain=missing.some(uncertainVisibility);return {blocker:uncertain?'字段所在 hiddenexpress 的可见性不能安全确认,PDF 快照已阻断;不能猜测隐藏缺失字段。':'可见字段缺少已核实的后端值,PDF 快照已阻断。',diagnostics:{reason:uncertain?'UNSUPPORTED_VISIBILITY':'MISSING_BACKEND_FIELD',fieldPaths:[...new Set(missing.map(binding))]}};}
const printable=(element,item)=>{if(Object.hasOwn(item||{},'choiceSelected'))return item.choiceSelected?'☑':'☐';const raw=item?.value;if(raw!==null&&typeof raw==='object')return null;const text=raw==null?'':String(raw);if(['radio','checkbox'].includes(element.type))return raw===true||text===element.value?'☑':'☐';if(element.tagName==='SELECT')return [...element.options].find(option=>option.value===text)?.textContent||text;return text;};
for(const [element,item]of resolved)if(printable(element,item)===null)return {blocker:'字段包含多个或复合值,PDF 快照已阻断。'};
const copy=document.documentElement.cloneNode(true),originals=[document.documentElement,...document.documentElement.querySelectorAll('*')],copies=[copy,...copy.querySelectorAll('*')];
@@ -168,17 +180,25 @@ function hydrateReadonlyDocument({tables,fieldsOnly=false}){
function allowedResource(value){try{const url=baseURL(value);return !url.search&&!/(?:approve|fulfill|markread|upload|delete)/i.test(url.pathname)&&/\.(?:css|png|jpe?g|gif|webp|woff2?|ttf|ico)$/i.test(url.pathname);}catch{return false;}}
async function captureReadonlyForm({storageState,detailUrl,processName,nodeName,listProcessVersion=null,fieldsOnly=false,requestFactory=options=>loadPlaywright().request.newContext(options),browserFactory=()=>loadPlaywright().chromium.launch(browserOptions()),timeout=20000}){
if(!storageState)fail('BPM 会话需要续登。',401);if(typeof nodeName!=='string'||!nodeName.trim())fail('BPM 到达记录缺少明确当前节点。');
let request,browser,context;
let request,browser,context,partialDetail;
try{
request=await requestFactory({storageState,timeout});const loaded=await loadForm(request,detailUrl,{timeout}),identity={...loaded.identity,...require('./bpm-form-contracts.cjs').resolveFormIdentity({...loaded.identity,url:loaded.url},{processName,listProcessVersion})},tables=await loadData(request,loaded.url,{timeout}),evidence=flattenTables(tables);
const capturedAt=new Date().toISOString(),detail={readOnly:true,...identity,formVersion:identity.formVersion,listProcessVersion,processInstanceId:identity.pid,nodeName,nodeSource:'list.activityName',fields:evidence.fields,fieldSources:evidence.sources,processSteps:[],capturedAt,fieldSource:'DataProvider.GetFormProcessData'};
partialDetail={...identity,detail,snapshot:null,capturedAt,detailUrl:loaded.url.href,fieldsOnly:true,blockedRequests:[]};
browser=await browserFactory();context=await browser.newContext({storageState,javaScriptEnabled:false,serviceWorkers:'block',acceptDownloads:false,permissions:[]});const images=[];
await context.route('**/*',async route=>{try{const incoming=route.request();if(incoming.method()!=='GET')return await route.abort();if(incoming.url()===loaded.url.href&&incoming.isNavigationRequest())return await route.fulfill({status:200,contentType:'text/html; charset=utf-8',body:loaded.bytes});if(incoming.isNavigationRequest()||!allowedResource(incoming.url()))return await route.abort();const response=await request.get(incoming.url(),{maxRedirects:0,timeout});if(response.status()!==200)return await route.abort();const bytes=await response.body();if(bytes.length>MAX_BYTES)return await route.abort();const contentType=(response.headers()['content-type']||'').split(';')[0];if(/^image\/(png|jpeg|gif|webp)$/.test(contentType))images.push({url:incoming.url(),data:'data:'+contentType+';base64,'+bytes.toString('base64')});return await route.fulfill({status:200,headers:{'content-type':contentType||'application/octet-stream'},body:bytes});}catch{await route.abort().catch(()=>{});}});
if(context.routeWebSocket)await context.routeWebSocket(/.*/,socket=>socket.close());const page=await context.newPage();page.on('dialog',dialog=>dialog.dismiss().catch(()=>{}));page.on('download',download=>download.cancel().catch(()=>{}));
const response=await page.goto(loaded.url.href,{waitUntil:'load',timeout});if(!response||response.status()!==200||page.url()!==loaded.url.href)fail('BPM 静态表单未能只读加载。',401);
const hydrated=await page.evaluate(hydrateReadonlyDocument,{tables,fieldsOnly});if(hydrated.blocker)fail(hydrated.blocker);if(hydrated.snapshot)hydrated.snapshot.images=images;
const capturedAt=new Date().toISOString(),detail={readOnly:true,...identity,formVersion:identity.formVersion,listProcessVersion,processInstanceId:identity.pid,nodeName,nodeSource:'list.activityName',fields:evidence.fields,fieldSources:evidence.sources,processSteps:hydrated.processSteps||[],capturedAt,fieldSource:'DataProvider.GetFormProcessData'};
const hydrated=await page.evaluate(hydrateReadonlyDocument,{tables,fieldsOnly});if(hydrated.snapshot)hydrated.snapshot.images=images;
detail.processSteps=hydrated.processSteps||[];
const evidenceHash=crypto.createHash('sha256').update(JSON.stringify(detail)).digest('hex');detail.evidenceHash=evidenceHash;
return {...identity,detail,snapshot:hydrated.snapshot||null,capturedAt,evidenceHash,detailUrl:loaded.url.href,fieldsOnly,blockedRequests:[]};
}catch(error){if(error.status)throw error;fail('BPM 只读字段或静态快照采集失败。',502);}finally{await context?.close().catch(()=>{});await browser?.close().catch(()=>{});await request?.dispose?.().catch(()=>{});}
const captured={...identity,detail,snapshot:hydrated.snapshot||null,capturedAt,evidenceHash,detailUrl:loaded.url.href,fieldsOnly,blockedRequests:[]};
if(hydrated.blocker)throw Object.assign(new Error(hydrated.blocker),{status:409,code:'FORM_STRUCTURE_UNSUPPORTED',diagnostics:hydrated.diagnostics||{reason:'UNSUPPORTED_FORM_LAYOUT'},partialDetail:{...captured,snapshot:null,fieldsOnly:true}});
return captured;
}catch(error){
const cause=error.status?error:Object.assign(new Error('BPM 只读字段或静态快照采集失败。'),{status:502});
if(partialDetail&&!cause.partialDetail){const evidenceHash=crypto.createHash('sha256').update(JSON.stringify(partialDetail.detail)).digest('hex');partialDetail.detail.evidenceHash=evidenceHash;cause.partialDetail={...partialDetail,evidenceHash};}
throw cause;
}finally{await context?.close().catch(()=>{});await browser?.close().catch(()=>{});await request?.dispose?.().catch(()=>{});}
}
module.exports={parseEntryURL,loadForm,loadData,flattenTables,hydrateReadonlyDocument,captureReadonlyForm,allowedResource};
+12 -2
View File
@@ -141,13 +141,23 @@ class BpmOrderProvider{
return this.saved(connection,principal,async storageState=>{
if(row.source!=='todo'||row.configurationOnly||row.demo)throw fail('只读执行只接受真实当前通知任务。');
const rows=await this.client.scan(storageState),same=rows.filter(fresh=>fresh.source===row.source&&String(fresh.taskId)===String(row.taskId)&&(!row.portalSource||fresh.portalSource===row.portalSource));
if(same.length!==1)throw fail('此任务已不在当前列表或来源存在歧义,请重新扫描。');
if(same.length!==1)throw Object.assign(fail('此任务已不在当前列表或来源存在歧义,请核验原通知任务后再恢复。'),{code:'BPM_TASK_NOT_CURRENT'});
const current=same[0];
if(['instanceId','processName','nodeName','processVersion','revision','receivedAt','serialNum','title'].some(key=>String(current[key]??'')!==String(row[key]??'')))throw fail('当前通知任务的节点、版本、摘要或时间已变化,请重新核对。');
const original=row.detailUrl?safeEntry(row.detailUrl):null,fresh=current.detailUrl?safeEntry(current.detailUrl):null;
if(!fresh||original&&(original.target.searchParams.get('tid')!==fresh.target.searchParams.get('tid')||original.target.searchParams.get('pid')!==fresh.target.searchParams.get('pid')))throw fail('通知任务与详情标识不一致。');
const reader=this.readonlyReader||require('../../bpm-readonly-form.cjs').captureReadonlyForm;
const captured=await reader({storageState,detailUrl:current.detailUrl,processName:current.processName,nodeName:current.nodeName,listProcessVersion:current.processVersion,fieldsOnly,browserFactory:this.browserFactory});
let captured;
try{captured=await reader({storageState,detailUrl:current.detailUrl,processName:current.processName,nodeName:current.nodeName,listProcessVersion:current.processVersion,fieldsOnly,browserFactory:this.browserFactory});}
catch(error){
if(error.partialDetail){
const partial=error.partialDetail;
const fields=Object.entries(partial.detail.fields).map(([path,value])=>({path,label:path,value}));
error.partialCapture={sample:{...row,detailUrl:fresh.target.href,connectionId:connection.id,fields:structuredClone(partial.detail.fields),brief:row.title,formVersion:partial.detail.formVersion,formPath:partial.detail.formPath,detailEvidence:{tid:partial.detail.tid,pid:partial.detail.pid,formVersion:partial.detail.formVersion,listProcessVersion:current.processVersion,readOnly:true}},snapshot:{purpose:'field-mapping',fields,sourceUrl:partial.detailUrl,processVersion:current.processVersion,formVersion:partial.detail.formVersion,formPath:partial.detail.formPath,fieldSources:partial.detail.fieldSources,capturedAt:partial.capturedAt,evidenceHash:partial.evidenceHash}};
delete error.partialDetail;
}
this.checks={...this.checks,pdfExport:false,message:error.message};throw error;
}
const fields=Object.entries(captured.detail.fields).map(([path,value])=>({path,label:path,value}));
if(!fields.length||fields.length>4000||fields.every(field=>field.value===null||field.value===''))throw fail('只读表单缺少有效字段或超过采集上限。');
const snapshot={...(captured.snapshot||{purpose:'field-mapping'}),fields,sourceUrl:captured.detailUrl,processVersion:current.processVersion,formVersion:captured.detail.formVersion,formPath:captured.detail.formPath,fieldSources:captured.detail.fieldSources,capturedAt:captured.capturedAt,evidenceHash:captured.evidenceHash};
+55 -5
View File
@@ -47,10 +47,48 @@ class OrderExecutionCoordinator{
}
validateCapture(task,connection,{sample,snapshot}){
for(const key of ['source','portalSource','instanceId','taskId','revision','processName','nodeName','receivedAt']){
if(String(sample?.[key])!==String(task.row[key]))throw Error('采集样本与待办事件身份不一致。');
if(String(sample?.[key])!==String(task.row[key]))throw Object.assign(Error('采集样本与待办事件身份不一致。'),{code:'BPM_CAPTURE_IDENTITY_MISMATCH',status:409});
}
if(sample.connectionId!==connection.id)throw Error('采集样本连接身份不一致。');
if(!snapshot||snapshot.processVersion!==sample.processVersion||!Array.isArray(snapshot.fields))throw Error('采集样本流程版本或实际字段证据不一致。');
if(sample.connectionId!==connection.id)throw Object.assign(Error('采集样本连接身份不一致。'),{code:'BPM_CAPTURE_IDENTITY_MISMATCH',status:409});
if(!snapshot||snapshot.processVersion!==sample.processVersion||!Array.isArray(snapshot.fields))throw Object.assign(Error('采集样本流程版本或实际字段证据不一致。'),{code:'BPM_CAPTURE_IDENTITY_MISMATCH',status:409});
}
captureFailure(task,connection,error){
task.errorCode=typeof error.code==='string'?error.code:'CAPTURE_FAILED';
if(error.diagnostics)task.captureDiagnostics=clone(error.diagnostics);
if(error.partialCapture){
try{
const partial=clone(error.partialCapture);this.validateCapture(task,connection,partial);
// Fields are useful diagnostic evidence, never a completed PDF snapshot.
partial.snapshot={...partial.snapshot,purpose:'field-mapping'};delete partial.snapshot.html;
task.captureEvidenceId=this.store.recordCapture({...partial,taskId:task.id}).id;
delete task.captureEvidenceError;
}catch(evidenceError){task.captureEvidenceError=evidenceError.message;}
}
task.retryCount=(task.retryCount||0)+1;
if(['FORM_STRUCTURE_UNSUPPORTED','BPM_TASK_NOT_CURRENT','BPM_CAPTURE_IDENTITY_MISMATCH'].includes(task.errorCode)||[403,409].includes(Number(error.status))){
task.retryPolicy='manual';delete task.nextRetryAt;
if(task.errorCode==='BPM_TASK_NOT_CURRENT')task.status='blocked';
}else{
task.retryPolicy='automatic';
task.nextRetryAt=new Date(+this.clock()+Math.min(1800000,60000*2**Math.min(task.retryCount-1,5))).toISOString();
}
}
resetCaptureRetry(task){delete task.retryPolicy;delete task.retryCount;delete task.nextRetryAt;delete task.errorCode;delete task.captureDiagnostics;delete task.captureEvidenceError;}
markUnavailableCaptures(rows,connection){
const absent=[];
this.store.transaction(()=>{
for(const task of this.store.all('execution-tasks')){
if(task.connectionId!==connection.id||task.step!=='capture'||!['failed','blocked'].includes(task.status)||task.sampleId||task.conflict||task.missedWindow||task.supersededBy||task.revisionIgnored)continue;
const order=task.orderId&&this.store.get('orders',task.orderId);
if(order&&(order.archive||reserved(order)||['batched','sending','sent','unknown','ignored'].includes(order.status)))continue;
if(rows.some(row=>row.source==='todo'&&taskIdentity.sameTask(task,row)))continue;
if(task.errorCode==='BPM_TASK_NOT_CURRENT'&&task.recovery?.status==='requires-history-verification')continue;
task.recovery={status:'requires-history-verification',at:this.now(),previousErrorCode:task.errorCode||null,previousError:task.error||null,reason:'完整待办对账已成功,但原通知任务不在当前待办;需核验原通知节点及历史到达证据,禁止自动补发。'};
task.sourceUnavailableAt=task.sourceUnavailableAt||this.now();task.errorCode='BPM_TASK_NOT_CURRENT';task.status='blocked';task.retryPolicy='manual';delete task.nextRetryAt;
task.error=task.recovery.reason;this.persist(task);absent.push(task);
}
});
return absent;
}
async checkArchivedRuntime(task,order,connection,principal){
const published=this.store.executionRule(this.store.get('rules',order.ruleSnapshot?.id));
@@ -239,6 +277,7 @@ class OrderExecutionCoordinator{
if(needsRuntimeCheck(order))await this.checkArchivedRuntime(task,order,connection,principal);
continue;
}
if(task.step==='capture'&&(task.retryPolicy==='manual'||input.automatic&&Date.parse(task.nextRetryAt)>+this.clock()))continue;
await this.execute(task,connection,principal,{automatic:input.automatic===true});
}finally{
this.query.processedCount++;
@@ -257,6 +296,9 @@ class OrderExecutionCoordinator{
await this.checkArchivedRuntime(task,order,connection,principal);
}
if(input.automatic&&!this.store.settings().processingEnabled)throw Error('自动处理已暂停,待处理对账保留到恢复后继续。');
// Durable discovery hints can replay an older complete list. Only a fresh,
// fully successful provider scan can establish that a retained task left.
if(trustedRows===undefined)for(const task of this.markUnavailableCaptures(rows,connection))await this.exception(task,connection,false);
this.query={...this.query,status:'complete',finishedAt:this.now(),rowCount:rows.length,targetCount:considered};this.store.put('execution-requests',{id:key,at:this.now()});
}catch(error){this.query={...this.query,status:'failed',error:error.message,finishedAt:this.now()};throw error;}
finally{
@@ -275,7 +317,12 @@ class OrderExecutionCoordinator{
task.status='capturing';task.step='capture';this.persist(task);
const {sample,snapshot}=await this.provider.capture(connection,principal,clone(task.row));
this.validateCapture(task,connection,{sample,snapshot});
const captured=this.store.recordCapture({sample,snapshot,taskId:task.id});task.sampleId=captured.id;task.step='identify';this.persist(task);
const captured=this.store.recordCapture({sample,snapshot,taskId:task.id});task.sampleId=captured.id;task.step='identify';this.resetCaptureRetry(task);
if(task.sourceUnavailableAt||task.recovery?.status==='requires-history-verification'){
task.sourceAvailabilityHistory=[...(task.sourceAvailabilityHistory||[]),{sourceUnavailableAt:task.sourceUnavailableAt||null,recovery:clone(task.recovery||null),verifiedAt:this.now()}].slice(-100);
delete task.sourceUnavailableAt;if(task.recovery?.status==='requires-history-verification')delete task.recovery;
}
this.persist(task);
}
const captured=this.store.get('capture-samples',task.sampleId);let order;
await this.refreshCapabilities(this.store.executionRule(this.store.all('rules').find(r=>task.categoryIds.includes(r.id))));
@@ -296,7 +343,9 @@ class OrderExecutionCoordinator{
await this.exception(task,connection,true);
}catch(error){
if(expectedArchive&&!require('../order-rules/migrations.cjs').archiveContextMatches(this.store,this.store.get('orders',task.orderId),expectedArchive))return;
const uncertain=task.status==='archiving';task.status=uncertain&&this.archive.supportsRecovery!==true?'unknown':'failed';task.error=uncertain?'归档结果待核验,需人工受控恢复:'+error.message:error.message;this.persist(task);await this.exception(task,connection,false);
const uncertain=task.status==='archiving';task.status=uncertain&&this.archive.supportsRecovery!==true?'unknown':'failed';task.error=uncertain?'归档结果待核验,需人工受控恢复:'+error.message:error.message;
if(task.step==='capture')this.captureFailure(task,connection,error);
this.persist(task);await this.exception(task,connection,false);
}
}
adoptRevision(task,order,captured){
@@ -401,6 +450,7 @@ class OrderExecutionCoordinator{
if(typeof input.note!=='string'||input.note.trim().length<5||input.note.length>2000)fail('请填写至少5字重试依据。');
if(!['failed','blocked'].includes(task.status)||task.conflict||task.missedWindow)fail('此任务结果未明确或不允许重试。',409);
this.store.audit(principal.user.username,'人工重试订单任务',id+':'+input.note.trim());
if(task.step==='capture')this.resetCaptureRetry(task);
await this.execute(task,connection,principal);
this.store.put('execution-requests',{id:requestId,taskId:id,inputHash,at:this.now()});
return this.snapshot(principal);
+12 -2
View File
@@ -31,11 +31,21 @@ const pick=(row,keys)=>Object.fromEntries(keys.filter(k=>row?.[k]!==undefined).m
const identity=row=>pick(row,['source','connectionId','instanceId','taskId','revision','processName','nodeName','orderNo','serialNum','receivedAt','quantity','brief','model','project','title']);
function taskEvidence(task,context,user){
if(!task)return null;
const value=pick(task,['id','version','connectionId','status','step','error','exceptionId','conflict','missedWindow','recovery','updatedAt','createdAt']);
const value=pick(task,['id','version','connectionId','status','step','error','errorCode','retryPolicy','nextRetryAt','exceptionId','conflict','missedWindow','recovery','updatedAt','createdAt']);
if(Array.isArray(task.diagnostics))value.diagnostics=task.diagnostics.slice(0,100).map(d=>pick(d,['code','reason','message','table','path','field','rowCount','expectedRows','actualRows']));
if(task.captureDiagnostics)value.captureDiagnostics=pick(task.captureDiagnostics,['reason','tableName','dynamicArea','backendRows','domRows','fieldPaths']);
const previous=(task.history||[]).findLast(h=>h.step==='capture'&&h.error&&h.error!==task.error&&['failed','blocked'].includes(h.status));
if(previous)value.previousCaptureFailure=pick(previous,['at','error']);
value.row=identity(task.row);
if(task.conflictRow){const previous=task.previousTaskId?context.orderRules.get('execution-tasks',task.previousTaskId):task;value.revisionEvidence={previous:identity(previous&&canReadConnection(context.workspace,user,previous.connectionId)?previous.row:undefined),incoming:identity(task.conflictRow)};}
return value;
}
function captureEvidence(context,task,user){
if(!task?.captureEvidenceId||!canReadConnection(context.workspace,user,task.connectionId))return undefined;
const captured=context.orderRules.get('capture-samples',task.captureEvidenceId),sample=captured?.sample;
if(captured?.provenance!=='server-provider'||captured.taskId!==task.id||sample?.connectionId!==task.connectionId||sample.taskId!==task.row?.taskId||sample.instanceId!==task.row?.instanceId)return undefined;
return {id:captured.id,readOnly:true,capturedAt:captured.capturedAt,processVersion:sample.processVersion,fields:(captured.snapshot?.fields||[]).slice(0,1000).map(f=>({...pick(f,['path','label','value']),source:pick(captured.snapshot?.fieldSources?.[f.path],['source','table','column','rowIndex'])}))};
}
function orderRow(order,task){
const event=identity(order?.event||task?.row);
return {...event,...pick(order,['id','status','categoryId','error','reason','batchId','createdAt','updatedAt','orderNo','receivedAt']),...(order?{notifications:notifications(order),...(order.ruleSnapshot?.testRunId?{testRunId:order.ruleSnapshot.testRunId}:{})}:{}),
@@ -93,7 +103,7 @@ function orderDetail(context,id,user){
const order=id.startsWith('task:')?null:context.orderRules.get('orders',id);
if(!classified(order,task)||(order?!canReadOrder(context.workspace,user,order):!task||!canReadConnection(context.workspace,user,task.connectionId)))fail('订单不存在或无权查看。',404);
const files=(order?.archive?.files||[]).map((file,index)=>({...pick(file,['name','size','sha256','kind']),index,url:'/api/order-rules/orders/'+encodeURIComponent(id)+'/files/'+index}));
return {...orderRow(order,task),event:identity(order?.event||task?.row),executionTask:taskEvidence(task,context,user),files,archivedAt:order?.archive?.archivedAt,archiveGeneration:order?.archiveGeneration||1,ruleVersion:order?.ruleSnapshot?.version,batches:[...data.batches,...require('./collecting-batches.cjs').list(context.orderRules)].filter(b=>b.orderIds?.includes(id)&&canReadBatch(context.workspace,user,b,context.orderRules)).map(b=>batchRow(context,b))};
return {...orderRow(order,task),event:identity(order?.event||task?.row),executionTask:taskEvidence(task,context,user),captureEvidence:captureEvidence(context,task,user),files,archivedAt:order?.archive?.archivedAt,archiveGeneration:order?.archiveGeneration||1,ruleVersion:order?.ruleSnapshot?.version,batches:[...data.batches,...require('./collecting-batches.cjs').list(context.orderRules)].filter(b=>b.orderIds?.includes(id)&&canReadBatch(context.workspace,user,b,context.orderRules)).map(b=>batchRow(context,b))};
}
function batchDetail(context,id,user){
const batch=id.startsWith('collecting-')?require('./collecting-batches.cjs').get(context.orderRules,id):context.orderRules.get('batches',id);