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