fix: bind observed models and calibrate every model dispatch budget
This commit is contained in:
@@ -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('图片身份或预算与发送前封存不匹配。');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user