feat: connect controlled engines to owned Tasks, approvals and budget ledger
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { isId, accountedTokens, emptyUsage } from './execution.mjs';
|
||||
import { selectedEngineInput } from './engine-execution.mjs';
|
||||
|
||||
export const WORKER_TOOLS = new Set(['inspect_session', 'inspect_project', 'index_project', 'query_code', 'read_artifact',
|
||||
'list_files', 'read_file', 'search_files', 'propose_file', 'propose_patch', 'prepare_change_set', 'plan_checks', 'run_checks']);
|
||||
@@ -29,8 +30,9 @@ const text = (value, maximum, label) => {
|
||||
};
|
||||
|
||||
export function collaborationInput(input, config) {
|
||||
if (!input || Array.isArray(input) || Object.keys(input).some(key => !['goal', 'budget', 'children'].includes(key))) throw workerConflict('协作只接受父目标、预算和两个子任务。');
|
||||
if (!Array.isArray(input.children) || input.children.length !== 2) throw workerConflict('本阶段需要两个受限子任务。');
|
||||
if (!input || Array.isArray(input) || Object.keys(input).some(key => !['goal', 'budget', 'children', 'engine'].includes(key))) throw workerConflict('协作只接受父目标、预算、子任务和已检测的引擎。');
|
||||
const engine = input.engine === undefined ? null : selectedEngineInput(input.engine);
|
||||
if (!Array.isArray(input.children) || input.children.length !== (engine ? 1 : 2)) throw workerConflict(engine ? '受控引擎计划需要一个所属子工程。' : '本阶段需要两个受限子任务。');
|
||||
const raw = input.budget ?? {};
|
||||
if (!raw || typeof raw !== 'object' || Array.isArray(raw) || Object.keys(raw).some(key => !['maxTokens', 'maxSteps', 'maxToolCalls', 'maxDurationMs'].includes(key))) throw workerConflict('协作预算字段无效。');
|
||||
const tokenMaximum = Math.min(config.maxTaskTokens ?? 1000000, config.maxRunTokens ?? 200000);
|
||||
@@ -47,12 +49,12 @@ export function collaborationInput(input, config) {
|
||||
if (new Set(paths.map(workerPathKey)).size !== paths.length) throw workerConflict('子任务范围不能重复。');
|
||||
const part = item.budget ?? {};
|
||||
if (!part || Array.isArray(part) || Object.keys(part).some(key => !['maxTokens', 'maxSteps', 'maxToolCalls'].includes(key))) throw workerConflict('子预算字段无效。');
|
||||
const allocate = key => index === 0 ? Math.floor(budget[key] / 2) : budget[key] - Math.floor(budget[key] / 2);
|
||||
const allocate = key => engine ? budget[key] : index === 0 ? Math.floor(budget[key] / 2) : budget[key] - Math.floor(budget[key] / 2);
|
||||
return { label: text(item.label, 64, '子任务名称'), goal: text(item.goal, 8000, '子目标'), paths,
|
||||
budget: Object.fromEntries(['maxTokens', 'maxSteps', 'maxToolCalls'].map(key => [key, bounded(part[key], allocate(key), key === 'maxTokens' ? 500 : 1, budget[key], key)])) };
|
||||
});
|
||||
for (const key of ['maxTokens', 'maxSteps', 'maxToolCalls']) if (children.reduce((sum, child) => sum + child.budget[key], 0) > budget[key]) throw workerConflict('子任务预算之和不能超过父预算:' + key);
|
||||
return { goal: text(input.goal, 20000, '父目标'), budget, children };
|
||||
return { goal: text(input.goal, 20000, '父目标'), budget, children, ...(engine ? { engine } : {}) };
|
||||
}
|
||||
|
||||
export function collaborationSeal(group) {
|
||||
@@ -64,7 +66,8 @@ export function workerSeal(worker) {
|
||||
parentSessionId: worker.parentSessionId, parentTaskId: worker.parentTaskId, parentRunId: worker.parentRunId,
|
||||
projectId: worker.projectId, sessionId: worker.sessionId, taskId: worker.taskId, workspaceId: worker.workspaceId,
|
||||
label: worker.label, goal: worker.goal, paths: worker.paths, budget: worker.budget, model: worker.model, baseCommit: worker.baseCommit,
|
||||
baselineArtifactId: worker.baselineArtifactId, baselineSha256: worker.baselineSha256, baselineRunId: worker.baselineRunId };
|
||||
baselineArtifactId: worker.baselineArtifactId, baselineSha256: worker.baselineSha256, baselineRunId: worker.baselineRunId,
|
||||
...(worker.engine ? { engine: worker.engine } : {}) };
|
||||
}
|
||||
export function workerForProject(store, projectId) {
|
||||
return [...(store.core.worker?.values() ?? [])].find(value => value.projectId === projectId) ?? null;
|
||||
@@ -79,7 +82,8 @@ export function workerForSession(store, session) {
|
||||
|| definition.taskId !== worker.taskId || definition.projectId !== worker.projectId || definition.sessionId !== worker.sessionId
|
||||
|| definition.workspaceId !== worker.workspaceId || JSON.stringify(definition.paths) !== JSON.stringify(worker.paths)
|
||||
|| JSON.stringify(definition.budget) !== JSON.stringify(worker.budget) || definition.goal !== worker.goal
|
||||
|| JSON.stringify(group.model) !== JSON.stringify(worker.model)) throw workerConflict('子任务与父任务的封存分工不一致。');
|
||||
|| JSON.stringify(group.model) !== JSON.stringify(worker.model)
|
||||
|| JSON.stringify(definition.engine) !== JSON.stringify(worker.engine)) throw workerConflict('子任务与父任务的封存分工不一致。');
|
||||
const task = store.core.task.get(worker.taskId);
|
||||
if (!task || task.goal !== worker.goal || task.workerId !== worker.id || task.budget.maxTokens !== worker.budget.maxTokens) throw workerConflict('子任务目标、身份或累计预算已变化。');
|
||||
const original = store.owned('artifact', worker.contractArtifactId, session.id);
|
||||
|
||||
Reference in New Issue
Block a user