派发前的候选预留不占生图预算

This commit is contained in:
WuYongJie 2026-11-28 00:21:15 +08:00
parent 0c878d616e
commit 2a6b05fd94
4 changed files with 46 additions and 9 deletions

View File

@ -702,6 +702,7 @@ export async function openProductionOperations(host,{maxActiveDelegates=6,maxGen
};
const queueDispatch=async(slot,approval,request,signal,workerNumber)=>{
await current();
const pending=request.phase==='prepared'?await pendingNative():[];
const live=await liveSlot(slot);
signal.throwIfAborted();
const id=approval.requestId;
@ -713,9 +714,8 @@ export async function openProductionOperations(host,{maxActiveDelegates=6,maxGen
}
const status=slotDrawStatus(live);
if(status.drawLimitReached)return drawLimit(live,status);
const original=(live.candidates??[]).find(row=>!row.sourceType&&!row.reusedFrom&&
['dispatched','running','unknown'].includes(row.status));
if(original)return {slotId:slot.id,phase:'unknown',candidateId:original.id,pendingOriginal:original.id};
const original=pending.find(row=>row.slotId===slot.id);
if(original)return {...original,phase:'unknown',pendingOriginal:original.candidateId};
const sending=[...dispatchingImages].find(([,job])=>job?.slotId===slot.id);
if(sending)return {slotId:slot.id,phase:'unknown',candidateId:sending[0],pendingOriginal:sending[0]};

View File

@ -159,6 +159,26 @@ test('a host-owned original survives a Pi restart and is recovered without anoth
await assert.rejects(fs.stat(path.join(dir,'production-detail-oak.json')),{code:'ENOENT'});
});
test('a candidate reserved before POST neither owns the slot nor spends its draw budget',async t=>{
const {host}=await tailFixture(t,['hero']);
const workflowRead=host.canvas.workflowRead;
host.canvas.workflowRead=async input=>{
const view=await workflowRead(input);
return {...view,slots:view.slots.map(slot=>({...slot,generationRequestIds:[],
candidates:[{id:'reserved-before-send',status:'dispatched'}]}))};
};
const posted=[];
imageBridge(host,async(_args,id)=>{
posted.push(id);
return {id,status:'complete'};
});
const actions=await openProductionOperations(host,{maxGenerationAttempts:1});
const receipt=await generateAndWait(actions,{slotId:'hero',prompt:'本款正面'});
assert.equal(receipt.phase,'complete');
assert.deepEqual(posted,[receipt.candidateId]);
await actions.close();
});
test('six original lookups run while all six generation delegates are posting',{timeout:8000},async t=>{
const posts=Array.from({length:6},(_,index)=>'new-'+index);
const originals=Array.from({length:6},(_,index)=>'old-'+index);

View File

@ -31,6 +31,7 @@ async function fixture(t) {
const slots=pages.map((documentId,i)=>({documentId,id:`slot-${i+1}`,fieldId:`pic-${i+1}`,selectedCandidateId:'candidate'}));
const state={revision:7,overlay:true,reads:[],referenceReads:0};
const canvas={
imageRequest:async()=>[],
workflowRead:async()=>({available:true,canvasFirst:true,outputId,productVariantId:variantId,
revisions:{canvas:state.revision,creation:1},template:{contentHash:'template'},totalSlots:slots.length,slots,
productEvidence:{brief:{name:'木椅',variants:[{id:variantId,colorName:'深木色'}]}}}),

View File

@ -3036,12 +3036,25 @@ fn snapshot_for_view(dir: &Path, view: Option<Value>) -> R<Value> {
(state, history)
};
let active = state["activeVariantId"].as_str().ok_or("当前花色缺失")?;
let mut dispatched_generations = std::collections::HashSet::new();
for record in &mut history {
if !matches!(record["phase"].as_str(), Some("complete" | "failed" | "expired")) {
let task = dir.join("image-jobs").join(uuid(&record["id"], "候选")?).join("task.json");
let settled = matches!(record["phase"].as_str(), Some("complete" | "failed" | "expired"));
let generated = record["sourceType"].is_null() && record["reusedFrom"].is_null();
let id = uuid(&record["id"], "候选")?;
let dispatched = if !settled || generated && record["phase"] == "failed" {
let task = dir.join("image-jobs").join(&id).join("task.json");
if task.is_file() {
*record = reconcile_candidate(dir, &read_json(&task)?)?;
let journal = read_json(&task)?;
if !settled { *record = reconcile_candidate(dir, &journal)?; }
journal["phase"] != "prepared"
} else {
false
}
} else {
true
};
if generated && dispatched {
dispatched_generations.insert(id);
}
}
let current = creation::load(dir)?;
@ -3097,7 +3110,7 @@ fn snapshot_for_view(dir: &Path, view: Option<Value>) -> R<Value> {
let retired: Vec<_> = in_slot().filter(|record| record["retirement"].is_object())
.map(|record| &record["id"]).collect();
let generation_request_ids: Vec<_> = in_slot().filter(|record|
record["sourceType"].is_null() && record["reusedFrom"].is_null() && record["phase"] != "prepared")
record["id"].as_str().is_some_and(|id| dispatched_generations.contains(id)))
.map(|record| &record["id"]).collect();
let candidates: Vec<_> = in_slot().filter(|record| record["retirement"].is_null()).map(|record| {
let candidate_id = record["id"].as_str().unwrap_or("");
@ -3986,9 +3999,12 @@ mod tests {
write_json(&candidate_path(&fixture.dir, &id).unwrap(),
&json!({"version":2,"id":id,"binding":binding,"phase":"prepared"})).unwrap();
let file = fixture.dir.join("image-jobs").join(&id).join("task.json");
let mut task = json!({"id":id,"slotBinding":binding,"outputScope":"main_images","phase":"unknown"});
write_json(&file, &task).unwrap();
let mut task = json!({"id":id,"slotBinding":binding,"outputScope":"main_images","phase":"prepared"});
let view = json!({"activeOutput":"detail","candidates":[id]});
write_json(&file, &task).unwrap();
assert_eq!(pending_production_images(&fixture.dir, &view).unwrap(), json!([]));
task["phase"] = json!("unknown");
write_json(&file, &task).unwrap();
let originals = pending_production_images(&fixture.dir, &view).unwrap();
assert_eq!(originals[0]["candidateId"], id);
assert_eq!(originals[0]["outputId"], "main_images");