fix: bind observed models and calibrate every model dispatch budget
This commit is contained in:
@@ -17,6 +17,7 @@ export function BudgetPausePanel({session,config,onSession,onRecovery}:{session:
|
||||
const [selection,setSelection]=useState(''),[options,setOptions]=useState<{value:string;name:string}[]>([]);
|
||||
const run=session.execution?.run,pause=run?.budgetPause,reason=pause?.reason??run?.stopReason??'';
|
||||
const controlled=session.execution?.task?.origin==='isolated-worker'||run?.engine==='deepseek-workbench';
|
||||
const calibrationSources=new Set(pause?.tokenReservation?.routes.flatMap(route=>route.sources.map(source=>source.attemptId))??[]).size;
|
||||
if(!['paused','failed'].includes(session.status)||!reasons[reason])return null;
|
||||
async function start(){setOpen(true);setError('');setPlan(null);try{const center=await api<{selected:{type:string;id:string};connections:{id:string;name:string;contextLimit:number}[];pools:{id:string;name:string}[]}>('/model-center');setSelection(center.selected.type+':'+center.selected.id);setOptions([...center.connections.map(row=>({value:(row.id==='legacy-default'?'legacy':'connection')+':'+row.id,name:row.name+' · '+row.contextLimit.toLocaleString()+' 上下文'})),...center.pools.map(row=>({value:'pool:'+row.id,name:row.name+' · 按成员最小容量'}))]);}catch(cause){setError((cause as Error).message);}}
|
||||
async function preview(){setBusy(true);setError('');setPlan(null);try{const [type,id]=selection.split(':');const value=await api<Preview>('/sessions/'+session.id+'/resume-preview',{...fields,...(selection?{selection:{type,id}}:{})});setPlan(value);onSession(await api<Session>('/sessions/'+session.id));}catch(cause){setError((cause as Error).message);}finally{setBusy(false);}}
|
||||
@@ -25,6 +26,9 @@ export function BudgetPausePanel({session,config,onSession,onRecovery}:{session:
|
||||
{pause?.originalInputTokens!==undefined&&<p>原生完整历史估算 {pause.originalInputTokens.toLocaleString()} tokens。原始输入、历史索引与拒绝正文已保存为任务工件,本次拒绝没有发送模型请求。</p>}
|
||||
{!!pause?.transportReservationTokens&&<p>准备原生任务时另留 {pause.transportReservationTokens.toLocaleString()} tokens 给引擎提示和消息封装;实际原生请求仍单独核对容量。</p>}
|
||||
{!!pause?.imageReservationTokens&&<p>图像使用单独的本地预留,属于容量估算;实际计费与服务报告用量分列。</p>}
|
||||
{pause?.modelFailure&&<p>本次模型原错误:{pause.modelFailure.message}{pause.modelFailure.upstreamRequestId?<> · 上游请求 ID:{pause.modelFailure.upstreamRequestId}</>:null}。原响应和请求诊断仍可核对。</p>}
|
||||
{pause?.tokenReservation&&<p>{calibrationSources?<>依据同一模型线路的 {calibrationSources} 次已知响应校准,</>:'按本地估算,'}下一次请求需预留 <strong>{pause.tokenReservation.reservedTokens.toLocaleString()}</strong> tokens{pause.remainingTokens!==undefined?<>,剩余 <strong>{pause.remainingTokens.toLocaleString()}</strong></>:null}。已在发送前暂停;这项预留用于后续预算,原始用量记录保持。</p>}
|
||||
{pause?.tokenAccounting&&<><dl className="budget-facts"><dt>本次运行上游报告用量</dt><dd>{pause.tokenAccounting.run.reportedTokens.toLocaleString()}</dd><dt>用量未知请求 / 本地预留</dt><dd>{pause.tokenAccounting.run.unknownRequests.toLocaleString()} / {pause.tokenAccounting.run.estimatedUnknownTokens.toLocaleString()}</dd><dt>本次运行预算 / 记账超出</dt><dd>{pause.tokenAccounting.run.maximumTokens>0?pause.tokenAccounting.run.maximumTokens.toLocaleString():'不限'} / {pause.tokenAccounting.run.exceededTokens.toLocaleString()}</dd><dt>任务累计预算 / 记账超出</dt><dd>{pause.tokenAccounting.task.maximumTokens>0?pause.tokenAccounting.task.maximumTokens.toLocaleString():'不限'} / {pause.tokenAccounting.task.exceededTokens.toLocaleString()}</dd></dl><p>单个在途响应可能超过预估。超额后停止后续请求;用量未知的请求仍按本地预留控制预算,不补造实际计费。</p></>}
|
||||
{controlled?<><p>受限子任务在隔离协作中处理。当前协作沿用已封存容量与累计预算;若配置不足,先保留现场,再为新目标选择合适配置。已完成工具和原失败不会自动重放。</p>{onRecovery&&<button type="button" className="button secondary" onClick={onRecovery}>打开隔离协作</button>}</>:<button type="button" className="button secondary" onClick={()=>void start()}><RotateCw size={14}/>核对预算并续接原任务</button>}
|
||||
{open&&<Modal title="续接原任务" description="先核对新Run的容量与预算,再确认执行。" onClose={()=>{if(!busy)setOpen(false);}} wide><p className="input-sha">Task {session.execution?.task?.id}<br/>原 Run {run?.id}</p><div className="budget-resume-fields"><label>本次模型连接或池<select aria-label="续接模型连接" value={selection} disabled={busy} onChange={event=>{setSelection(event.target.value);setPlan(null);}}>{options.map(row=><option key={row.value} value={row.value}>{row.name}</option>)}</select></label>{([['contextLimit','上下文窗口',1000,200000],['maxOutputTokens','响应预留',256,16000],['maxRunTokens','新Run累计tokens',1000,2000000],['maxTaskTokens','原任务累计tokens',1000,10000000],['maxSteps','新Run工具轮次',1,30],['maxRunDurationMs','新Run时间(分钟)',1,60]] as const).map(([key,label,min,max])=><label key={key}>{label}<input aria-label={label} type="number" min={min} max={max} value={key==='maxRunDurationMs'?fields[key]/60000:fields[key]} disabled={busy} onChange={event=>{setFields({...fields,[key]:Number(event.target.value)*(key==='maxRunDurationMs'?60000:1)});setPlan(null);}}/></label>)}</div><p>容量须符合服务实际限制;池按成员最小容量核对。新增预算只用于这次明确续接,不修改全局设置。</p>{error&&<p className="budget-error" role="alert">{error}</p>}<div className="modal-actions"><button type="button" className="button secondary" disabled={busy||!selection} onClick={()=>void preview()}>准备续接预览</button></div>{plan&&<div className="budget-preview" role="status"><strong>将创建原Task的新Run</strong><p>可用模型 {plan.eligibleConnections.length?plan.eligibleConnections.map(row=>row.name+' / '+row.model).join('、'):plan.config.model} · 上下文 {plan.budget.contextLimit.toLocaleString()} · 可用输入 {plan.budget.inputBudget.toLocaleString()} tokens</p><p>保留 {plan.completedToolIds.length} 项完成工具、{plan.attachmentIds.length} 张附件;{plan.unknownToolIds.length} 项未知结果仍须核对。</p><p>{plan.note}</p><p className="input-sha">预览 SHA-256 {plan.sha256}</p><div className="modal-actions"><button type="button" className="button primary" disabled={busy} onClick={()=>void confirm()}>确认预算并续接</button></div></div>}</Modal>}
|
||||
</aside>;
|
||||
|
||||
@@ -47,6 +47,9 @@ export interface InputAttachment { id:string;name:string;status:string;mediaType
|
||||
export interface BudgetPause { reason:string;requiredInputTokens?:number;contextLimit?:number;inputBudget?:number;outputReservation?:number;safetyTokens?:number;runRemainingTokens?:number|null;taskRemainingTokens?:number|null;imageReservationTokens?:number;breakdown?:Record<string,number> }
|
||||
export interface Run { budgetPause?:BudgetPause }
|
||||
export interface BudgetPause { originalInputTokens?:number;transportReservationTokens?:number }
|
||||
export interface BudgetPause { modelFailure?:RuntimeFailure }
|
||||
export interface TokenBudgetCounter { maximumTokens:number;reportedTokens:number;unknownRequests:number;estimatedUnknownTokens:number;accountedTokens:number;remainingTokens:number|null;exceededTokens:number }
|
||||
export interface BudgetPause { remainingTokens?:number;tokenReservation?:{schemaVersion:number;reservedTokens:number;routes:{sources:{runId:string;attemptId:string}[]}[]};tokenAccounting?:{schemaVersion:number;singleInFlightResponseMayExceed:boolean;run:TokenBudgetCounter;task:TokenBudgetCounter} }
|
||||
export interface ToolEvent {
|
||||
id: string; kind: string; label: string; phase: string; createdAt: string; tool?: string;
|
||||
input?: Record<string, unknown>; output?: string; durationMs?: number; startedAt?: string; finishedAt?: string;
|
||||
|
||||
@@ -6,6 +6,7 @@ import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from
|
||||
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
|
||||
import { skillCatalogMessage, SKILL_LIMITS } from '../skills/skill-registry.mjs';
|
||||
import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs';
|
||||
import { modelReservation,taskConsumption,verifyModelReservation } from '../../shared/contracts/model-reservation.mjs';
|
||||
|
||||
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex');
|
||||
const PLACEHOLDER_ID = '00000000-0000-0000-0000-000000000000';
|
||||
@@ -158,7 +159,7 @@ export class RequestPlanner {
|
||||
return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length };
|
||||
}
|
||||
|
||||
prepare(session, run, config, { system, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0 }) {
|
||||
prepare(session, run, config, { system, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0,engineStepOnly=false }) {
|
||||
const task = this.store.owned('task', run.taskId, session.id);
|
||||
this.store.owned('run', run.id, session.id);
|
||||
const groups = historyGroups(session.providerMessages), budget = budgetFor(config, run, task, transportReservationTokens);
|
||||
@@ -306,12 +307,17 @@ export class RequestPlanner {
|
||||
}
|
||||
const body = completionBody(config, messages, tools), metrics = requestMetrics(body);
|
||||
if (metrics.estimatedInputTokens > budget.inputBudget) throw pause(budget, metrics, breakdown);
|
||||
const tokenReservation=engineStepOnly?null:modelReservation(config,body,taskConsumption(this.store,task.id));
|
||||
const runCredit=budget.runRemainingTokens??Infinity,taskCredit=budget.taskRemainingTokens??Infinity,credit=Math.min(runCredit,taskCredit);
|
||||
if(tokenReservation&&tokenReservation.reservedTokens>credit)throw pause({...budget,limitingReason:runCredit<=taskCredit?'token_limit':'task_token_limit',
|
||||
tokenReservation,remainingTokens:credit},metrics,breakdown);
|
||||
const requestArtifact = this.artifact(session, run, body, 'model-request');
|
||||
const record = { schemaVersion: 1, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run.taskId,
|
||||
runId: run.id, stepId, requestIndex: [...this.store.core.context_plan.values()].filter(plan => plan.runId === run.id).length + 1,
|
||||
status: 'prepared', createdAt: new Date().toISOString(), finishedAt: null,
|
||||
requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens,
|
||||
requestArtifactId: requestArtifact.id,
|
||||
...(tokenReservation?{tokenReservation,maxBudgetAttempts:Math.min(8,Math.floor(credit/tokenReservation.reservedTokens))}:{engineStepOnly:true}),
|
||||
estimateMethod: metrics.estimateMethod, beforeInputTokens: before.estimatedInputTokens, beforeBytes: before.bytes, budget,
|
||||
...(metrics.imageCount?{imageCount:metrics.imageCount,imageReservationTokens:metrics.imageReservationTokens,imageEstimate:metrics.imageEstimate,
|
||||
inputImages:config.inputImages.map(({dataUrl,...identity})=>identity)}:{}),
|
||||
@@ -360,11 +366,14 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
};
|
||||
const request = get(plan.requestArtifactId, 'model-request', plan.requestSha256);
|
||||
const body = JSON.parse(store.artifactStore.read(request)), metrics = requestMetrics(body);
|
||||
if(plan.tokenReservation)verifyModelReservation(store,session,plan.taskId,{...store.owned('run',plan.runId,session.id).model,
|
||||
modelRouting:store.owned('run',plan.runId,session.id).model.routing},body,plan.tokenReservation);
|
||||
if (metrics.bytes !== plan.requestBytes || metrics.estimatedInputTokens !== plan.estimatedInputTokens
|
||||
|| requestVariants(body).some(value => outputReservation(value) !== plan.budget.outputReservation) || metrics.estimatedInputTokens > plan.budget.inputBudget) throw new Error('请求预算与原正文不匹配。');
|
||||
historyGroups(requestHistory(body));
|
||||
for (const variant of requestVariants(body)) historyGroups(requestHistory(variant));
|
||||
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
|
||||
for(const key of ['tokenReservation','maxBudgetAttempts'])if(JSON.stringify(frozen[key]??null)!==JSON.stringify(plan[key]??null))throw new Error('模型用量预留与发送前封存不一致。');
|
||||
for(const key of ['imageCount','imageReservationTokens','imageEstimate','inputImages']) {
|
||||
if(JSON.stringify(frozen[key]??null)!==JSON.stringify(plan[key]??null))throw new Error('图片身份或预算与发送前封存不匹配。');
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { resolveControlledInstallation, assertControlledInstallation, controlled
|
||||
import { engineRunInput, engineRunReport, verifyEngineInput, nativeMessagesToChat } from '../../shared/contracts/engine-execution.mjs';
|
||||
import { engineError } from '../../shared/contracts/engines.mjs';
|
||||
import { modelSnapshot, accountedTokens, checkBudget, BudgetExceeded } from '../../shared/contracts/execution.mjs';
|
||||
import { modelReservation,taskConsumption,checkModelDispatchBudget } from '../../shared/contracts/model-reservation.mjs';
|
||||
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
|
||||
import { workerForSession } from '../../shared/contracts/workers.mjs';
|
||||
import { selectPurgeStrategy, assertPurgeModelInput } from './purge-strategy.mjs';
|
||||
@@ -208,7 +209,8 @@ class EngineRunDriver {
|
||||
inputBudget:projected.inputBudget,safetyTokens:projected.safetyTokens,retainedGroups:projected.retainedGroups,archivedGroups:projected.archivedGroups};
|
||||
const maximumAttempts = config.mode === 'demo' ? 1 : config.modelRouting?.maxAttempts ?? 1;
|
||||
const credit = Math.min(this.run.record.budget.maxTokens - accountedTokens(this.run.record.usage), task.budget.maxTokens - accountedTokens(task.usage));
|
||||
const estimate = metrics.estimatedInputTokens + 128 + config.maxOutputTokens;
|
||||
const tokenReservation=modelReservation(config,body,taskConsumption(this.store,task.id),128);
|
||||
const estimate = tokenReservation.reservedTokens;
|
||||
const attempts = Math.min(maximumAttempts, Math.floor(credit / estimate), this.run.worker.budget.maxSteps - task.usage.requests);
|
||||
if (metrics.estimatedInputTokens>projected.inputBudget || attempts < 1) {
|
||||
const overhead=requestMetrics(completionBody(config,[],[])).estimatedInputTokens;
|
||||
@@ -216,7 +218,7 @@ class EngineRunDriver {
|
||||
const fixedInstructionTokens=requestMetrics(completionBody(config,originalMessages.filter(x=>['system','developer','user'].includes(x.role)),[])).estimatedInputTokens-overhead;
|
||||
const contextBudget={contextLimit:config.contextLimit,outputReservation:config.maxOutputTokens,safetyTokens:projected.safetyTokens,
|
||||
inputBudget:projected.inputBudget,requiredInputTokens:metrics.estimatedInputTokens,originalInputTokens:projected.before.estimatedInputTokens,
|
||||
remainingTokens:credit,runRemainingTokens:Math.max(0,this.run.record.budget.maxTokens-accountedTokens(this.run.record.usage)),
|
||||
remainingTokens:credit,tokenReservation,runRemainingTokens:Math.max(0,this.run.record.budget.maxTokens-accountedTokens(this.run.record.usage)),
|
||||
taskRemainingTokens:Math.max(0,task.budget.maxTokens-accountedTokens(task.usage)),maxAttempts:Math.max(0,attempts),sourceArtifactId:source.id,sourceSha256:source.sha256,
|
||||
breakdown:{requestOverheadTokens:overhead,toolDefinitionTokens,fixedInstructionTokens,
|
||||
retainedHistoryAndViewTokens:metrics.estimatedInputTokens-overhead-toolDefinitionTokens-fixedInstructionTokens},
|
||||
@@ -231,6 +233,7 @@ class EngineRunDriver {
|
||||
request = { id: randomUUID(), bridgeTraceId: trace.id, sourceSha256: controlledDigest(input), requestSha256: metrics.sha256,
|
||||
contextViewVersion: 1,
|
||||
requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, reservedPerAttempt: estimate,
|
||||
reservationVersion:1,tokenReservation,
|
||||
maxAttempts: attempts, accountedAttempts: 0, observedAttempts: 0, status: 'prepared', startedAt: at() };
|
||||
const plan = { ...request, ...owner };
|
||||
const artifact = this.store.artifactStore.write(this.session, this.run.record,
|
||||
@@ -259,7 +262,7 @@ class EngineRunDriver {
|
||||
modelEvent.label = progress.switching ? '引擎切换模型池连接' : progress.phase === 'failed' ? '引擎模型请求失败' : progress.phase === 'completed' ? '引擎模型响应已收到' : '引擎请求模型服务';
|
||||
if (['completed', 'failed'].includes(progress.phase)) modelEvent.finishedAt = at();
|
||||
this.save('model_progress');
|
||||
}, plan, tools, { attempts: 1 });
|
||||
}, plan, tools, { attempts: 1,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,this.session,this.run.record,config,body,128,previousFailure) });
|
||||
const count = Math.max(1, request.observedAttempts);
|
||||
request.actualAttemptsFinal = count; request.responseReceivedAt = at(); request.status = 'responded';
|
||||
while (request.accountedAttempts < count - 1) {
|
||||
|
||||
@@ -282,12 +282,12 @@ export class ModelHub {
|
||||
}
|
||||
} finally { this.waiting--; }
|
||||
}
|
||||
async complete(config, body, signal, onProgress, plan, execute) {
|
||||
async complete(config, body, signal, onProgress, plan, execute,beforeDispatch) {
|
||||
const operation = this.provider.requestOperation?.(signal);
|
||||
try { return await this.completeOwned(config, body, operation?.signal ?? signal, onProgress, plan, execute); }
|
||||
try { return await this.completeOwned(config, body, operation?.signal ?? signal, onProgress, plan, execute,beforeDispatch); }
|
||||
finally { operation?.finish(); }
|
||||
}
|
||||
async completeOwned(config, body, signal, onProgress, plan, execute) {
|
||||
async completeOwned(config, body, signal, onProgress, plan, execute,beforeDispatch) {
|
||||
const routing = config.modelRouting, excluded = new Set(), deadline = Date.now() + Math.min(routing.queueTimeoutMs, 60000);
|
||||
const requestId = plan?.id ?? randomUUID(), runId = plan?.runId ?? requestId;
|
||||
const requirements = config.capabilityProbe ? [] : ['text', ...(requestVariants(body).some(value => value.tools?.length) ? ['tools'] : []),
|
||||
@@ -303,6 +303,7 @@ export class ModelHub {
|
||||
let lastError;
|
||||
for (let count = 1; count <= routing.maxAttempts; count++) {
|
||||
signal?.throwIfAborted();
|
||||
beforeDispatch?.({previousFailure:lastError?.diagnostic});
|
||||
let lease;
|
||||
try { lease = await this.acquire(eligibleRouting, runId, excluded, signal, deadline, requirements); }
|
||||
catch (cause) { throw lastError ?? cause; }
|
||||
|
||||
@@ -8,6 +8,7 @@ import { normalizeProtocolResponse, protocolRoute, validateModelResponse, report
|
||||
import { readModelStream } from '../core/models/model-stream.mjs';
|
||||
import { modelWaitSettings, requestDeadline } from '../core/models/request-deadline.mjs';
|
||||
import { ModelTransport } from '../core/models/transport.mjs';
|
||||
import { localModelEstimate } from '../shared/contracts/model-reservation.mjs';
|
||||
import { ERROR_CAPTURE_LIMIT, readErrorResponse, upstreamCapture } from '../core/models/upstream-error.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
@@ -156,19 +157,22 @@ export class Provider {
|
||||
const waits = modelWaitSettings(config), timeoutMs = body ? waits.modelResponseTimeoutMs : 20000;
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs, totalTimeoutMs: timeoutMs,
|
||||
idleTimeoutMs: waits.modelStreamIdleTimeoutMs, ...this.retryPolicy, ...options };
|
||||
policy.attempts=Math.min(policy.attempts,options.maxBudgetAttempts??policy.attempts);
|
||||
const operation = this.requestOperation(signal); signal = operation.signal;
|
||||
const deadline = Date.now() + policy.totalTimeoutMs;
|
||||
const startedAt = Date.now();
|
||||
let attempts = 0;
|
||||
let attempts = 0, previousFailure = null;
|
||||
this.activeRequests = (this.activeRequests ?? 0) + 1;
|
||||
try {
|
||||
for (let attempt = 1; attempt <= policy.attempts; attempt++) {
|
||||
signal?.throwIfAborted();
|
||||
options.beforeDispatch?.({previousFailure});
|
||||
attempts = attempt;
|
||||
const timeout = requestDeadline({ signal, timeoutMs: Math.max(1, Math.min(policy.timeoutMs, deadline - Date.now())), idleTimeoutMs: policy.idleTimeoutMs });
|
||||
const requestSignal = timeout.signal;
|
||||
let readingStream = false, streamTerminalSeen = false, dispatched = false, usage = null, outcome = 'unknown';
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString() };
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString(),
|
||||
...(body?{localEstimate:localModelEstimate(body)}:{}) };
|
||||
const context = { endpoint: base, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, wireAttemptId: receipt.attemptId });
|
||||
let responseDetails = null;
|
||||
@@ -255,6 +259,7 @@ export class Provider {
|
||||
const retry = diagnostic.retryable && attempt < policy.attempts && remaining > waitMs;
|
||||
diagnostic.retryDecision = retry ? 'retry_after_wait' : readingStream ? 'stream_not_replayed' : !diagnostic.retryable ? 'not_retryable' : attempt >= policy.attempts ? 'attempt_limit' : 'wait_budget';
|
||||
diagnostic.remainingWaitBudgetMs = Math.max(0, remaining);
|
||||
previousFailure = diagnostic;
|
||||
this.lastRequest = { ...diagnostic, status: retry ? 'retrying' : 'failed', durationMs: Date.now() - startedAt };
|
||||
onProgress?.({ phase: retry ? 'retrying' : 'failed', attempt, maxAttempts: policy.attempts,
|
||||
diagnostic, waitMs: retry ? waitMs : 0, durationMs: Date.now() - startedAt });
|
||||
@@ -289,7 +294,7 @@ export class Provider {
|
||||
return this.request(selected, protocolRoute(selected), input, signal, key, progress, { ...(config.modelRouting ? { attempts: 1 } : {}), ...requestPolicy,
|
||||
transformResponse: raw => validateModelResponse(normalizeProtocolResponse(selected, raw), tools) });
|
||||
};
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute)
|
||||
const result = config.modelRouting && this.hub ? await this.hub.complete(config, body, signal, onProgress, requestPlan, execute,requestPolicy.beforeDispatch)
|
||||
: await execute(config, this.apiKey, body, onProgress);
|
||||
const received = result.choices?.[0]?.message;
|
||||
if (!received) throw new Error('兼容服务响应中缺少 choices[0].message。');
|
||||
|
||||
+14
-5
@@ -16,6 +16,7 @@ import { projectContext, toolDefinitions, researchToolDefinitions, engineToolDef
|
||||
import { now, repairToolPairs } from './store.mjs';
|
||||
import { executionEnvironment, sessionContext, sessionSnapshot } from './diagnostics.mjs';
|
||||
import { BudgetExceeded, checkBudget } from '../shared/contracts/execution.mjs';
|
||||
import { tokenBudgetState,checkModelDispatchBudget } from '../shared/contracts/model-reservation.mjs';
|
||||
import { taskToolAllowed, taskContract } from '../shared/contracts/task-control.mjs';
|
||||
import { CheckRunner } from '../core/check-runner.mjs';
|
||||
import { inspectProject } from '../core/project-inspector.mjs';
|
||||
@@ -362,7 +363,7 @@ export class Runtime {
|
||||
const projectMemory = await this.memoryManager().context(session, { redact: value => this.provider.redact(value) });
|
||||
signal.throwIfAborted();
|
||||
const planned = this.requestPlanner().prepare(session, run.record, config, { system: context, tools, stepId: stepRecord.id, projectMemory, skillCatalog, mcpCatalog,
|
||||
transportReservationTokens:run.worker?.engine?ENGINE_SEED_RESERVATION:0 });
|
||||
transportReservationTokens:run.worker?.engine?ENGINE_SEED_RESERVATION:0,engineStepOnly:!!run.worker?.engine });
|
||||
if (planned.record.archivedGroups && !projectionLogged) {
|
||||
projectionLogged = true; session.compactions++;
|
||||
this.store.event(session, { kind: 'context', label: '整理请求上下文', phase: 'completed', contextPlanId: planned.record.id,
|
||||
@@ -379,7 +380,7 @@ export class Runtime {
|
||||
requestStarted = true;
|
||||
const progressHandler = progress => {
|
||||
if (progress.consumption) {
|
||||
this.store.recordConsumption(session, run.record, { ...progress.consumption, requestId: planned.record.id }, planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
this.store.recordConsumption(session, run.record, { ...progress.consumption, requestId: planned.record.id }, planned.record.tokenReservation?.reservedTokens??planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
responseAccounted = true;
|
||||
if (run.worker) { planned.record.workerUsageAccounted = true; this.store.save(session); }
|
||||
return;
|
||||
@@ -418,9 +419,11 @@ export class Runtime {
|
||||
if (['completed', 'failed'].includes(progress.phase)) requestEvent.finishedAt = now();
|
||||
this.store.save(session);
|
||||
};
|
||||
const requestConfig=config.modelRouting&&!externalEngine?{...config,modelRouting:{...config.modelRouting,maxAttempts:Math.min(config.modelRouting.maxAttempts,planned.record.maxBudgetAttempts)}}:config;
|
||||
response = externalEngine
|
||||
? await this.engineRunner().complete(session, run, planned.messages, progressHandler, planned.record, tools)
|
||||
: await this.provider.complete(config, planned.messages, run.project, signal, progressHandler, planned.record, tools);
|
||||
: await this.provider.complete(requestConfig, planned.messages, run.project, signal, progressHandler, planned.record, tools,
|
||||
{maxBudgetAttempts:planned.record.maxBudgetAttempts,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,session,run.record,requestConfig,planned.body,0,previousFailure)});
|
||||
if (response.compatibility?.length && requestEvent) { requestEvent.compatibility = response.compatibility; this.store.save(session); }
|
||||
this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts });
|
||||
if (run.worker && !externalEngine && !responseAccounted) {
|
||||
@@ -431,10 +434,15 @@ export class Runtime {
|
||||
if (requestEvent?.stream && requestEvent.stream.status !== 'completed') { requestEvent.stream.status = signal.aborted ? 'interrupted' : 'incomplete'; requestEvent.phase = signal.aborted ? 'interrupted' : 'failed'; requestEvent.finishedAt = now(); }
|
||||
this.requestPlanner().finish(session, planned.record, { error: Object.assign(new Error(this.provider.redact(error.message ?? String(error))), { name: signal.aborted ? 'AbortError' : error.name }), attempts: planned.record.requestAttempts });
|
||||
if (run.worker && requestStarted && !responseAccounted) {
|
||||
this.store.recordResponse(session, run.record, null, planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
this.store.recordResponse(session, run.record, null, planned.record.tokenReservation?.reservedTokens??planned.record.estimatedInputTokens + config.maxOutputTokens);
|
||||
planned.record.workerUsageAccounted = true;
|
||||
this.store.save(session, { type: 'worker.request_usage_unknown' });
|
||||
}
|
||||
try { checkBudget(run.record,task); }
|
||||
catch(budget){
|
||||
if(budget instanceof BudgetExceeded&&error.diagnostic)budget.contextBudget={modelFailure:structuredClone(error.diagnostic)};
|
||||
throw budget;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
signal.throwIfAborted();
|
||||
@@ -543,8 +551,9 @@ export class Runtime {
|
||||
run.stopReason = budget?.reason ?? 'cancelled';
|
||||
if(budget)run.record.budgetPause={schema:1,reason:budget.reason,createdAt:now(),taskId:run.taskId,runId:run.id,
|
||||
...(budget.contextBudget??{}),runUsage:structuredClone(run.record.usage),taskUsage:structuredClone(this.store.core.task.get(run.taskId).usage),
|
||||
tokenAccounting:tokenBudgetState(run.record,this.store.core.task.get(run.taskId)),
|
||||
runBudget:structuredClone(run.record.budget),taskBudget:structuredClone(this.store.core.task.get(run.taskId).budget),
|
||||
note:'本地预留估算与服务实际用量分开记录;图片预留不是厂商计费或容量保证。'};
|
||||
note:'本地预留与上游报告用量分开记录。单个在途响应可能超过预估;超额后停止后续请求,未知用量保持未知。'};
|
||||
for (const event of session.events.filter(item => item.kind === 'model' && item.runId === run.id && ['running', 'retrying'].includes(item.phase))) {
|
||||
event.phase = 'interrupted'; event.finishedAt = now(); event.output = '模型请求已停止,已完成的工具结果保留。';
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ export class Store extends EventEmitter {
|
||||
if (plan.workerDispatchedAt && !plan.workerUsageAccounted && !plan.engineStepOnly) {
|
||||
const run = this.core.run.get(plan.runId);
|
||||
if (run && this.core.task.get(run.taskId)?.workerId) {
|
||||
this.recordResponse(session, run, null, plan.estimatedInputTokens + (run.model.maxOutputTokens ?? 2048));
|
||||
this.recordResponse(session, run, null, plan.tokenReservation?.reservedTokens??plan.estimatedInputTokens + (run.model.maxOutputTokens ?? 2048));
|
||||
plan.workerUsageAccounted = true; plan.workerUsageRecovered = true; changed = true;
|
||||
}
|
||||
}
|
||||
@@ -553,6 +553,7 @@ export class Store extends EventEmitter {
|
||||
model: receipt.model, endpoint: receipt.endpoint, protocol: receipt.protocol,
|
||||
startedAt: receipt.startedAt, finishedAt: receipt.finishedAt, outcome: receipt.outcome,
|
||||
usageStatus: receipt.usage ? 'reported' : 'unknown', usage: receipt.usage ?? null,
|
||||
...(receipt.localEstimate?{localEstimate:structuredClone(receipt.localEstimate)}:{}),
|
||||
estimatedUnknownTokens: receipt.usage ? 0 : Math.max(0, Math.ceil(estimate)) });
|
||||
this.recordResponse(session, run, receipt.usage, estimate);
|
||||
this.save(session, { type: 'model.consumption', detail: { attemptId: receipt.attemptId, usageStatus: receipt.usage ? 'reported' : 'unknown' } });
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { engineError, engineCanonicalDigest as controlledDigest } from './engines.mjs';
|
||||
import { completionBody, requestMetrics, requestVariants, outputReservation } from './model-request.mjs';
|
||||
import { engineContextIndex,engineContextEvidence,engineContextView,engineSeedPrompt } from './engine-context.mjs';
|
||||
import { verifyModelReservation } from './model-reservation.mjs';
|
||||
|
||||
export const ENGINE_RUN_ACTIVE = new Set(['queued', 'starting', 'initializing', 'running', 'waiting_tool', 'closing', 'stopping']);
|
||||
export function selectedEngineInput(input) {
|
||||
@@ -64,12 +65,16 @@ export function verifyEngineModelInput(store, session, job, request) {
|
||||
if (input.plan[key] !== request[key]) throw engineError('原引擎模型请求或尝试预算与封存输入不一致。');
|
||||
}
|
||||
if(request.contextViewVersion!==undefined&&(request.contextViewVersion!==1||input.plan.contextViewVersion!==request.contextViewVersion||!input.view))throw engineError('原生模型上下文视图版本缺失或不匹配。');
|
||||
if(request.reservationVersion!==undefined){
|
||||
if(request.reservationVersion!==1||input.plan.reservationVersion!==1||controlledDigest(request.tokenReservation)!==controlledDigest(input.plan.tokenReservation))throw engineError('原生用量预留版本或输入不一致。');
|
||||
verifyModelReservation(store,session,job.taskId,{...job.model,modelRouting:job.model.routing},input.body,request.tokenReservation);
|
||||
}else if(input.plan.reservationVersion!==undefined||input.plan.tokenReservation!==undefined)throw engineError('原生用量预留版本缺失。');
|
||||
if (input.owner.sessionId !== job.sessionId || input.owner.taskId !== job.taskId || input.owner.runId !== job.runId || input.owner.projectId !== job.projectId
|
||||
|| controlledDigest(input.native) !== request.sourceSha256 || metrics.sha256 !== request.requestSha256 || metrics.bytes !== request.requestBytes
|
||||
|| !Number.isSafeInteger(request.maxAttempts) || request.maxAttempts < 1 || request.maxAttempts > 8
|
||||
|| !Number.isSafeInteger(input.native.maxTokens) || input.native.maxTokens < 1
|
||||
|| !requestVariants(input.body).every(body => outputReservation(body) === input.native.maxTokens)
|
||||
|| request.reservedPerAttempt !== metrics.estimatedInputTokens + 128 + input.native.maxTokens) throw engineError('原引擎模型输入、归属或预算核对失败。');
|
||||
|| request.reservedPerAttempt !== (request.reservationVersion===1?request.tokenReservation.reservedTokens:metrics.estimatedInputTokens + 128 + input.native.maxTokens)) throw engineError('原引擎模型输入、归属或预算核对失败。');
|
||||
if(input.view){
|
||||
const view=input.view,source=store.owned('artifact',view.sourceArtifactId,session.id),sourceValue=JSON.parse(store.artifactStore.read(source));
|
||||
if(view.schemaVersion!==1||sourceValue.schemaVersion!==1||source.kind!=='engine-native-input'||source.taskId!==job.taskId||source.runId!==job.runId||source.sha256!==view.sourceSha256
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import {actualRequest,outputReservation,requestMetrics} from './model-request.mjs';
|
||||
import {accountedTokens,checkBudget,BudgetExceeded} from './execution.mjs';
|
||||
|
||||
const count=value=>Number.isSafeInteger(value)&&value>=0;
|
||||
const bounded=value=>Math.min(Number.MAX_SAFE_INTEGER,Math.max(0,Math.ceil(value)));
|
||||
const endpoint=value=>String(value??'').replace(/\/+$/,'');
|
||||
export function localModelEstimate(body){
|
||||
const metrics=requestMetrics(body);
|
||||
return {schemaVersion:1,inputTokens:metrics.estimatedInputTokens,outputTokens:outputReservation(body),
|
||||
requestSha256:metrics.sha256,requestBytes:metrics.bytes,estimateMethod:metrics.estimateMethod};
|
||||
}
|
||||
export function taskConsumption(store,taskId){
|
||||
return [...store.core.run.values()].filter(run=>run.taskId===taskId)
|
||||
.flatMap(run=>(run.consumption??[]).map(receipt=>({...receipt,runId:run.id})));
|
||||
}
|
||||
|
||||
export function checkModelDispatchBudget(store,session,run,config,body,overheadTokens=0,previousFailure=null){
|
||||
const task=store.owned('task',run.taskId,session.id);
|
||||
try{
|
||||
checkBudget(run,task);
|
||||
const tokenReservation=modelReservation(config,body,taskConsumption(store,task.id),overheadTokens);
|
||||
const remaining=value=>value.budget.maxTokens>0?Math.max(0,value.budget.maxTokens-accountedTokens(value.usage)):Infinity;
|
||||
const runRemainingTokens=remaining(run),taskRemainingTokens=remaining(task),remainingTokens=Math.min(runRemainingTokens,taskRemainingTokens);
|
||||
if(tokenReservation.reservedTokens>remainingTokens){
|
||||
const error=new BudgetExceeded(runRemainingTokens<=taskRemainingTokens?'token_limit':'task_token_limit','下一次模型请求的用量预留超过剩余预算,已在发送前暂停。原用量和已完成工具结果保留。');
|
||||
error.contextBudget={tokenReservation,remainingTokens,runRemainingTokens:Number.isFinite(runRemainingTokens)?runRemainingTokens:null,
|
||||
taskRemainingTokens:Number.isFinite(taskRemainingTokens)?taskRemainingTokens:null};
|
||||
throw error;
|
||||
}
|
||||
return tokenReservation;
|
||||
}catch(error){
|
||||
if(error instanceof BudgetExceeded&&previousFailure)error.contextBudget={...error.contextBudget,modelFailure:structuredClone(previousFailure)};
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// A conservative reservation for the next dispatch, not corrected historical
|
||||
// billing or a promise about an unseen upstream response. Never lowers estimates.
|
||||
export function modelReservation(config,body,receipts=[],overheadTokens=0){
|
||||
if(!count(overheadTokens))throw new Error('模型请求封装预留无效。');
|
||||
const members=config.modelRouting?.members??[{id:null,model:config.model,protocol:config.protocol??'chat-completions',baseUrl:config.baseUrl}];
|
||||
const routes=members.map(member=>{
|
||||
const wire=actualRequest(body,member),local=localModelEstimate(wire),seen=new Set(),sources=[];
|
||||
let inputRatio=1,observedOutputTokens=local.outputTokens,observedTotalFloor=0;
|
||||
if(!count(local.inputTokens)||!count(local.outputTokens))throw new Error('模型请求输入或输出预留无效。');
|
||||
for(const receipt of receipts){
|
||||
const estimate=receipt.localEstimate,usage=receipt.usage;
|
||||
if(receipt.model!==member.model||(receipt.protocol??'chat-completions')!==(member.protocol??'chat-completions')
|
||||
||(receipt.connectionId??null)!==(member.id??null)||endpoint(receipt.endpoint)!==endpoint(member.baseUrl)
|
||||
||receipt.usageStatus!=='reported'||!usage||estimate?.schemaVersion!==1||!count(estimate.inputTokens)||estimate.inputTokens<1
|
||||
||!receipt.attemptId||seen.has(receipt.attemptId))continue;
|
||||
if(!count(usage.total_tokens)&&!(count(usage.prompt_tokens)&&count(usage.completion_tokens)))continue;
|
||||
seen.add(receipt.attemptId);
|
||||
if(count(usage.prompt_tokens))inputRatio=Math.max(inputRatio,usage.prompt_tokens/estimate.inputTokens);
|
||||
if(count(usage.completion_tokens))observedOutputTokens=Math.max(observedOutputTokens,usage.completion_tokens);
|
||||
const total=count(usage.total_tokens)?usage.total_tokens:usage.prompt_tokens+usage.completion_tokens;
|
||||
observedTotalFloor=Math.max(observedTotalFloor,total);
|
||||
sources.push({runId:receipt.runId,attemptId:receipt.attemptId,localInputTokens:estimate.inputTokens,
|
||||
reportedInputTokens:count(usage.prompt_tokens)?usage.prompt_tokens:null,reportedOutputTokens:count(usage.completion_tokens)?usage.completion_tokens:null,totalTokens:total});
|
||||
}
|
||||
const reservedTokens=bounded(Math.max(local.inputTokens*inputRatio+observedOutputTokens,observedTotalFloor)+overheadTokens);
|
||||
return {connectionId:member.id??null,model:member.model,protocol:member.protocol??'chat-completions',endpoint:member.baseUrl,
|
||||
localInputTokens:local.inputTokens,outputReservation:local.outputTokens,inputRatio,observedOutputTokens,observedTotalFloor,reservedTokens,sources};
|
||||
});
|
||||
if(!routes.length)throw new Error('模型预留没有实际候选线路。');
|
||||
return {schemaVersion:1,method:'observed-route-max-v1',overheadTokens,reservedTokens:Math.max(...routes.map(route=>route.reservedTokens)),routes};
|
||||
}
|
||||
|
||||
export function verifyModelReservation(store,session,taskId,config,body,reservation){
|
||||
if(reservation?.schemaVersion!==1||reservation.method!=='observed-route-max-v1'||!Array.isArray(reservation.routes))throw new Error('模型用量预留版本或线路缺失。');
|
||||
const receipts=new Map();
|
||||
for(const route of reservation.routes)for(const source of route.sources??[]){
|
||||
const run=store.owned('run',source.runId,session.id);
|
||||
if(run.taskId!==taskId)throw new Error('模型预算校准来源不属于当前Task。');
|
||||
const receipt=run.consumption?.find(value=>value.attemptId===source.attemptId);
|
||||
if(!receipt)throw new Error('模型预算校准缺少原消费记录。');
|
||||
receipts.set(source.attemptId,{...receipt,runId:run.id});
|
||||
}
|
||||
const expected=modelReservation(config,body,[...receipts.values()],reservation.overheadTokens);
|
||||
if(JSON.stringify(expected)!==JSON.stringify(reservation))throw new Error('模型预算预留与原估算/用量/实际线路不一致。');
|
||||
return reservation;
|
||||
}
|
||||
|
||||
export function tokenBudgetState(run,task){
|
||||
const state=value=>{
|
||||
const reportedTokens=value.usage?.totalTokens??0,estimatedUnknownTokens=value.usage?.estimatedUnknownTokens??0,
|
||||
accountedTokens=reportedTokens+estimatedUnknownTokens,maximumTokens=value.budget?.maxTokens??0;
|
||||
return {maximumTokens,reportedTokens,unknownRequests:value.usage?.unknownRequests??0,estimatedUnknownTokens,accountedTokens,
|
||||
remainingTokens:maximumTokens>0?Math.max(0,maximumTokens-accountedTokens):null,
|
||||
exceededTokens:maximumTokens>0?Math.max(0,accountedTokens-maximumTokens):0};
|
||||
};
|
||||
return {schemaVersion:1,singleInFlightResponseMayExceed:true,run:state(run),task:state(task)};
|
||||
}
|
||||
Reference in New Issue
Block a user