feat: add task records and verified engineering check jobs
This commit is contained in:
+35
-9
@@ -7,17 +7,20 @@ import { projectContext, projectMessages } from './provider.mjs';
|
||||
import { now, repairToolPairs } from './store.mjs';
|
||||
import { executionEnvironment, sessionContext, sessionSnapshot } from './diagnostics.mjs';
|
||||
import { BudgetExceeded, checkBudget } from '../shared/contracts/execution.mjs';
|
||||
import { CheckRunner } from '../core/check-runner.mjs';
|
||||
import { inspectProject } from '../core/project-inspector.mjs';
|
||||
|
||||
const labels = { inspect_session: '核对工作台运行记录', list_files: '查看项目文件', read_file: '读取文件', search_files: '搜索项目', propose_file: '准备文件改动', run_command: '运行终端命令' };
|
||||
const labels = { inspect_session: '核对工作台运行记录', inspect_project: '识别工程与Git基线', plan_checks: '准备工程检查计划', run_checks: '执行工程检查', list_files: '查看项目文件', read_file: '读取文件', search_files: '搜索项目', propose_file: '准备文件改动', run_command: '运行终端命令' };
|
||||
|
||||
export function validateToolArgs(tool, args) {
|
||||
if (!Object.hasOwn(labels, tool)) throw new Error('当前能力表没有工具:' + tool);
|
||||
if (!args || typeof args !== 'object' || Array.isArray(args)) throw new Error('工具参数必须是 JSON 对象。');
|
||||
const required = tool === 'inspect_session' ? [] : tool === 'list_files' || tool === 'read_file' ? ['path'] : tool === 'search_files' ? ['query']
|
||||
const required = ['inspect_session', 'inspect_project', 'plan_checks'].includes(tool) ? [] : tool === 'run_checks' ? ['plan_id'] : tool === 'list_files' || tool === 'read_file' ? ['path'] : tool === 'search_files' ? ['query']
|
||||
: tool === 'propose_file' ? ['path', 'content', 'reason'] : ['command', 'reason'];
|
||||
for (const field of required) if (typeof args[field] !== 'string') throw new Error('工具参数 ' + field + ' 必须是文本。');
|
||||
for (const field of ['depth', 'start_line', 'end_line']) if (args[field] !== undefined && (!Number.isInteger(args[field]) || args[field] < 0)) throw new Error('工具参数 ' + field + ' 必须是非负整数。');
|
||||
if (args.case_sensitive !== undefined && typeof args.case_sensitive !== 'boolean') throw new Error('case_sensitive 必须是布尔值。');
|
||||
if (tool === 'plan_checks' && args.names !== undefined && (!Array.isArray(args.names) || args.names.some(name => typeof name !== 'string'))) throw new Error('检查脚本名称需要文本数组。');
|
||||
}
|
||||
|
||||
export class Runtime {
|
||||
@@ -25,10 +28,17 @@ export class Runtime {
|
||||
this.store = store; this.provider = provider;
|
||||
this.active = new Map(); this.waiters = new Map();
|
||||
}
|
||||
isActive(id) { return this.active.has(id); }
|
||||
isActive(id) { return this.active.has(id) || !!this.checks?.isActive(id); }
|
||||
hasActivity() { return this.active.size > 0 || (this.checks?.active.size ?? 0) > 0; }
|
||||
projectHasActivity(projectId, exceptSessionId = null) {
|
||||
return [...this.active.entries()].some(([id, run]) => id !== exceptSessionId && run.project.id === projectId) || !!this.checks?.projectActive(projectId);
|
||||
}
|
||||
checkRunner() {
|
||||
return this.checks ??= new CheckRunner(this.store, { redact: value => this.provider.redact(value), projectBusy: (projectId, exceptId) => [...this.active.entries()].some(([id, run]) => id !== exceptId && run.project.id === projectId) });
|
||||
}
|
||||
start(id, content, resume = false) {
|
||||
const session = this.store.session(id);
|
||||
if (this.active.has(id)) throw Object.assign(new Error('这个会话已有任务在执行。'), { status: 409 });
|
||||
if (this.isActive(id) || this.checks?.projectActive(session.projectId)) throw Object.assign(new Error('这个会话或项目已有任务/检查在执行。'), { status: 409 });
|
||||
if (!resume && (typeof content !== 'string' || !content.trim() || content.length > 20000)) throw new Error('任务内容不能为空,也不能超过 20,000 字符。');
|
||||
const project = this.store.project(session.projectId);
|
||||
const controller = new AbortController();
|
||||
@@ -167,16 +177,30 @@ export class Runtime {
|
||||
const args = JSON.parse(call.function.arguments);
|
||||
validateToolArgs(tool, args);
|
||||
event.input = JSON.parse(this.provider.redact(JSON.stringify(args)));
|
||||
if (tool !== 'run_command') this.store.invocationState(session, event, 'running');
|
||||
if (!['run_command', 'run_checks'].includes(tool)) this.store.invocationState(session, event, 'running');
|
||||
else this.store.save(session);
|
||||
if (tool === 'inspect_session') result = { ...sessionSnapshot(session, run.project, run.config, value => this.provider.redact(value)), execution: this.store.executionSnapshot(session) };
|
||||
else if (tool === 'inspect_project') result = await inspectProject(run.project);
|
||||
else if (tool === 'plan_checks') result = await this.checkRunner().plan(session, args.names);
|
||||
else if (tool === 'run_checks') {
|
||||
const plan = this.checkRunner().planFor(session, args.plan_id);
|
||||
result = await this.requestCommand(session, event, { command: plan.checks.map(check => check.command).join('\n'), checkPlanId: plan.id,
|
||||
checkScripts: plan.checks.map(check => ({ name: check.name, script: check.script, lifecycle: check.lifecycle })),
|
||||
reason: '执行工程检查计划 ' + plan.id.slice(0, 8) + ';保存测试数量、退出码和日志。' }, run, async () => {
|
||||
const { promise } = await this.checkRunner().start(session, plan.id, { signal: run.controller.signal, source: 'agent' });
|
||||
const job = await promise;
|
||||
return { status: job.status === 'cancelled' ? 'interrupted' : job.status === 'passed' ? 'completed' : job.status,
|
||||
check_status: job.status, job_id: job.id, plan_id: job.planId, results: job.results, summary: job.summary,
|
||||
requires_verification: job.requiresVerification === true, fingerprint: job.fingerprint };
|
||||
});
|
||||
}
|
||||
else if (tool === 'list_files') result = listFiles(run.project, args.path, args.depth ?? 3, 500);
|
||||
else if (tool === 'read_file') result = readLines(run.project, args);
|
||||
else if (tool === 'search_files') result = searchFiles(run.project, args);
|
||||
else if (tool === 'propose_file') result = proposeFile(this.store, session, args);
|
||||
else if (tool === 'run_command') result = await this.requestCommand(session, event, args, run);
|
||||
else throw new Error('当前能力表没有工具:' + tool);
|
||||
event.phase = ['denied', 'interrupted'].includes(result.status) ? result.status : result.status === 'failed' ? 'failed' : 'completed';
|
||||
event.phase = ['denied', 'interrupted', 'unknown', 'unverified', 'stale'].includes(result.status) ? result.status : result.status === 'failed' ? 'failed' : 'completed';
|
||||
if (typeof result.stdout === 'string') { result.stdout = this.provider.redact(result.stdout); result.stderr = this.provider.redact(result.stderr ?? ''); }
|
||||
event.output = this.provider.redact(typeof result.stdout === 'string' ? result.stdout + (result.stderr ? '\n' + result.stderr : '') : JSON.stringify(result).slice(0, 16000));
|
||||
if (result.exit_code !== undefined) event.exitCode = result.exit_code;
|
||||
@@ -191,11 +215,12 @@ export class Runtime {
|
||||
this.store.finishInvocation(session, event, result);
|
||||
return result;
|
||||
}
|
||||
async requestCommand(session, event, args, run) {
|
||||
async requestCommand(session, event, args, run, execute) {
|
||||
const command = String(args.command ?? '').trim();
|
||||
if (!command || command.length > 6000) throw new Error('终端命令不能为空,也不能超过 6,000 字符。');
|
||||
const approval = { id: randomUUID(), toolCallId: event.toolCallId, eventId: event.id, invocationId: event.invocationId,
|
||||
taskId: run.taskId, runId: run.id, projectId: session.projectId, command,
|
||||
...(execute ? { checkPlanId: args.checkPlanId, checkScripts: args.checkScripts } : {}),
|
||||
reason: String(args.reason ?? '').slice(0, 800), cwd: run.project.root, shell: executionEnvironment().shell, status: 'pending', createdAt: now() };
|
||||
session.approvals.push(approval);
|
||||
event.phase = 'waiting'; session.status = 'awaiting_approval';
|
||||
@@ -213,6 +238,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 (execute) return execute();
|
||||
return this.executeCommand(command, cwd, run.controller.signal, output => {
|
||||
event.output = output; this.store.save(session);
|
||||
});
|
||||
@@ -274,11 +300,11 @@ export class Runtime {
|
||||
}
|
||||
async stop(sessionId) {
|
||||
const run = this.active.get(sessionId);
|
||||
if (!run) return;
|
||||
if (!run) { await this.checks?.stop(sessionId); return; }
|
||||
const session = this.store.session(sessionId);
|
||||
session.status = 'stopping'; this.store.save(session);
|
||||
run.controller.abort(new Error('用户停止任务'));
|
||||
await run.promise;
|
||||
}
|
||||
async stopAll() { await Promise.all([...this.active.keys()].map(id => this.stop(id))); }
|
||||
async stopAll() { await Promise.all([...this.active.keys()].map(id => this.stop(id))); await this.checks?.stopAll(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user