gongxiangp2/apps/detail-agent-pi/test/workflow-execute.test.mjs
WuYongJie 2b42f2c05a 清理小壹热文件里的整轮封锁、自动核查与无画布模式(R5-X)
- 普通对话里写入失败不再封锁整轮:未派发、用户接手、修订冲突的宿主原因原样作为工具回执交回,下一步由小壹决定
- 结果未知的原请求按图位留存,只挡同一图位;同图位先按原号核查或等过期,其他图位与画布编辑照常
- 删去"本轮相同操作已有原记录"拦截,花费由每图位生图上限控制
- 删去普通对话的 pi-recover 自动核查整套(后端早已不处理该动作)
- 开工不再因任意无关的未结束记录被拒
- 删去无画布模式和画布方法存在性判断,测试与 QA 脚本改用完整假画布
- 删去第二套 structured-json 工具协议
2026-11-28 21:21:57 +08:00

156 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { BridgeError } from '../src/bridge-error.mjs';
import test from 'node:test';
import assert from 'node:assert/strict';
import { executeWorkflow } from '../src/workflow-execute.mjs';
import { StdioCanvas } from '../src/stdio-canvas.mjs';
const args={version:1,intent:'generate_candidate',outputId:'detail',slotId:'hero',productVariantId:'walnut'};
function setup(phases) {
const calls=[], events=[]; let id;
const host={canvas:{run:'r',async workflowExecute(input){ calls.push(['execute',input]);id=input.operationId;return {operationId:id,phase:'submitted'}; },async workflowPoll(op){calls.push(['poll',op]);return {operationId:id,phase:phases.shift()||'submitted'};}},store:{async appendEvent(v){events.push(v)}}, async readWorkflow(input){calls.push(['read',input]);return {version:2,section:'slots'};}};
return {host,calls,events};
}
test('late prior-turn workflow receipt cannot stop the new turn and retains unknown operation identity',async()=>{
const x=setup([]);x.host.turnEpoch=1;
let release;
x.host.canvas.workflowExecute=input=>{x.calls.push(['execute',input]);return new Promise(resolve=>{release=()=>resolve({operationId:input.operationId,phase:'submitted'});});};
const task=executeWorkflow(x.host,args,undefined,{pause:async()=>{}});
await new Promise(resolve=>setImmediate(resolve));
const operationId=x.calls[0][1].operationId;
x.host.turnEpoch=2;x.host.canvas.run='new-run';
release();await assert.rejects(task,/原回合已结束/);
assert.deepEqual(x.host.canvasOperations.original,{kind:'workflow',operationId,run:'r',documentId:undefined,identity:{scope:undefined,project:undefined,accountId:undefined,origin:undefined},epoch:1,slotId:'hero',productVariantId:'walnut'});
await assert.rejects(executeWorkflow(x.host,args),/按原号核查/);
assert.equal(x.calls.length,1);
});
test('one dispatch polls matching operation and refreshes after complete',async()=>{
const x=setup(['submitted','complete']);const result=await executeWorkflow(x.host,args,undefined,{pause:async()=>{}});
assert.deepEqual(x.calls.map(c=>c[0]),['execute','poll','poll','read']);assert.equal(result.operation.phase,'complete');assert.equal(result.workflow.version,2);
assert.equal(x.calls[0][1].operationId,x.calls[1][1]);
});
for (const phase of ['submitted','complete','failed','unknown']) {
test(`a lost workflow acknowledgment is checked by its original ID (${phase})`,async()=>{
const x=setup([]);
const submit=x.host.canvas.workflowExecute;
const run=x.host.canvas.run, documentId=7;
x.host.canvas.documentId=documentId;
let resultPhase=phase;
x.host.canvas.workflowExecute=async input=>{
await submit(input); // Rust accepted the operation before the transport lost its reply.
throw new BridgeError('画布连接中断,原操作不会重发','transport_interrupted');
};
x.host.canvas.workflowPoll=async()=>assert.fail('query the original bound receipt after a lost acknowledgment');
x.host.canvas.workflowReconcile=async original=>{
const operationId=x.calls[0][1].operationId;
assert.deepEqual(original,{run,documentId,operationId});
x.calls.push(['reconcile',operationId]);
const receipt={...original,phase:resultPhase,candidateId:'candidate'};
resultPhase='complete';
if(receipt.phase==='failed') Object.assign(receipt,{message:'模型服务暂时不可用',retryableFailure:true,retryAfterSeconds:5});
return receipt;
};
const call=executeWorkflow(x.host,args,undefined,{pause:async()=>{},keepFailedCandidate:true});
if(phase==='unknown'){
await assert.rejects(call,/结果未知/);
assert.equal(x.host.canvasOperations.original.operationId,x.calls[0][1].operationId);
assert.deepEqual(x.events,[]);
}else{
const result=await call;
assert.equal(result.operation.phase,phase==='failed'?'failed':'complete');
assert.equal(result.operation.candidateId,'candidate');
assert.equal(x.events[0].acknowledgment,'reconciled');
assert.equal(x.host.canvasOperations.original,undefined);
if(phase==='failed') assert.equal(result.operation.retryAfterSeconds,5);
}
const expected=phase==='submitted'?['execute','reconcile','reconcile','read']:
phase==='complete'?['execute','reconcile','read']:['execute','reconcile'];
assert.deepEqual(x.calls.map(([kind])=>kind),expected);
});
}
test('unknown, failed and invalid receipt never redispatch; only an unsettled one stays with its slot',async()=>{
for (const phase of ['unknown','failed','bad']) {const x=setup([phase]);await assert.rejects(executeWorkflow(x.host,args,undefined,{pause:async()=>{}}));assert.equal(x.calls.filter(c=>c[0]==='execute').length,1);assert.equal(x.host.canvasOperations.original?.slotId,phase==='failed'?undefined:'hero');assert.equal(x.calls.some(c=>c[0]==='read'),false);}
});
test('a whole-page run can continue past an identified failed image, never an unknown receipt',async()=>{
const x=setup([]);
x.host.canvas.workflowPoll=async operationId=>({operationId,phase:'failed',candidateId:'original',message:'模型拒绝了该图位的请求'});
const options={pause:async()=>{},keepFailedCandidate:true};
const result=await executeWorkflow(x.host,args,undefined,options);
assert.deepEqual(result.operation,{phase:'failed',candidateId:'original',message:'模型拒绝了该图位的请求'});
assert.equal(x.host.canvasOperations.original,undefined);
x.host.canvas.workflowPoll=async operationId=>({operationId,phase:'unknown',candidateId:'next-original',message:'原请求待核查'});
await assert.rejects(executeWorkflow(x.host,{...args,slotId:'next'},undefined,options),/待核查/);
assert.equal(x.host.canvasOperations.original.slotId,'next');
assert.equal(x.calls.filter(c=>c[0]==='execute').length,2);
});
test('an upstream failure keeps its real reason instead of describing a user handover',async()=>{
const x=setup([]);
x.host.canvas.workflowPoll=async operationId=>({operationId,phase:'unknown',candidateId:'original',message:'图片服务暂时不可用(503),原请求待核查;不会重发'});
await assert.rejects(executeWorkflow(x.host,args,undefined,{pause:async()=>{}}),/503/);
assert.equal(x.calls.filter(c=>c[0]==='execute').length,1);
await assert.rejects(executeWorkflow(x.host,args),/按原号核查/);
});
test('cancellation and broken poll preserve operation and block further dispatch',async()=>{
const x=setup([]); x.host.canvas.workflowPoll=async()=>{throw Error('pipe closed')};
await assert.rejects(executeWorkflow(x.host,args,undefined,{pause:async()=>{}}));await assert.rejects(executeWorkflow(x.host,args));assert.equal(x.calls.filter(c=>c[0]==='execute').length,1);
const y=setup([]), controller=new AbortController();controller.abort();await assert.rejects(executeWorkflow(y.host,args,controller.signal));assert.equal(y.calls.length,0);
});
test('stdio transport carries session identity and operation id',async()=>{
const sent=[];const c=new StdioCanvas(async (action,input)=>{sent.push({action,input});},7);c.run='run';await c.workflowExecute({...args,operationId:'op'});await c.workflowPoll('op');assert.deepEqual(sent.map(v=>v.action),['pi-workflow-execute','pi-workflow-poll']);assert.equal(sent[1].input.run,'run');assert.equal(sent[1].input.documentId,7);assert.equal(sent[1].input.operationId,'op');
});
test('adoption obtains current revision write permission before dispatch',async()=>{
const x=setup(['complete']); x.host.canvas.read=async()=>{x.calls.push(['canvas-read']);return {revision:17,conversationActive:true,writeBlocked:false};};
x.host.canvas.requestWrite=async revision=>{x.calls.push(['permission',revision]);return {allowed:true,revision};};
await executeWorkflow(x.host,{...args,intent:'adopt_candidate',candidateId:'chosen'},undefined,{pause:async()=>{}});
assert.deepEqual(x.calls.map(c=>c[0]),['canvas-read','permission','execute','poll','read']); assert.equal(x.calls[1][1],17); assert.equal(x.host.canvasOperations.writeRequested,true);
});
test('parallel placement waits for a manual edit and requests the new revision',async()=>{
const fixture=setup(['complete']);
let reads=0,waiting=0;
fixture.host.canvas.read=async()=>({conversationActive:true,revision:++reads+10,detailWriteWaiting:reads===1});
fixture.host.canvas.requestWrite=async revision=>{
fixture.calls.push(['permission',revision]);
return revision===12?{allowed:false,retryWrite:true,revision:13}:{allowed:true,revision};
};
await executeWorkflow(fixture.host,{...args,intent:'adopt_candidate',candidateId:'chosen'},undefined,{
detailBatch:true,pause:async()=>{},onWriteWaiting:()=>{waiting++;},
});
assert.equal(waiting,2);
assert.deepEqual(fixture.calls.map(([kind])=>kind),['permission','permission','execute','poll']);
assert.deepEqual(fixture.calls.slice(0,2).map(([,revision])=>revision),[12,13]);
});
test('an unsettled original holds only its own slot; another slot dispatches normally',async()=>{
const x=setup(['unknown','complete']);
await assert.rejects(executeWorkflow(x.host,args,undefined,{pause:async()=>{}}),/结果未知/);
await assert.rejects(executeWorkflow(x.host,args),/图位 hero 的原请求 .* 结果未知/);
const other=await executeWorkflow(x.host,{...args,slotId:'side'},undefined,{pause:async()=>{}});
assert.equal(other.operation.phase,'complete');
assert.equal(x.calls.filter(c=>c[0]==='execute').length,2);
assert.equal(x.host.canvasOperations.original.slotId,'hero');
});
test('failed or stale adoption permission never dispatches',async()=>{
for (const permission of [{allowed:false,revision:17},{allowed:true,revision:18},null]) {
const x=setup(['complete']);x.host.canvas.read=async()=>({revision:17});x.host.canvas.requestWrite=async()=>permission;
await assert.rejects(executeWorkflow(x.host,{...args,intent:'adopt_candidate',candidateId:'chosen'}));
assert.equal(x.calls.length,0); assert.equal(x.host.canvasOperations.original,undefined);
}
});
test('generation never requests a write lease',async()=>{
const x=setup(['complete']);x.host.canvas.read=async()=>{throw Error('unexpected canvas read')};x.host.canvas.requestWrite=async()=>{throw Error('unexpected permission')};
await executeWorkflow(x.host,args,undefined,{pause:async()=>{}});assert.equal(x.host.canvasOperations.writeRequested,undefined);
});
test('a long image generation keeps polling the original operation after the former two-minute cutoff', async t => {
const x = setup(['submitted', 'submitted', 'complete']);
let now = 0;
t.mock.method(Date, 'now', () => now);
const result = await executeWorkflow(x.host, args, undefined, {
pause: async () => { now += 65_000; },
});
assert.equal(result.operation.phase, 'complete');
assert.deepEqual(x.calls.map(([type]) => type), ['execute', 'poll', 'poll', 'poll', 'read']);
assert.ok(x.calls.filter(([type]) => type === 'poll').every(([, id]) => id === x.calls[0][1].operationId));
});