feat: connect controlled engines to owned Tasks, approvals and budget ledger

This commit is contained in:
Agent Workbench Dev
2026-10-31 05:04:55 +08:00
parent a01c41811b
commit f0eecbda47
27 changed files with 1653 additions and 33 deletions
+22 -6
View File
@@ -22,6 +22,7 @@ import { WorkspaceManager, verifyWorkspaceProject } from '../core/workspaces/git
import { assertWorkspacesResolved, projectWorkspaceIdentity } from '../shared/contracts/workspaces.mjs';
import { ResearchManager } from '../core/research-manager.mjs';
import { EngineManager } from '../core/engine-manager.mjs';
import { EngineRunner } from '../core/engines/engine-runner.mjs';
import { BrowserManager, publicBrowserPlan } from '../core/browser/browser-manager.mjs';
import { browserPlanInput } from '../shared/contracts/browser.mjs';
import { TaskPlanManager } from '../core/tasks/task-plan-manager.mjs';
@@ -118,6 +119,7 @@ export class Runtime {
redact: value => this.provider.redact(value), projectBusy: projectId => this.projectHasActivity(projectId) });
}
requestPlanner() { return this.contextPlans ??= new RequestPlanner(this.store); }
engineRunner() { return this.controlledEngines ??= new EngineRunner(this.store, this.provider, { appDirectory: this.appDirectory }); }
memoryManager() { return this.projectMemories ??= new ProjectMemoryManager(this.store); }
checkpointManager() { return this.taskCheckpoints ??= new TaskCheckpointManager(this.store, {
taskPlans: this.taskPlanner(), redact: value => this.provider.redact(value) }); }
@@ -267,29 +269,38 @@ export class Runtime {
' tokens;省略 ' + planned.record.archivedGroups + ' 个完整消息组,原历史和参数已保存为SHA256工件。估算与服务实际用量分别记录。' });
}
let requestEvent;
let response, requestStarted = false, responseAccounted = false;
const externalEngine = !!run.worker?.engine;
if (externalEngine) { planned.record.engineStepOnly = true; planned.record.engine = 'deepseek-workbench'; }
let response, requestStarted = false, responseAccounted = externalEngine;
try {
signal.throwIfAborted(); checkBudget(run.record, task);
if (run.worker) { planned.record.workerDispatchedAt = now(); this.store.save(session, { type: 'worker.request_intent' }); }
if (run.worker && !externalEngine) { planned.record.workerDispatchedAt = now(); this.store.save(session, { type: 'worker.request_intent' }); }
requestStarted = true;
response = await this.provider.complete(config, planned.messages, run.project, signal, progress => {
if (!requestEvent) requestEvent = this.store.event(session, { kind: 'model', runId: run.id, source: 'model_connection', label: '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id });
const progressHandler = progress => {
if (!requestEvent) requestEvent = this.store.event(session, { kind: externalEngine ? 'engine' : 'model', runId: run.id,
source: externalEngine ? 'controlled_engine' : 'model_connection', label: externalEngine ? '受控引擎执行' : '请求模型服务', phase: 'running', startedAt: now(), contextPlanId: planned.record.id });
if (progress.engineRunId) requestEvent.engineRunId = progress.engineRunId;
planned.record.status = progress.phase === 'running' || progress.phase === 'retrying' ? 'requesting' : planned.record.status;
planned.record.requestAttempts = Math.max(planned.record.requestAttempts, progress.attempt ?? 0);
stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0);
requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt;
if (progress.connectionId) { requestEvent.connectionId = progress.connectionId; requestEvent.modelAttemptId = progress.modelAttemptId; requestEvent.actualModel = progress.model; }
requestEvent.label = progress.switching ? '正在切换模型池连接' : progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
if (externalEngine) requestEvent.label = progress.phase === 'completed' ? '引擎原结果已收到' : '受控引擎执行';
requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。'
: progress.phase === 'completed' ? '模型响应已收到;工具请求将在校验后执行。'
: progress.diagnostic?.message ?? '正在等待模型响应。';
if (externalEngine) requestEvent.output = progress.phase === 'completed' ? '原引擎工具请求或最终输出已返回,后续按所属任务核验。' : '等待所属引擎;实际模型尝试另有记录。';
if (progress.diagnostic) requestEvent.diagnostic = progress.diagnostic;
if (progress.durationMs !== undefined) requestEvent.durationMs = progress.durationMs;
if (['completed', 'failed'].includes(progress.phase)) requestEvent.finishedAt = now();
this.store.save(session);
}, planned.record, tools);
};
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);
this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts });
if (run.worker) {
if (run.worker && !externalEngine) {
this.store.recordResponse(session, run.record, response.usage, planned.record.estimatedInputTokens + Math.ceil(Buffer.byteLength(JSON.stringify(response.message), 'utf8') / 3) + 8);
planned.record.workerUsageAccounted = true; this.store.save(session); responseAccounted = true;
}
@@ -323,6 +334,7 @@ export class Runtime {
if (!calls.length) {
this.store.finishStep(session, stepRecord);
session.status = session.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'completed';
if (externalEngine) await this.engineRunner().finish(session, run);
if (session.status === 'completed' && run.worker) {
try { await this.workerManager().output(session, run.worker); signal.throwIfAborted(); }
catch (error) {
@@ -407,6 +419,8 @@ export class Runtime {
output: diagnostic.message, diagnostic: session.lastFailure });
}
this.store.save(session);
} finally {
if (run.worker?.engine) await this.engineRunner().finish(session, run);
}
}
async executeTool(session, call, run) {
@@ -419,6 +433,7 @@ export class Runtime {
const args = JSON.parse(call.function.arguments);
validateToolArgs(tool, args);
assertWorkerTool(this.store, session, tool, args);
if (run.worker?.engine) this.engineRunner().assertCurrent(session, run);
event.input = JSON.parse(this.provider.redact(JSON.stringify(args)));
const proofHint = !!this.store.core.task.get(run.taskId)?.currentTaskPlanId && PLAN_READ_TOOLS.includes(tool);
const readMetadata = { ...(proofHint ? { tool_invocation_id: event.invocationId } : {}),
@@ -580,6 +595,7 @@ export class Runtime {
const cwd = safePath(run.project);
event.phase = 'running'; event.executedAt = now(); session.status = 'running';
this.store.invocationState(session, event, 'running');
if (run.worker?.engine) this.engineRunner().assertCurrent(session, run);
if (execute) return execute();
return this.executeCommand(command, cwd, run.controller.signal, output => {
event.output = output; this.store.save(session);