fix: restore archived task progress and runtime gates after restart

This commit is contained in:
abaishu0730 committed 2026-10-19 13:49:33 +08:00
1 parent eb92a4f033
commit cc1ade2261
3 files changed
+102 -8

No files matched your search

+31 -2
View File
@@ -35,6 +35,35 @@ test('capture failures block the entire category rather than silently omitting a
assert.equal(f.execution.snapshot().tasks.filter(t=>t.status==='failed').length,1);
assert.equal(f.store.prepare('2026-10-19T03:00:00Z','admin').length,0);
});
test('cold restart probes archived pending work and restores dispatch gates without exporting again',async t=>{
const f=fixture(t);await configure(f);await f.execution.scan({connectionId:'c1',idempotencyKey:'archive-before-restart'},principal);
const before=f.counts();let captured=false,probed=false;
const provider={scan:f.provider.scan,capabilities:()=>({liveTodo:true,pdfExport:captured}),capture:async(...args)=>{captured=true;return f.provider.capture(...args);}};
const archive={...f.archive,capabilities:()=>({automaticArchive:probed,pdfExport:probed}),probe:async rule=>{assert.equal(rule.id,'bs-change');probed=true;return {automaticArchive:true,pdfExport:true};}};
const execution=new OrderExecutionCoordinator({...f.execution,provider,archive});
assert.equal(execution.snapshot().capabilities.pdfExport,false);
await execution.scan({connectionId:'c1',idempotencyKey:'cold-restart'},principal);
assert.deepEqual(execution.snapshot().capabilities,{liveTodo:true,pdfExport:true,automaticArchive:true});assert.equal(captured,true);assert.equal(probed,true);assert.equal(f.counts().exports,before.exports);assert.equal(f.store.all('orders')[0].status,'pending');
await execution.stop();
f.store.prepare('2026-10-19T03:00:00Z','admin');captured=false;probed=false;
const batchedRestart=new OrderExecutionCoordinator({...f.execution,provider,archive});await batchedRestart.scan({connectionId:'c1',idempotencyKey:'cold-batched-restart'},principal);
assert.equal(batchedRestart.snapshot().tasks[0].status,'batched');assert.equal(f.store.providersReady(),true);assert.equal(f.counts().exports,before.exports);assert.equal(captured,true);assert.equal(probed,true);await batchedRestart.stop();
});
for(const supportsRecovery of [false,true])test(`committed archive receipt reconciles interrupted task before uncertainty classification (recovery=${supportsRecovery})`,async t=>{
const f=fixture(t);await configure(f);await f.execution.scan({connectionId:'c1',idempotencyKey:'archived'},principal);
const task=f.store.all('execution-tasks')[0],order=f.store.get('orders',task.orderId),before=f.counts();task.status='archiving';task.step='archive';task.error=null;f.store.put('execution-tasks',task);
const archive={...f.archive,supportsRecovery};const execution=new OrderExecutionCoordinator({...f.execution,archive});
assert.equal(execution.snapshot().tasks[0].status,'pending');assert.equal(execution.snapshot().tasks[0].step,'complete');
await execution.scan({connectionId:'c1',idempotencyKey:'reconciled'},principal);assert.equal(f.counts().exports,before.exports);assert.deepEqual(f.store.get('orders',order.id).archive,order.archive);assert.equal(f.store.prepare('2026-10-19T03:00:00Z','admin').length,1);
await execution.stop();
});
test('unavailable original source leaves archived work blocked without replay or discarding its receipt',async t=>{
const f=fixture(t);await configure(f);await f.execution.scan({connectionId:'c1',idempotencyKey:'archived'},principal);
const before=f.counts(),receipt=f.store.all('orders')[0].archive;
const provider={scan:async()=>[],capabilities:()=>({liveTodo:true,pdfExport:false}),capture:async()=>{throw Error('原任务已不在已核验节点');}};
const execution=new OrderExecutionCoordinator({...f.execution,provider});await execution.scan({connectionId:'c1',idempotencyKey:'source-unavailable'},principal);
assert.equal(f.store.providersReady(),false);assert.equal(execution.snapshot().tasks[0].runtimeCheck.status,'blocked');assert.match(execution.snapshot().tasks[0].error,/原任务已不在/);assert.equal(f.counts().exports,before.exports);assert.deepEqual(f.store.all('orders')[0].archive,receipt);assert.equal(f.store.prepare('2026-10-19T03:00:00Z','admin').length,0);await execution.stop();
});
test('failed frozen batch resend is versioned admin-only idempotent and cannot replay unknown',async t=>{
const f=fixture(t);await configure(f);await f.execution.scan({connectionId:'c1',idempotencyKey:'second'},principal);
f.store.saveSettings({...f.store.settings(),channelId:'channel1'},'admin');const batch=f.store.prepare('2026-10-19T03:00:00Z','admin')[0];f.store.finishSending(batch.id,'failed','SMTP verify failed');
@@ -84,9 +113,9 @@ test('interrupted archive recovery is explicitly retried and never silently repl
f.archive.archive=archive;await f.execution.scan({connectionId:'c1',idempotencyKey:'third'},principal);assert.equal(f.counts().exports,0);
await f.execution.retry(task.id,{version:task.version,idempotencyKey:'retry',note:'已核对归档幂等凭据'},principal);
assert.equal(f.execution.snapshot().tasks[0].status,'pending');assert.equal(f.counts().exports,1);
const running=f.store.get('execution-tasks',task.id);running.status='archiving';f.store.put('execution-tasks',running);
const running=f.store.get('execution-tasks',task.id);running.status='archiving';running.step='archive';f.store.put('execution-tasks',running);
const restarted=new OrderExecutionCoordinator({...f.execution,store:f.store,workspace:f.execution.workspace,provider:f.provider,archive:f.archive});
assert.equal(restarted.snapshot().tasks[0].status,'failed');await restarted.stop();
assert.equal(restarted.snapshot().tasks[0].status,'pending');await restarted.stop();
});
test('unknown send first requires recorded verification; only then frozen manual resend is reachable',async t=>{
const f=fixture(t);await configure(f);await f.execution.scan({connectionId:'c1',idempotencyKey:'second'},principal);