fix(budget): verify reservation history in ledger order and preserve output bounds
This commit is contained in:
@@ -34,7 +34,7 @@ export function checkModelDispatchBudget(store,session,run,config,body,overheadT
|
||||
}
|
||||
}
|
||||
|
||||
export const RESERVATION_METHOD='observed-route-max-v2';
|
||||
export const RESERVATION_METHOD='observed-route-max-v3';
|
||||
export const ISOLATED_OUTLIER_FACTOR=1.5,ISOLATED_OUTLIER_MIN_RECEIPTS=3;
|
||||
const members=config=>config.modelRouting?.members??[{id:null,model:config.model,protocol:config.protocol??'chat-completions',baseUrl:config.baseUrl}];
|
||||
function routeSources(member,receipts){
|
||||
@@ -53,13 +53,13 @@ function routeSources(member,receipts){
|
||||
}
|
||||
return sources;
|
||||
}
|
||||
function calibrate(local,sources,overheadTokens){
|
||||
function calibrate(local,sources,overheadTokens,outputSources=sources){
|
||||
let inputRatio=1,observedOutputTokens=local.outputTokens,observedTotalFloor=0;
|
||||
for(const source of sources){
|
||||
if(source.reportedInputTokens!==null)inputRatio=Math.max(inputRatio,source.reportedInputTokens/source.localInputTokens);
|
||||
if(source.reportedOutputTokens!==null)observedOutputTokens=Math.max(observedOutputTokens,source.reportedOutputTokens);
|
||||
observedTotalFloor=Math.max(observedTotalFloor,source.totalTokens);
|
||||
}
|
||||
for(const source of outputSources)if(source.reportedOutputTokens!==null)observedOutputTokens=Math.max(observedOutputTokens,source.reportedOutputTokens);
|
||||
return {inputRatio,observedOutputTokens,observedTotalFloor,
|
||||
reservedTokens:bounded(Math.max(local.inputTokens*inputRatio+observedOutputTokens,observedTotalFloor)+overheadTokens)};
|
||||
}
|
||||
@@ -79,16 +79,17 @@ function reservationRoutes(config,body,receipts,overheadTokens,method){
|
||||
const routes=members(config).map(member=>{
|
||||
const local=localModelEstimate(actualRequest(body,member));
|
||||
if(!count(local.inputTokens)||!count(local.outputTokens))throw new Error('模型请求输入或输出预留无效。');
|
||||
const all=routeSources(member,receipts),outlier=method===RESERVATION_METHOD?isolatedOutlier(all):null;
|
||||
const all=routeSources(member,receipts),outlier=method!=='observed-route-max-v1'?isolatedOutlier(all):null;
|
||||
const sources=outlier?all.filter((_,index)=>index!==outlier.index):all;
|
||||
const route={connectionId:member.id??null,model:member.model,protocol:member.protocol??'chat-completions',endpoint:member.baseUrl,
|
||||
localInputTokens:local.inputTokens,outputReservation:local.outputTokens,...calibrate(local,sources,overheadTokens),sources};
|
||||
if(method===RESERVATION_METHOD)route.excludedSources=outlier?[{...all[outlier.index],sourceIndex:outlier.index,reason:'isolated_input_ratio_outlier',
|
||||
localInputTokens:local.inputTokens,outputReservation:local.outputTokens,...calibrate(local,sources,overheadTokens,method===RESERVATION_METHOD?all:sources),sources};
|
||||
if(method!=='observed-route-max-v1')route.excludedSources=outlier?[{...all[outlier.index],sourceIndex:outlier.index,reason:'isolated_input_ratio_outlier',
|
||||
inputRatio:outlier.ratio,nextHighestRatio:outlier.nextRatio,factor:ISOLATED_OUTLIER_FACTOR}]:[];
|
||||
return route;
|
||||
});
|
||||
if(!routes.length)throw new Error('模型预留没有实际候选线路。');
|
||||
return {schemaVersion:1,method,overheadTokens,reservedTokens:Math.max(...routes.map(route=>route.reservedTokens)),routes};
|
||||
return {schemaVersion:1,method,overheadTokens,reservedTokens:Math.max(...routes.map(route=>route.reservedTokens)),routes,
|
||||
...(method===RESERVATION_METHOD?{receiptHistory:receipts.map(({runId,attemptId})=>({runId,attemptId}))}:{})};
|
||||
}
|
||||
|
||||
// A conservative reservation for the next dispatch, not corrected historical
|
||||
@@ -101,9 +102,13 @@ export function modelReservation(config,body,receipts=[],overheadTokens=0){
|
||||
export function legacyModelReservation(config,body,receipts=[],overheadTokens=0){
|
||||
return reservationRoutes(config,body,receipts,overheadTokens,'observed-route-max-v1');
|
||||
}
|
||||
export function legacyV2ModelReservation(config,body,receipts=[],overheadTokens=0){
|
||||
return reservationRoutes(config,body,receipts,overheadTokens,'observed-route-max-v2');
|
||||
}
|
||||
|
||||
export function verifyModelReservation(store,session,taskId,config,body,reservation){
|
||||
const compute=reservation?.method===RESERVATION_METHOD?modelReservation:reservation?.method==='observed-route-max-v1'?legacyModelReservation:null;
|
||||
const compute=reservation?.method===RESERVATION_METHOD?modelReservation:reservation?.method==='observed-route-max-v2'?legacyV2ModelReservation:
|
||||
reservation?.method==='observed-route-max-v1'?legacyModelReservation:null;
|
||||
if(reservation?.schemaVersion!==1||!compute||!Array.isArray(reservation.routes))throw new Error('模型用量预留版本或线路缺失。');
|
||||
const receipts=new Map(),ordered=route=>{
|
||||
const list=[...(route.sources??[])];
|
||||
@@ -120,7 +125,22 @@ export function verifyModelReservation(store,session,taskId,config,body,reservat
|
||||
if(!receipt)throw new Error('模型预算校准缺少原消费记录。');
|
||||
receipts.set(source.attemptId,{...receipt,runId:run.id});
|
||||
}
|
||||
const expected=compute(config,body,[...receipts.values()],reservation.overheadTokens);
|
||||
// The proof may describe an earlier request: later consumption must not make
|
||||
// that sealed snapshot stale. Recover its prefix from the original ledger,
|
||||
// never from the supplied source order or excludedSources.sourceIndex.
|
||||
const ledger=taskConsumption(store,taskId),key=value=>JSON.stringify([value.runId,value.attemptId]);
|
||||
let original;
|
||||
if(reservation.method===RESERVATION_METHOD){
|
||||
if(!Array.isArray(reservation.receiptHistory)||reservation.receiptHistory.length>ledger.length)throw new Error('模型预算预留的原消费范围不一致。');
|
||||
original=ledger.slice(0,reservation.receiptHistory.length);
|
||||
if(JSON.stringify(original.map(({runId,attemptId})=>({runId,attemptId})))!==JSON.stringify(reservation.receiptHistory))throw new Error('模型预算预留的原消费顺序不一致。');
|
||||
for(const receipt of original){const run=store.owned('run',receipt.runId,session.id);if(run.taskId!==taskId)throw new Error('模型预算校准来源不属于当前Task。');}
|
||||
}else{
|
||||
const selected=new Set([...receipts.values()].map(key));
|
||||
original=ledger.filter(receipt=>selected.has(key(receipt)));
|
||||
if(original.length!==receipts.size)throw new Error('模型预算预留的原消费范围不一致。');
|
||||
}
|
||||
const expected=compute(config,body,original,reservation.overheadTokens);
|
||||
if(JSON.stringify(expected)!==JSON.stringify(reservation))throw new Error('模型预算预留与原估算/用量/实际线路不一致。');
|
||||
return reservation;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user