fix(engine): bound native history and preserve verified continuation context
This commit is contained in:
@@ -48,7 +48,7 @@ export function historyGroups(messages) {
|
||||
return groups;
|
||||
}
|
||||
|
||||
export function budgetFor(config, run, task) {
|
||||
export function budgetFor(config, run, task, transportReservationTokens = 0) {
|
||||
const outputReservation = config.maxOutputTokens;
|
||||
const safetyTokens = Math.max(512, Math.ceil(Math.min(config.contextLimit, 24000) * 0.1));
|
||||
const remaining = (maximum, usage) => maximum > 0 ? Math.max(0, maximum - accountedTokens(usage)) : null;
|
||||
@@ -57,8 +57,9 @@ export function budgetFor(config, run, task) {
|
||||
let limit = config.contextLimit, reason = 'context_limit';
|
||||
if (runRemainingTokens !== null && runRemainingTokens < limit) { limit = runRemainingTokens; reason = 'token_limit'; }
|
||||
if (taskRemainingTokens !== null && taskRemainingTokens < limit) { limit = taskRemainingTokens; reason = 'task_token_limit'; }
|
||||
const inputBudget = limit - outputReservation - safetyTokens;
|
||||
return { contextLimit: config.contextLimit, outputReservation, safetyTokens, inputBudget, targetInputTokens: Math.min(16000, inputBudget),
|
||||
if(!Number.isSafeInteger(transportReservationTokens)||transportReservationTokens<0)throw new Error('请求封装预留无效。');
|
||||
const inputBudget = limit - outputReservation - safetyTokens - transportReservationTokens;
|
||||
return { contextLimit: config.contextLimit, outputReservation, safetyTokens, ...(transportReservationTokens?{transportReservationTokens}:{}), inputBudget, targetInputTokens: Math.min(16000, inputBudget),
|
||||
runRemainingTokens, taskRemainingTokens, limitingReason: reason };
|
||||
}
|
||||
|
||||
@@ -157,10 +158,10 @@ 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 }) {
|
||||
prepare(session, run, config, { system, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0 }) {
|
||||
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);
|
||||
const groups = historyGroups(session.providerMessages), budget = budgetFor(config, run, task, transportReservationTokens);
|
||||
const lastUser = groups.findLast(group => group.messages[0].role === 'user');
|
||||
const fixed = { role: 'system', content: system };
|
||||
const breakdown = metrics => {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { modelSnapshot, accountedTokens, checkBudget, BudgetExceeded } from '../
|
||||
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
|
||||
import { workerForSession } from '../../shared/contracts/workers.mjs';
|
||||
import { selectPurgeStrategy, assertPurgeModelInput } from './purge-strategy.mjs';
|
||||
import { engineHistoryGroups,engineContextEvidence,engineContextIndex,engineContextView,engineSeedPrompt } from '../../shared/contracts/engine-context.mjs';
|
||||
|
||||
const at = () => new Date().toISOString();
|
||||
const deferred = () => {
|
||||
@@ -42,7 +43,7 @@ export class EngineRunner {
|
||||
if (!driver) {
|
||||
if (workerForSession(this.store, session) !== run.worker || !run.worker?.engine) throw engineError('外部引擎只能在所属受限子工程中执行。');
|
||||
assertControlledInstallation(run.worker.engine);
|
||||
driver = new EngineRunDriver(this, session, run, planned, tools); this.drivers.set(run.id, driver);
|
||||
driver = new EngineRunDriver(this, session, run, planned, tools, messages); this.drivers.set(run.id, driver);
|
||||
}
|
||||
return driver.complete(messages, progress).catch(error => { driver.fail(error); throw error; });
|
||||
}
|
||||
@@ -71,7 +72,7 @@ export class EngineRunner {
|
||||
}
|
||||
|
||||
class EngineRunDriver {
|
||||
constructor(manager, session, run, seedPlan, tools) {
|
||||
constructor(manager, session, run, seedPlan, tools, messages) {
|
||||
this.manager = manager; this.store = manager.store; this.provider = manager.provider; this.session = session; this.run = run;
|
||||
this.queue = []; this.waiter = null; this.pendingCalls = new Map(); this.finished = false;
|
||||
const base = path.resolve(manager.directoryRoot ?? path.join(os.tmpdir(), 'AgentWorkbench-Engine-Runs'));
|
||||
@@ -82,6 +83,9 @@ class EngineRunDriver {
|
||||
sdkSessionId: randomUUID(), directory: path.join(base, id), model: modelSnapshot(run.config), budget: { ...run.record.budget, maxToolCalls: run.worker.budget.maxToolCalls },
|
||||
tools: structuredClone(tools), sourceContextPlanId: seedPlan.id, sourceRequestSha256: seedPlan.requestSha256,
|
||||
status: 'queued', startedAt: at(), finishedAt: null, requiresVerification: false, verified: false, native: null, requests: [], calls: [] };
|
||||
const seed=this.store.artifactStore.write(session,run.record,
|
||||
JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,sourceContextPlanId:seedPlan.id,sourceRequestSha256:seedPlan.requestSha256,messages,prompt:engineSeedPrompt(messages)}))),{kind:'engine-seed-context'});
|
||||
this.store.core.artifact.set(seed.id,seed);this.job.seedContext={schemaVersion:1,artifactId:seed.id,sha256:seed.sha256};
|
||||
const artifact = this.store.artifactStore.write(session, run.record, engineRunInput(this.job), { kind: 'engine-run-input' });
|
||||
this.store.core.artifact.set(artifact.id, artifact); this.job.inputArtifactId = artifact.id; this.job.inputSha256 = artifact.sha256;
|
||||
run.record.engine = 'deepseek-workbench'; run.record.engineRunId = id;
|
||||
@@ -126,7 +130,7 @@ class EngineRunDriver {
|
||||
});
|
||||
this.nativeWait = (async () => {
|
||||
try {
|
||||
await this.native.prompt('Execute the following workbench task context as task data. Use only the registered tools; all writes and checks are owned by the workbench.\n' + JSON.stringify(messages));
|
||||
await this.native.prompt(engineSeedPrompt(messages));
|
||||
const text = await this.native.wait(); this.finished = true; this.publish({ finalText: text });
|
||||
} catch (error) { this.fail(error); }
|
||||
})();
|
||||
@@ -183,23 +187,54 @@ class EngineRunDriver {
|
||||
this.assertInput(); signal.throwIfAborted();
|
||||
assertPurgeModelInput(input, this.job.installation.strategy);
|
||||
const task = this.store.core.task.get(this.run.taskId); checkBudget(this.run.record, task);
|
||||
const messages = nativeMessagesToChat(input), tools = input.tools.map(tool => ({ type: 'function', function: tool }));
|
||||
const config = { ...this.run.config, maxOutputTokens: input.maxTokens }, body = completionBody(config, messages, tools), metrics = requestMetrics(body);
|
||||
const originalMessages = nativeMessagesToChat(input), tools = input.tools.map(tool => ({ type: 'function', function: tool }));
|
||||
engineHistoryGroups(originalMessages);
|
||||
const config = { ...this.run.config, maxOutputTokens: input.maxTokens };
|
||||
const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id };
|
||||
const source=this.store.artifactStore.write(this.session,this.run.record,
|
||||
JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,owner,native:input}))),{kind:'engine-native-input'});
|
||||
this.store.core.artifact.set(source.id,source);
|
||||
const safety=Math.max(512,Math.ceil(Math.min(config.contextLimit,24000)*0.1));
|
||||
let index=null,reference={id:null,sha256:null};
|
||||
if(requestMetrics(completionBody(config,originalMessages,tools)).estimatedInputTokens>config.contextLimit-config.maxOutputTokens-safety){
|
||||
index=engineContextIndex(originalMessages,engineContextEvidence(this.store,this.session,this.job,originalMessages),
|
||||
{artifact_id:source.id,sha256:source.sha256,owner});
|
||||
reference=this.store.artifactStore.write(this.session,this.run.record,JSON.parse(this.provider.redact(JSON.stringify(index))),{kind:'engine-history-index'});
|
||||
this.store.core.artifact.set(reference.id,reference);
|
||||
}
|
||||
const projected=engineContextView(originalMessages,tools,config,index,reference),{messages,body,metrics}=projected;
|
||||
const view={schemaVersion:1,mode:projected.mode,sourceArtifactId:source.id,sourceSha256:source.sha256,
|
||||
indexArtifactId:projected.indexArtifactId,indexSha256:projected.indexSha256,beforeInputTokens:projected.before.estimatedInputTokens,
|
||||
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 attempts = Math.min(maximumAttempts, Math.floor(credit / estimate), this.run.worker.budget.maxSteps - task.usage.requests);
|
||||
if (metrics.estimatedInputTokens + 128 + config.maxOutputTokens > config.contextLimit || attempts < 1) {
|
||||
throw new BudgetExceeded('engine_input_budget', '原引擎完整模型输入或累计尝试超出封存预算,未发送模型请求。');
|
||||
if (metrics.estimatedInputTokens>projected.inputBudget || attempts < 1) {
|
||||
const overhead=requestMetrics(completionBody(config,[],[])).estimatedInputTokens;
|
||||
const toolDefinitionTokens=requestMetrics(completionBody(config,[],tools)).estimatedInputTokens-overhead;
|
||||
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)),
|
||||
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},
|
||||
limitingReason:metrics.estimatedInputTokens>projected.inputBudget?'context_limit':'remaining_token_budget'};
|
||||
const rejected=this.store.artifactStore.write(this.session,this.run.record,
|
||||
JSON.parse(this.provider.redact(JSON.stringify({schemaVersion:1,owner,view,body,budget:contextBudget,dispatched:false}))),{kind:'engine-model-rejection'});
|
||||
this.store.core.artifact.set(rejected.id,rejected);(this.job.inputRejections??=[]).push({artifactId:rejected.id,sha256:rejected.sha256,...contextBudget});this.save('input_rejected');
|
||||
const error=new BudgetExceeded('engine_input_budget','原引擎必需目标、最新结果或剩余尝试超出封存预算,未发送模型请求。原输入和容量分项已保存;请核对上下文/预算后显式继续。');
|
||||
error.contextBudget={...contextBudget,rejectionArtifactId:rejected.id,rejectionSha256:rejected.sha256};throw error;
|
||||
}
|
||||
const selected = config.modelRouting ? { ...config, modelRouting: { ...config.modelRouting, maxAttempts: attempts } } : config;
|
||||
request = { id: randomUUID(), bridgeTraceId: trace.id, sourceSha256: controlledDigest(input), requestSha256: metrics.sha256,
|
||||
contextViewVersion: 1,
|
||||
requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, reservedPerAttempt: estimate,
|
||||
maxAttempts: attempts, accountedAttempts: 0, observedAttempts: 0, status: 'prepared', startedAt: at() };
|
||||
const owner = { sessionId: this.session.id, projectId: this.session.projectId, taskId: this.run.taskId, runId: this.run.id };
|
||||
const plan = { ...request, ...owner };
|
||||
const artifact = this.store.artifactStore.write(this.session, this.run.record,
|
||||
JSON.parse(this.provider.redact(JSON.stringify({ schemaVersion: 1, owner, native: input, body, plan }))), { kind: 'engine-model-input' });
|
||||
JSON.parse(this.provider.redact(JSON.stringify({ schemaVersion: 1, owner, native: input, body, plan,view }))), { kind: 'engine-model-input' });
|
||||
this.store.core.artifact.set(artifact.id, artifact); request.inputArtifactId = artifact.id; request.inputSha256 = artifact.sha256;
|
||||
this.job.requests.push(request);
|
||||
const modelEvent = this.store.event(this.session, { kind: 'model', source: 'engine_model_bridge', engineRunId: this.job.id, engineModelRequestId: request.id,
|
||||
|
||||
Reference in New Issue
Block a user