fix(budget): verify reservation history in ledger order and preserve output bounds
This commit is contained in:
@@ -21,3 +21,9 @@ W六请求71044 tokens、0提案0交付是事实;末轮新增468字符、还
|
||||
## 执行记录与阶段回顾
|
||||
|
||||
起步工作树干净,独立复核相关49/49已通过。后续检查结果按阶段追加。
|
||||
|
||||
### 预留阶段
|
||||
|
||||
新observed-route-max-v3封存receiptHistory,按原Task账本恢复计算时消费前缀,不能用自报sourceIndex决定历史顺序;原v2的核验也改用原账本顺序。后续真实消费不会使早先封存失效。输入比率异常仅影响输入校准及其总量floor,已报告输出最大值继续参与预留。旧v1/v2算法单独保留,原R暂停与v2重算不重写。
|
||||
|
||||
专项13/13、task-context/budget-resume/engine-context/controlled-engine相关24/24通过。包含真实本机HTTP夹具和原R封存;重排最新高值/改历史/重复来源拒绝、已报9000输出保留、后来新增消费后旧证明通过。证据.qa/reliability-1.25.2/reservation.tap及reservation-related.tap。此为本地修复证据,尚不证明真实模型交付或异常计量根因。
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- 用户授权后从干净532b9d60开始。复用独立复核三个反例,正式运行保持不变。
|
||||
- 已读AGENTS及复核;采用planning-with-files,session-catchup无待同步输出。首次多文件补丁上下文失败且无变化,随后按真实标题修正。
|
||||
- 当前阶段:预留来源顺序修复;后续安装变化核验、混合进展及吞吐、发行验收、新真实实验,各阶段验证/回顾/本地提交。
|
||||
- 预留阶段:v3原消费前缀+原账本顺序,v1/v2保持历史算法;输入异常不丢输出最大值。专项13/13、上下文/预算/引擎24/24通过,原R保持72571及原暂停。已补阶段回顾,按范围本地提交。一次读取不存在src/core/state路径的rg失败,改读真实src/server/store.mjs。
|
||||
|
||||
## 1.25接续
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@
|
||||
|
||||
用户在独立复核后授权全量修复、开发、验证与本地提交。基线532b9d60干净;采用planning-with-files,先解决已复现问题,再新冻真实样本。正式PID/Provider/Key保持,不委派、不推送、不续跑旧样本。原失败和未知不改成通过。
|
||||
|
||||
1. [in_progress] 预留证明绑定原回执顺序,拒绝重排/重复/跨Task,保留历史v1/v2原证据;核对输出异常与输入异常的处理。
|
||||
2. [pending] 恢复全清单扫描后普通文件的归属/硬链接变化核验,保持单次realpath提速;补时间边界回归与性能证据。
|
||||
1. [complete] 预留v3绑定原账本消费前缀,拒绝重排/重复/跨Task;保留v1/v2算法和原封存核验,输入异常不再丢弃已报告输出上界。专项13/13、上下文/预算/引擎相关24/24通过。
|
||||
2. [in_progress] 恢复全清单扫描后普通文件的归属/硬链接变化核验,保持单次realpath提速;补时间边界回归与性能证据。
|
||||
3. [pending] 修复混合进展读取停止,保留真正重复/相同检查/相同提案的有界停止;核对实际请求中读页与检查事实并提高可用分页吞吐。
|
||||
4. [pending] 完整相关/全量/构建与实际界面、不可变发行及隔离安装验收;保留状态升级1.25.2,阶段回顾和本地提交。
|
||||
5. [pending] 新冻结单次诊断/身份预检/有界Node;通过后才React/Python及Purge AB/BA,分别报告失败、成本和因果边界,不循环重试求通过。
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import {modelReservation,legacyModelReservation,localModelEstimate,verifyModelReservation} from '../src/shared/contracts/model-reservation.mjs';
|
||||
import {modelReservation,legacyModelReservation,legacyV2ModelReservation,localModelEstimate,verifyModelReservation} from '../src/shared/contracts/model-reservation.mjs';
|
||||
import {completionBody} from '../src/shared/contracts/model-request.mjs';
|
||||
import {Provider} from '../src/server/provider.mjs';
|
||||
import {Runtime} from '../src/server/runtime.mjs';
|
||||
@@ -31,7 +31,7 @@ test('reservation uses actual known usage from the same route, never lowers esti
|
||||
|
||||
test('reservation verification binds original Task/Run consumption and rejects changed usage or foreign provenance',()=>{
|
||||
const receipt=observed(),run={id:'owned-run',taskId:'owned-task',sessionId:'owned-session',consumption:[receipt]},session={id:'owned-session'};
|
||||
const store={owned(kind,id,sessionId){assert.equal(kind,'run');assert.equal(id,run.id);assert.equal(sessionId,run.sessionId);return run;}};
|
||||
const store={core:{run:new Map([[run.id,run]])},owned(kind,id,sessionId){assert.equal(kind,'run');assert.equal(id,run.id);assert.equal(sessionId,run.sessionId);return run;}};
|
||||
const reservation=modelReservation(config,body,[receipt],128);
|
||||
assert.deepEqual(verifyModelReservation(store,session,'owned-task',config,body,reservation),reservation);
|
||||
const altered=structuredClone(reservation);altered.routes[0].inputRatio=1;
|
||||
@@ -129,26 +129,28 @@ function rEvidence(){
|
||||
return {proof,route,receipts,config,body:rJson('node/captures/704d88f7-fbef-4507-9597-22cf79481961-request.json')};
|
||||
}
|
||||
const rStore=(proof,receipts)=>{const run={id:proof.pause.runId,taskId:proof.pause.taskId,sessionId:'r-session',consumption:receipts};
|
||||
return {session:{id:'r-session'},store:{owned(kind,id,sessionId){assert.equal(id,run.id);assert.equal(sessionId,run.sessionId);return run;}}};};
|
||||
return {session:{id:'r-session'},store:{core:{run:new Map([[run.id,run]])},owned(kind,id,sessionId){assert.equal(id,run.id);assert.equal(sessionId,run.sessionId);return run;}}};};
|
||||
|
||||
test('original R pause stays verifiable under v1 while v2 excludes only the single isolated outlier and keeps the ledger intact',()=>{
|
||||
test('original R v1/v2 stay verifiable while v3 excludes the isolated input outlier and keeps the ledger intact',()=>{
|
||||
const {proof,route,receipts,config,body}=rEvidence(),original=structuredClone(receipts),{store,session}=rStore(proof,receipts);
|
||||
const legacy=legacyModelReservation(config,body,receipts);
|
||||
assert.equal(legacy.reservedTokens,29623);assert.deepEqual(legacy.routes[0],route);
|
||||
assert.deepEqual(verifyModelReservation(store,session,proof.pause.taskId,config,body,proof.pause.tokenReservation),proof.pause.tokenReservation);
|
||||
const current=modelReservation(config,body,receipts);
|
||||
assert.equal(current.method,'observed-route-max-v2');assert.equal(current.reservedTokens,15285);
|
||||
assert.equal(current.method,'observed-route-max-v3');assert.equal(current.reservedTokens,15285);
|
||||
assert.ok(current.reservedTokens<=proof.pause.remainingTokens);assert.ok(current.reservedTokens>=12352+2048);
|
||||
const [excluded]=current.routes[0].excludedSources;assert.equal(current.routes[0].excludedSources.length,1);
|
||||
assert.equal(excluded.attemptId,'23226d1c-9ad6-4daa-81f5-1b44f0d57ecc');assert.equal(excluded.reason,'isolated_input_ratio_outlier');
|
||||
assert.equal(current.routes[0].sources.length,5);assert.equal(current.routes[0].observedTotalFloor,13107);
|
||||
assert.deepEqual(verifyModelReservation(store,session,proof.pause.taskId,config,body,current),current);
|
||||
const v2=legacyV2ModelReservation(config,body,receipts);
|
||||
assert.deepEqual(verifyModelReservation(store,session,proof.pause.taskId,config,body,v2),v2);
|
||||
const forged=structuredClone(current);forged.routes[0].excludedSources=[];forged.routes[0].sources.push(excluded);
|
||||
assert.throws(()=>verifyModelReservation(store,session,proof.pause.taskId,config,body,forged),/不一致/);
|
||||
assert.deepEqual(receipts,original);assert.equal(proof.pause.runUsage.totalTokens,72571);
|
||||
});
|
||||
|
||||
test('v2 never excludes a repeated high ratio, the latest receipt, or a short history',()=>{
|
||||
test('v3 never excludes a repeated high ratio, the latest receipt, or a short history',()=>{
|
||||
const {receipts,config,body}=rEvidence(),high=receipts[3];
|
||||
const repeated=[...receipts,{...high,attemptId:'r-repeat'}];
|
||||
assert.equal(modelReservation(config,body,repeated).routes[0].excludedSources.length,0);
|
||||
@@ -159,3 +161,34 @@ test('v2 never excludes a repeated high ratio, the latest receipt, or a short hi
|
||||
const short=[receipts[0],receipts[3]];
|
||||
assert.equal(modelReservation(config,body,short).routes[0].excludedSources.length,0);
|
||||
});
|
||||
|
||||
test('reservation proofs reject reordered latest high receipts, altered history and duplicates while old snapshots survive later consumption',()=>{
|
||||
const {proof,receipts,config,body}=rEvidence(),latest=[receipts[0],receipts[1],receipts[3]],{store,session}=rStore(proof,latest);
|
||||
const verify=value=>verifyModelReservation(store,session,proof.pause.taskId,config,body,value);
|
||||
const honest=modelReservation(config,body,latest);
|
||||
assert.equal(honest.routes[0].excludedSources.length,0);
|
||||
for(const compute of [modelReservation,legacyV2ModelReservation]){
|
||||
const reordered=compute(config,body,[latest[0],latest[2],latest[1]]);
|
||||
assert.equal(reordered.routes[0].excludedSources[0].attemptId,latest[2].attemptId);
|
||||
assert.throws(()=>verify(reordered),/不一致/);
|
||||
}
|
||||
const altered=structuredClone(honest);altered.receiptHistory.reverse();assert.throws(()=>verify(altered),/不一致/);
|
||||
const duplicate=structuredClone(honest);duplicate.routes[0].sources.push(duplicate.routes[0].sources[0]);assert.throws(()=>verify(duplicate),/不一致/);
|
||||
latest.push({...receipts[2],attemptId:'later-receipt'});
|
||||
assert.deepEqual(verify(honest),honest,'A later response must not rewrite an earlier reservation.');
|
||||
const run=store.core.run.values().next().value;
|
||||
run.consumption[0]={...run.consumption[0],usage:{prompt_tokens:1,completion_tokens:1,total_tokens:2}};
|
||||
assert.throws(()=>verify(honest),/不一致/);
|
||||
});
|
||||
|
||||
test('input-ratio exclusion preserves independently reported output maxima without rewriting historical v2',()=>{
|
||||
const {receipts,config,body,proof}=rEvidence(),high={...receipts[3],usage:{prompt_tokens:27164,completion_tokens:9000,total_tokens:36164}};
|
||||
const ledger=[receipts[0],high,receipts[1]],original=structuredClone(ledger),current=modelReservation(config,body,ledger);
|
||||
assert.equal(current.routes[0].excludedSources[0].attemptId,high.attemptId);
|
||||
assert.equal(current.routes[0].observedOutputTokens,9000);
|
||||
assert.ok(current.reservedTokens>=current.routes[0].localInputTokens+9000);
|
||||
assert.equal(legacyV2ModelReservation(config,body,ledger).routes[0].observedOutputTokens,2048);
|
||||
const {store,session}=rStore(proof,ledger);
|
||||
assert.deepEqual(verifyModelReservation(store,session,proof.pause.taskId,config,body,current),current);
|
||||
assert.deepEqual(ledger,original);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user