fix: bind observed models and calibrate every model dispatch budget

This commit is contained in:
Agent Workbench Dev
2026-11-02 20:50:42 +08:00
parent 30c318bf20
commit bb45dabac4
23 changed files with 526 additions and 29 deletions
+10 -1
View File
@@ -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('图片身份或预算与发送前封存不匹配。');
}
+6 -3
View File
@@ -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) {
+4 -3
View File
@@ -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; }