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
+14 -5
View File
@@ -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 = '模型请求已停止,已完成的工具结果保留。';
}