feat: persist execution state and tool results in SQLite

This commit is contained in:
Agent Workbench Dev
2026-10-29 13:11:30 +08:00
parent b047665a33
commit 93f4d90ccb
21 changed files with 1125 additions and 81 deletions
+12
View File
@@ -1,5 +1,17 @@
# 汇总发现
## M1开发核对:2026-10-29
- 当前main=b047665且工作区干净。已读运行/Store/API/升级入口;开发仍使用Node24.22.1,计划采用内置node:sqlite减少额外依赖。
- Store/Runtime/API通过修改session后save形成现有契约;新内核先保留会话投影兼容,SQLite同时保存任务/运行/工具身份和追加事件,不能做JSON与数据库互为独立真相的双写。
- 现有热升级只替换对象原型;SQLite资源与新版Runtime状态须在受控升级中初始化。原provider对象保持原身份和内存连接,独立fixture先验证。
- 工具完成事件早于providerMessages结果保存,崩溃可能将实际已完成工具修成“未知”;新工具账本与结果恢复是本轮必要验收。
- 新内核已写入独立Task/Run/Step/Tool与工件、SQLite状态/事件事务、JSON原字节备份和恢复;Runtime接入入场登记、实际结果账本、冻结模型配置、累计usage/预算、明确审批归属。
- 首轮Store/Runtime12项检查11通过,唯一失败为旧测试扫描已不再生成的sessions JSON目录;已改为检查实际数据库/结果工件中的Key缺失。其余运行、拒绝/取消、UTF-8与恢复行为通过。
- 随后25项针对性与完整47项回归通过,真实进程退出覆盖迁移、状态/事件事务、目标接纳、队列、审批、进入执行、实际副作用、结果持久化和下一次模型请求;已完成副作用均未重放。
- 新增时间预算、损坏工件与HTTP事件补发后,20项专项全部通过。SQLite是权威状态;旧JSON作为原字节备份/显式回退导出,配置镜像不会覆盖已迁移数据库。
- 后端持久seq、Last-Event-ID、分页历史与工件归属已验证;前端任务记录、工程检查/交付样本和实际服务升级尚未实现。内核代码保存在开发分支,当前真实服务仍为1.0.0/PID23080。
## 后续能力研究与产品规划:2026-10-29
- 用户确认本项目独立研究编程代理核心能力,其他项目不纳入后续计划。
+9
View File
@@ -1,5 +1,14 @@
# 统一执行进度
## 2026-10-29 M1可靠内核开发
- 用户批准下一步开发;从规划提交b047665开始,核对Store/Runtime/API/升级机制与Node版本,启动可靠任务状态和首个工程交付切片。
- 本轮开发计划已追加,原4318服务与真实内存连接保持。独立数据/端口用于开发验证。
- 已实现Task/Run/Step/Tool/Event/Artifact契约、SQLite事务状态与outbox、JSON迁移/原字节备份、工具实际结果恢复、冻结模型配置、累计usage/时间与token预算、严格审批归属。
- 后端增加持久事件补发/分页、执行快照与hash验证工件;升级器支持初始化原Store和保留连接的旧版迁移,独立0.1.0进程实测通过。
- 针对性25/25、完整构建与回归47/47通过;追加三类检查后的20/20专项通过,均0失败/跳过。后台测试已退出,原4318/PID23080保持。
- 本轮执行被中断,按已授权的小步Git方式保存内核检查点。界面、工程识别/TestRunner/Git样本及实际升级保持待办;接续从这些项推进,不重复已通过的证据研究。
## 2026-10-29
- 用户指定合并两个研究方向与产品执行入口。
- 读取另一对话与其研究文件,确认同一工作区、Cursor 目标与已完成边界。
+17 -13
View File
@@ -45,16 +45,17 @@ function sameTarget(target, file) {
return target.title === displayName && target.url === 'file://' + displayName;
}
export async function upgradeRunning({ url, pid, currentRoot, newRoot }) {
export async function upgradeRunning({ url, pid, currentRoot, newRoot, handlerRoot }) {
const origin = localUrl(url), original = fs.realpathSync.native(currentRoot), next = fs.realpathSync.native(newRoot);
const originalScript = path.join(original, 'dist-server', 'server.mjs');
const upgradeScript = path.join(next, 'dist-server', 'hot-upgrade.mjs');
if (!fs.existsSync(originalScript) || !fs.existsSync(upgradeScript) || !fs.existsSync(path.join(next, 'dist-client', 'index.html'))) throw new Error('Upgrade bundles are incomplete.');
if (!Number.isInteger(pid) || pid < 1 || pid === process.pid) throw new Error('Invalid target process.');
const before = await (await fetch(origin + '/api/bootstrap', { signal: AbortSignal.timeout(5000) })).json();
if (!before.token || before.version !== '0.1.0') throw new Error('This migration accepts an existing 0.1.0 Workbench.');
if (!before.token || !['0.1.0', '1.0.0', '1.1.0'].includes(before.version)) throw new Error('This migration accepts a supported existing Workbench.');
if (before.sessions.some(session => ['running', 'awaiting_approval', 'stopping'].includes(session.status))) throw new Error('Wait for existing tasks and approvals before upgrading.');
let protocol, socket, enabledHere = false, paused = false, breakpoint;
let protocol, socket, enabledHere = false, paused = false;
const breakpoints = [];
try {
let targets;
try { targets = await (await fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(500) })).json(); } catch {}
@@ -80,20 +81,23 @@ export async function upgradeRunning({ url, pid, currentRoot, newRoot }) {
const identity = await protocol.send('Runtime.evaluate', { expression: 'process.pid', returnByValue: true });
if (identity.result.value !== pid) throw new Error('The inspector belongs to a different process.');
await protocol.send('Debugger.enable');
const script = scripts.find(item => sameFile(item.url, originalScript));
if (!script) throw new Error('The existing server module is missing from inspector metadata.');
const { scriptSource } = await protocol.send('Debugger.getScriptSource', { scriptId: script.scriptId });
const lines = scriptSource.split('\n');
const locations = lines.flatMap((line, index) => line.includes('const url = new URL(request.url') ? [index] : []);
if (locations.length !== 1) throw new Error('The existing request handler could not be identified uniquely.');
breakpoint = (await protocol.send('Debugger.setBreakpoint', { location: { scriptId: script.scriptId, lineNumber: locations[0] } })).breakpointId;
const previousRoot = handlerRoot || before.runtimeRoot;
const expectedScripts = [originalScript, ...(previousRoot ? [path.join(fs.realpathSync.native(previousRoot), 'dist-server', 'hot-upgrade.mjs')] : [])];
const candidates = scripts.filter(item => expectedScripts.some(file => sameFile(item.url, file)));
if (!candidates.length) throw new Error('The existing server module is missing from inspector metadata.');
for (const script of candidates) {
const { scriptSource } = await protocol.send('Debugger.getScriptSource', { scriptId: script.scriptId });
const locations = scriptSource.split('\n').flatMap((line, index) => line.includes('const url = new URL(request.url') ? [index] : []);
if (locations.length !== 1) throw new Error('The existing request handler could not be identified uniquely.');
breakpoints.push((await protocol.send('Debugger.setBreakpoint', { location: { scriptId: script.scriptId, lineNumber: locations[0] } })).breakpointId);
}
const probe = fetch(origin + '/api/health', { signal: AbortSignal.timeout(15000) });
probe.catch(() => {});
const event = await within(pausedEvent, 10000, 'Workbench request observation');
const captured = await protocol.send('Debugger.evaluateOnCallFrame', { callFrameId: event.callFrames[0].callFrameId,
expression: "globalThis.__awUpgradeContext = { server, store, provider, runtime, token, clients, started, exampleProject, shutdown }; 'context-ready'", returnByValue: true });
if (captured.exceptionDetails || captured.result.value !== 'context-ready') throw new Error('Could not retain the existing runtime objects.');
await protocol.send('Debugger.removeBreakpoint', { breakpointId: breakpoint }); breakpoint = null;
for (const breakpointId of breakpoints.splice(0)) await protocol.send('Debugger.removeBreakpoint', { breakpointId });
await protocol.send('Debugger.resume'); paused = false; await probe;
const expression = '(() => { try { const module = process.getBuiltinModule("module").createRequire(' + JSON.stringify(upgradeScript) + ')(' + JSON.stringify(upgradeScript) + '); return module.installUpgrade(globalThis.__awUpgradeContext, { appDirectory: ' + JSON.stringify(next) + ' }); } finally { delete globalThis.__awUpgradeContext; } })()';
const upgraded = await protocol.send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true });
@@ -108,7 +112,7 @@ export async function upgradeRunning({ url, pid, currentRoot, newRoot }) {
return { ...result, verified: true, connectionPreserved: true, url: origin };
} finally {
if (protocol) {
try { if (breakpoint) await protocol.send('Debugger.removeBreakpoint', { breakpointId: breakpoint }); } catch {}
for (const breakpointId of breakpoints) { try { await protocol.send('Debugger.removeBreakpoint', { breakpointId }); } catch {} }
try { if (paused) await protocol.send('Debugger.resume'); } catch {}
try { await protocol.send('Runtime.evaluate', { expression: 'delete globalThis.__awUpgradeContext; true', returnByValue: true }); } catch {}
if (enabledHere) {
@@ -124,6 +128,6 @@ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.me
const args = Object.fromEntries(process.argv.slice(2).reduce((items, value, index, all) => {
if (value.startsWith('--')) items.push([value.slice(2), all[index + 1]]); return items;
}, []));
const result = await upgradeRunning({ url: args.url || 'http://127.0.0.1:4318', pid: Number(args.pid), currentRoot: args['current-root'], newRoot: args['new-root'] });
const result = await upgradeRunning({ url: args.url || 'http://127.0.0.1:4318', pid: Number(args.pid), currentRoot: args['current-root'], newRoot: args['new-root'], handlerRoot: args['handler-root'] });
console.log(JSON.stringify(result, null, 2));
}
+35
View File
@@ -0,0 +1,35 @@
import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
export class ArtifactStore {
constructor(directory) {
this.root = path.join(directory, 'artifacts'); fs.mkdirSync(this.root, { recursive: true });
if (fs.lstatSync(this.root).isSymbolicLink()) throw new Error('执行工件目录不能是链接。');
}
write(session, run, value, { kind = 'tool-result', mediaType = 'application/json' } = {}) {
const bytes = Buffer.from(typeof value === 'string' ? value : JSON.stringify(value), 'utf8');
if (bytes.length > 8 * 1024 * 1024) throw new Error('单项执行工件超过8MB,请缩小输出范围。');
const sha256 = createHash('sha256').update(bytes).digest('hex');
const relative = sha256.slice(0, 2) + '/' + sha256 + '.blob';
const file = path.join(this.root, relative);
fs.mkdirSync(path.dirname(file), { recursive: true });
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.existsSync(file) && fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能通过链接写入。');
if (!fs.existsSync(file)) {
const temporary = file + '.' + randomUUID() + '.tmp';
const handle = fs.openSync(temporary, 'wx', 0o600);
try { fs.writeFileSync(handle, bytes); fs.fsyncSync(handle); } finally { fs.closeSync(handle); }
try { fs.renameSync(temporary, file); } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
}
return { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run?.taskId ?? session.taskId ?? null,
runId: run?.id ?? session.runId ?? null, kind, mediaType, sha256, bytes: bytes.length, relative, createdAt: new Date().toISOString(), status: 'stored' };
}
read(artifact) {
if (!/^[a-f0-9]{2}\/[a-f0-9]{64}\.blob$/.test(artifact.relative)) throw new Error('执行工件路径无效。');
const file = path.join(this.root, artifact.relative);
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能是链接。');
const bytes = fs.readFileSync(file);
if (bytes.length !== artifact.bytes || createHash('sha256').update(bytes).digest('hex') !== artifact.sha256) throw new Error('执行工件校验失败,实际结果需要核对。');
return bytes.toString('utf8');
}
}
+78
View File
@@ -0,0 +1,78 @@
import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
import { isId } from '../../shared/contracts/execution.mjs';
export const DEFAULT_CONFIG = {
mode: 'demo', baseUrl: 'http://127.0.0.1:11434/v1', model: '', contextLimit: 16000, maxSteps: 12, maxOutputTokens: 2048,
maxRunDurationMs: 600000, maxRunTokens: 200000, maxTaskTokens: 1000000, configRevision: 1,
};
export function cleanConfig(value = {}) {
const result = { ...DEFAULT_CONFIG };
for (const key of Object.keys(result)) if (Object.hasOwn(value, key)) result[key] = value[key];
return result;
}
function readLegacy(file, fallback, notices) {
let found = false;
for (const candidate of [file, file + '.bak']) {
if (!fs.existsSync(candidate)) continue;
found = true;
if (fs.lstatSync(candidate).isSymbolicLink() || fs.statSync(candidate).size > 32 * 1024 * 1024) throw new Error('旧状态文件是链接或超过迁移读取上限。');
try {
const value = JSON.parse(fs.readFileSync(candidate, 'utf8'));
if (candidate !== file) notices.push({ file: path.basename(file), recovery: 'backup' });
return value;
} catch { notices.push({ file: path.basename(candidate), recovery: 'invalid-json-preserved' }); }
}
if (found) throw new Error('旧状态文件无法解析,原文件已保留:' + path.basename(file));
return structuredClone(fallback);
}
export function collectLegacy(directory, existing = null) {
const notices = [];
const config = existing ? existing.config : readLegacy(path.join(directory, 'config.json'), DEFAULT_CONFIG, notices);
const projects = existing ? existing.projects : readLegacy(path.join(directory, 'projects.json'), [], notices);
const sessions = [];
const sessionRoot = path.join(directory, 'sessions');
const names = fs.existsSync(sessionRoot) ? fs.readdirSync(sessionRoot) : [];
if (existing) sessions.push(...[...existing.sessions.values()].map(value => structuredClone(value)));
else for (const name of names) {
if (!/^[a-f0-9-]+\.json$/i.test(name)) continue;
const value = readLegacy(path.join(sessionRoot, name), null, notices);
if (!value) continue;
if (!isId(value.id) || name.toLowerCase() !== value.id.toLowerCase() + '.json' || !isId(value.projectId)) throw new Error('旧会话身份不符合迁移契约,原文件已保留。');
sessions.push(value);
}
if (!Array.isArray(projects) || projects.some(project => !isId(project.id) || typeof project.root !== 'string')) throw new Error('旧项目清单无效,原数据已保留。');
const projectIds = new Set(projects.map(project => project.id));
for (const session of sessions) {
if (!projectIds.has(session.projectId)) throw new Error('旧会话找不到所属项目,原数据已保留。');
for (const field of ['messages', 'providerMessages', 'events', 'changes', 'approvals']) {
if (session[field] === undefined) session[field] = [];
if (!Array.isArray(session[field])) throw new Error('旧会话集合无效:' + field);
}
}
const sourceFiles = [path.join(directory, 'config.json'), path.join(directory, 'projects.json'), ...names.filter(name => /^[a-f0-9-]+\.json(?:\.bak)?$/i.test(name)).map(name => path.join(sessionRoot, name))];
for (const file of [...sourceFiles]) if (fs.existsSync(file + '.bak') && !sourceFiles.includes(file + '.bak')) sourceFiles.push(file + '.bak');
const existingFiles = sourceFiles.filter(file => fs.existsSync(file));
let migration = null;
if (existingFiles.length) {
const id = 'json-v1-' + Date.now() + '-' + randomUUID();
const backupRoot = path.join(directory, 'legacy-backups', id);
fs.mkdirSync(backupRoot, { recursive: true });
const files = existingFiles.map(file => {
const relative = path.relative(directory, file);
if (relative.startsWith('..') || path.isAbsolute(relative) || fs.lstatSync(file).isSymbolicLink()) throw new Error('迁移备份路径无效。');
const bytes = fs.readFileSync(file), target = path.join(backupRoot, relative);
fs.mkdirSync(path.dirname(target), { recursive: true });
const handle = fs.openSync(target, 'wx', 0o600);
try { fs.writeFileSync(handle, bytes); fs.fsyncSync(handle); } finally { fs.closeSync(handle); }
return { path: relative.replaceAll('\\', '/'), bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') };
});
migration = { id, source: 'json-v1', backupRelative: path.relative(directory, backupRoot).replaceAll('\\', '/'), files, notices, createdAt: new Date().toISOString() };
fs.writeFileSync(path.join(backupRoot, 'manifest.json'), JSON.stringify(migration, null, 2), { encoding: 'utf8', flag: 'wx' });
}
return { config: cleanConfig(config), projects, sessions, migration };
}
+130
View File
@@ -0,0 +1,130 @@
import fs from 'node:fs';
import path from 'node:path';
import { DatabaseSync } from 'node:sqlite';
import { randomUUID } from 'node:crypto';
const DATABASE_SCHEMA = 1;
export class StateDatabase {
constructor(directory, { faultInjector } = {}) {
this.filename = path.join(directory, 'workbench.sqlite');
this.faultInjector = faultInjector;
this.database = new DatabaseSync(this.filename);
try {
const version = this.database.prepare('PRAGMA user_version').get().user_version;
if (version > DATABASE_SCHEMA) throw new Error('任务数据库由更新版本创建,请使用对应版本打开。');
this.database.exec('PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000;');
this.database.exec(`
CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT NOT NULL CHECK(json_valid(value)));
CREATE TABLE IF NOT EXISTS projects (id TEXT PRIMARY KEY, position INTEGER NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload)));
CREATE TABLE IF NOT EXISTS sessions (id TEXT PRIMARY KEY, project_id TEXT NOT NULL REFERENCES projects(id), revision INTEGER NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload)));
CREATE TABLE IF NOT EXISTS records (
kind TEXT NOT NULL, id TEXT NOT NULL, session_id TEXT NOT NULL REFERENCES sessions(id), project_id TEXT NOT NULL REFERENCES projects(id),
task_id TEXT, run_id TEXT, status TEXT, payload TEXT NOT NULL CHECK(json_valid(payload)), PRIMARY KEY(kind,id));
CREATE INDEX IF NOT EXISTS records_owner ON records(kind,session_id,task_id,run_id);
CREATE TABLE IF NOT EXISTS events (
seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL UNIQUE, session_id TEXT NOT NULL REFERENCES sessions(id), project_id TEXT NOT NULL,
task_id TEXT, run_id TEXT, type TEXT NOT NULL, revision INTEGER NOT NULL, created_at TEXT NOT NULL, payload TEXT NOT NULL CHECK(json_valid(payload)));
CREATE INDEX IF NOT EXISTS events_session ON events(session_id,seq);
CREATE TABLE IF NOT EXISTS outbox (seq INTEGER PRIMARY KEY REFERENCES events(seq), published_at TEXT);
PRAGMA user_version=${DATABASE_SCHEMA};
`);
} catch (error) { this.database.close(); throw error; }
}
transaction(action) {
this.database.exec('BEGIN IMMEDIATE');
try { const result = action(); this.faultInjector?.('before_commit'); this.database.exec('COMMIT'); return result; }
catch (error) { try { this.database.exec('ROLLBACK'); } catch {} throw error; }
}
meta(key, fallback = null) {
const row = this.database.prepare('SELECT value FROM metadata WHERE key=?').get(key);
return row ? JSON.parse(row.value) : structuredClone(fallback);
}
setMeta(key, value) {
this.database.prepare('INSERT INTO metadata(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value').run(key, JSON.stringify(value));
}
get initialized() { return this.meta('initialized', false); }
get cursor() { return this.database.prepare('SELECT COALESCE(MAX(seq),0) AS seq FROM events').get().seq; }
writeProjects(projects) {
const statement = this.database.prepare('INSERT INTO projects(id,position,payload) VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET position=excluded.position,payload=excluded.payload');
projects.forEach((project, index) => statement.run(project.id, index, JSON.stringify(project)));
}
writeSession(session) {
const existing = this.database.prepare('SELECT project_id FROM sessions WHERE id=?').get(session.id);
if (existing && existing.project_id !== session.projectId) throw new Error('会话不能移动到其他项目。');
this.database.prepare('INSERT INTO sessions(id,project_id,revision,payload) VALUES(?,?,?,?) ON CONFLICT(id) DO UPDATE SET revision=excluded.revision,payload=excluded.payload').run(
session.id, session.projectId, session.revision ?? 0, JSON.stringify(session));
}
writeRecords(records) {
const statement = this.database.prepare(`INSERT INTO records(kind,id,session_id,project_id,task_id,run_id,status,payload) VALUES(?,?,?,?,?,?,?,?)
ON CONFLICT(kind,id) DO UPDATE SET status=excluded.status,payload=excluded.payload`);
for (const record of records) {
const existing = this.database.prepare('SELECT session_id,project_id,task_id,run_id FROM records WHERE kind=? AND id=?').get(record.kind, record.id);
if (existing && (existing.session_id !== record.sessionId || existing.project_id !== record.projectId || existing.task_id !== record.taskId || existing.run_id !== record.runId)) {
throw new Error('持久执行记录不能移动到其他任务或项目。');
}
statement.run(record.kind, record.id, record.sessionId, record.projectId, record.taskId, record.runId, record.status, JSON.stringify(record.value));
}
}
appendEvent(session, type, payload) {
const entry = { id: randomUUID(), sessionId: session.id, projectId: session.projectId,
taskId: session.taskId ?? null, runId: session.runId ?? null, revision: session.revision ?? 0,
type, createdAt: new Date().toISOString(), payload };
const result = this.database.prepare('INSERT INTO events(id,session_id,project_id,task_id,run_id,type,revision,created_at,payload) VALUES(?,?,?,?,?,?,?,?,?)').run(
entry.id, entry.sessionId, entry.projectId, entry.taskId, entry.runId, type, entry.revision, entry.createdAt, JSON.stringify(payload));
entry.seq = Number(result.lastInsertRowid);
this.database.prepare('INSERT INTO outbox(seq) VALUES(?)').run(entry.seq);
return entry;
}
initialize({ projects, config, sessions, records, migration }) {
return this.transaction(() => {
this.writeProjects(projects); this.setMeta('config', config);
for (const session of sessions) {
this.writeSession(session);
session.eventSeq = this.appendEvent(session, 'session.imported', { status: session.status, source: migration ? 'json-v1' : 'new' }).seq;
this.writeSession(session);
}
this.writeRecords(records); this.faultInjector?.('migration_records');
this.setMeta('migration', migration); this.setMeta('initialized', true);
});
}
save(session, records, type, payload) {
return this.transaction(() => {
this.writeSession(session); this.writeRecords(records); this.faultInjector?.('after_state');
const event = this.appendEvent(session, type, payload);
session.eventSeq = event.seq; this.writeSession(session); this.faultInjector?.('after_event');
return event;
});
}
load() {
return {
projects: this.database.prepare('SELECT payload FROM projects ORDER BY position').all().map(row => JSON.parse(row.payload)),
config: this.meta('config', {}),
sessions: this.database.prepare('SELECT payload FROM sessions').all().map(row => JSON.parse(row.payload)),
records: this.database.prepare('SELECT kind,payload FROM records').all().map(row => ({ kind: row.kind, value: JSON.parse(row.payload) })),
};
}
eventsAfter(cursor = 0, { sessionId = null, limit = 100 } = {}) {
if (!Number.isSafeInteger(cursor) || cursor < 0 || !Number.isInteger(limit) || limit < 1 || limit > 500) throw new Error('事件游标或分页范围无效。');
const rows = sessionId
? this.database.prepare('SELECT * FROM events WHERE seq>? AND session_id=? ORDER BY seq LIMIT ?').all(cursor, sessionId, limit + 1)
: this.database.prepare('SELECT * FROM events WHERE seq>? ORDER BY seq LIMIT ?').all(cursor, limit + 1);
const events = rows.slice(0, limit).map(row => ({ seq: row.seq, id: row.id, sessionId: row.session_id, projectId: row.project_id,
taskId: row.task_id, runId: row.run_id, revision: row.revision, type: row.type, createdAt: row.created_at, payload: JSON.parse(row.payload) }));
return { events, nextCursor: events.at(-1)?.seq ?? cursor, hasMore: rows.length > limit, currentCursor: this.cursor };
}
published(seq) { this.database.prepare('UPDATE outbox SET published_at=? WHERE seq=?').run(new Date().toISOString(), seq); }
pendingPublications() { return this.database.prepare('SELECT COUNT(*) AS count FROM outbox WHERE published_at IS NULL').get().count; }
close() { if (this.database) { this.database.close(); this.database = null; } }
}
export function runtimeOwnerAlive(owner) {
if (!owner?.pid || owner.pid === process.pid) return false;
try { process.kill(owner.pid, 0); return true; } catch (error) { return error.code === 'EPERM'; }
}
+43 -3
View File
@@ -3,6 +3,22 @@ import path from 'node:path';
import { listFiles, readText, proposeFile, applyChange } from './files.mjs';
import { APP_VERSION, executionEnvironment, sessionSnapshot } from './diagnostics.mjs';
export function createSessionPublisher(clients) {
return session => {
const payload = (session.eventSeq ? 'id: ' + session.eventSeq + '\n' : '') + 'data: ' + JSON.stringify({ kind: 'session', seq: session.eventSeq, session }) + '\n\n';
for (const response of clients) {
if (response.destroyed || response.writableEnded || response.writableLength > 2 * 1024 * 1024) { response.destroy(); clients.delete(response); }
else response.write(payload);
}
};
}
function cursorNumber(value) {
if (value === undefined || value === null || value === '') return 0;
if (!/^\d+$/.test(String(value)) || !Number.isSafeInteger(Number(value))) throw new Error('事件游标无效。');
return Number(value);
}
export function createRequestHandler({ appDirectory, store, provider, runtime, token, clients, started, getPort, exampleProject, shutdown }) {
const research = [
{ id: 'overview', title: '统一机制报告', file: 'RESEARCH.md', description: '研究结论、证据与本产品采用的独立设计。' },
@@ -50,9 +66,10 @@ return async (request, response) => {
if (method === 'GET' && route === '/api/health') return json(response, { app: 'agent-workbench', version: APP_VERSION, pid: process.pid, ready: true, uptimeMs: Date.now() - started });
if (method === 'GET' && route === '/api/bootstrap') return json(response, {
token, version: APP_VERSION, config: { ...store.config, hasKey: provider.hasKey() },
eventCursor: store.eventCursor, storage: store.storageInfo(), runtimeRoot: appDirectory,
environment: executionEnvironment(),
projects: store.projects, sessions: store.listSessions(), exampleProjectId: exampleProject?.id ?? null,
capabilities: { fileChanges: 'review', commands: 'per-call-approval', recovery: 'explicit-resume', model: 'chat-completions-compatible' },
capabilities: { fileChanges: 'review', commands: 'per-call-approval', recovery: 'durable-tool-results', model: 'chat-completions-compatible', execution: 'task-run-step', events: 'persistent-cursor', artifacts: 'sha256-verified' },
});
if (method === 'GET' && route === '/api/diagnostics') {
const selected = url.searchParams.get('session');
@@ -60,16 +77,33 @@ return async (request, response) => {
return json(response, {
version: APP_VERSION, environment: executionEnvironment(),
connection: { mode: store.config.mode, endpoint: store.config.baseUrl, model: store.config.model, hasKey: provider.hasKey(), lastRequest: provider.lastRequest ?? null },
session: session ? sessionSnapshot(session, store.project(session.projectId), store.config, value => provider.redact(value)) : null,
storage: store.storageInfo(),
session: session ? { ...sessionSnapshot(session, store.project(session.projectId), store.config, value => provider.redact(value)), execution: store.executionSnapshot(session) } : null,
});
}
if (method === 'GET' && route === '/api/events') {
const cursor = Math.max(cursorNumber(url.searchParams.get('after')), cursorNumber(request.headers['last-event-id']));
if (cursor > store.eventCursor) throw Object.assign(new Error('事件游标超出当前历史,请重新加载工作台。'), { status: 409 });
response.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive', 'X-Accel-Buffering': 'no' });
clients.add(response); response.write('data: {"kind":"ready"}\n\n');
const history = store.eventsAfter(cursor, { limit: 500 });
for (const entry of history.events) response.write('id: ' + entry.seq + '\ndata: ' + JSON.stringify({ kind: 'session_changed', ...entry }) + '\n\n');
clients.add(response); response.write('data: ' + JSON.stringify({ kind: 'ready', eventCursor: store.eventCursor, replayIncomplete: history.hasMore }) + '\n\n');
const heartbeat = setInterval(() => response.write(': heartbeat\n\n'), 20000);
request.on('close', () => { clearInterval(heartbeat); clients.delete(response); });
return;
}
if (method === 'GET' && route === '/api/events/history') {
const selected = url.searchParams.get('session'); if (selected) store.session(selected);
return json(response, store.eventsAfter(cursorNumber(url.searchParams.get('after')), { sessionId: selected,
limit: url.searchParams.has('limit') ? cursorNumber(url.searchParams.get('limit')) : 100 }));
}
const artifactMatch = route.match(/^\/api\/artifacts\/([a-f0-9-]+)$/);
if (method === 'GET' && artifactMatch) {
const artifact = store.owned('artifact', artifactMatch[1], url.searchParams.get('session'));
const text = store.artifactStore.read(artifact), offset = cursorNumber(url.searchParams.get('offset'));
const limit = Math.min(64000, url.searchParams.has('limit') ? cursorNumber(url.searchParams.get('limit')) : 16000);
return json(response, { ...artifact, content: text.slice(offset, offset + limit), offset, totalCharacters: text.length, truncated: offset + limit < text.length });
}
if (method === 'POST' && route === '/api/projects') {
const input = await body(request);
if (typeof input.root !== 'string' || input.root.length > 2000) throw new Error('请输入项目文件夹的完整路径。');
@@ -92,6 +126,9 @@ return async (request, response) => {
const id = sessionMatch[1], action = sessionMatch[2] ?? '';
const session = store.session(id);
if (method === 'GET' && !action) return json(response, store.publicSession(session));
if (method === 'GET' && action === 'execution') return json(response, store.executionSnapshot(session));
const runMatch = action.match(/^runs\/([a-f0-9-]+)$/);
if (method === 'GET' && runMatch) return json(response, store.owned('run', runMatch[1], session.id));
if (method === 'GET' && action === 'export') {
response.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8',
'Content-Disposition': 'attachment; filename="agent-workbench-session-' + id + '.json"' });
@@ -153,6 +190,9 @@ return async (request, response) => {
contextLimit: Math.trunc(Math.max(1000, Math.min(200000, Number(input.contextLimit) || 16000))),
maxSteps: Math.trunc(Math.max(1, Math.min(30, Number(input.maxSteps) || 12))),
maxOutputTokens: Math.trunc(Math.max(256, Math.min(16000, Number(input.maxOutputTokens) || 2048))),
maxRunDurationMs: Math.trunc(Math.max(1000, Math.min(3600000, Number(input.maxRunDurationMs) || store.config.maxRunDurationMs))),
maxRunTokens: Math.trunc(Math.max(1000, Math.min(2000000, Number(input.maxRunTokens) || store.config.maxRunTokens))),
maxTaskTokens: Math.trunc(Math.max(1000, Math.min(10000000, Number(input.maxTaskTokens) || store.config.maxTaskTokens))),
};
provider.validate(config);
if (Object.hasOwn(input, 'apiKey')) provider.setKey(input.apiKey);
+15 -1
View File
@@ -3,7 +3,7 @@ import fs from 'node:fs';
import { Provider } from './provider.mjs';
import { Runtime } from './runtime.mjs';
import { Store, atomicJson, readJson } from './store.mjs';
import { createRequestHandler } from './app.mjs';
import { createRequestHandler, createSessionPublisher } from './app.mjs';
import { APP_VERSION } from './diagnostics.mjs';
// Keep the existing objects and their in-memory connection. No credential is
@@ -16,12 +16,17 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
if (!fs.existsSync(path.join(root, 'dist-client', 'index.html'))) throw new Error('The new client bundle is missing.');
const oldHandlers = server.listeners('request');
const oldPrototypes = [Object.getPrototypeOf(store), Object.getPrototypeOf(provider), Object.getPrototypeOf(runtime)];
const oldStoreProperties = { ...store };
const oldUpdates = store.listeners('update');
if (oldUpdates.length > 1) throw new Error('Unexpected Store observers; preserve their ownership before upgrading.');
const handler = createRequestHandler({ appDirectory: root, store, provider, runtime, token, clients, started,
exampleProject, shutdown, getPort: () => server.address().port });
try {
Object.setPrototypeOf(store, Store.prototype);
Object.setPrototypeOf(provider, Provider.prototype);
Object.setPrototypeOf(runtime, Runtime.prototype);
store.ensureStorage(); store.claimRuntime();
store.removeAllListeners('update'); store.on('update', createSessionPublisher(clients));
server.removeAllListeners('request'); server.on('request', handler);
if (updateServiceInfo) {
const file = path.join(store.directory, 'service-info.json');
@@ -30,7 +35,16 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
}
return { version: APP_VERSION, pid: process.pid, hasKey: provider.hasKey(), projects: store.projects.length, sessions: store.sessions.size, appDirectory: root };
} catch (error) {
if (store.storage && store.storage !== oldStoreProperties.storage) {
const filename = store.storage.filename;
store.close();
if (path.resolve(filename) !== path.join(path.resolve(oldStoreProperties.directory), 'workbench.sqlite')) throw new Error('Upgrade rollback database path is invalid.');
for (const suffix of ['', '-wal', '-shm']) if (fs.existsSync(filename + suffix)) fs.renameSync(filename + suffix, filename + '.rolledback-' + Date.now() + suffix);
}
for (const key of Object.keys(store)) if (!Object.hasOwn(oldStoreProperties, key)) delete store[key];
Object.assign(store, oldStoreProperties);
Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]);
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
throw error;
}
+5 -11
View File
@@ -7,13 +7,14 @@ import { fileURLToPath } from 'node:url';
import { Store } from './store.mjs';
import { Provider } from './provider.mjs';
import { Runtime } from './runtime.mjs';
import { createRequestHandler } from './app.mjs';
import { createRequestHandler, createSessionPublisher } from './app.mjs';
import { APP_VERSION } from './diagnostics.mjs';
const appDirectory = process.env.AGENT_WORKBENCH_APP_DIR || path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const dataDirectory = process.env.AGENT_WORKBENCH_DATA_DIR || path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), '.local', 'share'), 'AgentWorkbench');
const requestedPort = Number(process.env.AGENT_WORKBENCH_PORT || 4318);
const store = new Store(dataDirectory);
store.claimRuntime();
const provider = new Provider();
const runtime = new Runtime(store, provider);
const token = randomBytes(32).toString('hex');
@@ -27,14 +28,7 @@ try {
if (fs.existsSync(exampleRoot)) exampleProject = store.addProject(exampleRoot);
} catch {}
function sendEvent(value) {
const payload = 'data: ' + JSON.stringify(value) + '\n\n';
for (const response of clients) {
if (response.writableLength > 2 * 1024 * 1024) { response.destroy(); clients.delete(response); }
else response.write(payload);
}
}
store.on('update', session => sendEvent({ kind: 'session', session }));
store.on('update', createSessionPublisher(clients));
const server = http.createServer();
server.on('request', createRequestHandler({ appDirectory, store, provider, runtime, token, clients, started,
@@ -45,11 +39,11 @@ async function shutdown() {
await runtime.stopAll();
for (const response of clients) response.end();
clients.clear();
server.close(() => process.exit(0));
server.close(() => { store.close(); process.exit(0); });
setTimeout(() => process.exit(0), 3000).unref();
}
process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown);
server.on('error', error => { console.error('Workbench could not start: ' + error.message); process.exitCode = 1; });
server.on('error', error => { store.close(); console.error('Workbench could not start: ' + error.message); process.exitCode = 1; });
server.listen(requestedPort, '127.0.0.1', () => {
actualPort = server.address().port;
console.log(JSON.stringify({ app: 'agent-workbench', ready: true, url: 'http://127.0.0.1:' + actualPort, version: APP_VERSION }));
+49 -16
View File
@@ -6,6 +6,7 @@ import { safePath, listFiles, readLines, searchFiles, proposeFile } from './file
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';
const labels = { inspect_session: '核对工作台运行记录', list_files: '查看项目文件', read_file: '读取文件', search_files: '搜索项目', propose_file: '准备文件改动', run_command: '运行终端命令' };
@@ -33,7 +34,8 @@ export class Runtime {
const controller = new AbortController();
const config = structuredClone(this.store.config);
if (config.mode !== 'demo') this.provider.validate(config);
const run = { controller, id: randomUUID(), project };
const record = this.store.beginRun(session, content, resume, config);
const run = { controller, id: record.id, taskId: record.taskId, project, record, config };
this.active.set(id, run);
session.status = 'running'; session.recoveryNotice = null; session.executionMode = config.mode;
session.runId = run.id;
@@ -44,7 +46,11 @@ export class Runtime {
} else this.store.message(session, 'system', '从已保存的执行记录继续,已完成的工具结果保留。');
session.failure = null;
this.store.save(session);
run.promise = this.run(session, config, run).finally(() => this.active.delete(id));
const deadline = record.budget.maxDurationMs > 0 ? setTimeout(() => controller.abort(new BudgetExceeded('time_limit', '已到达本次运行的时间预算,执行记录已保存。')), record.budget.maxDurationMs) : null;
run.promise = this.run(session, config, run).finally(() => {
clearTimeout(deadline); this.active.delete(id);
this.store.finishRun(session, record, run.stopReason ?? null);
});
return session;
}
resume(id, content) {
@@ -60,6 +66,8 @@ export class Runtime {
try {
for (let step = 0; step < config.maxSteps; step++) {
signal.throwIfAborted();
checkBudget(run.record, this.store.core.task.get(run.taskId));
const stepRecord = this.store.beginStep(session, run.record);
const projected = projectMessages(session.providerMessages, config.contextLimit);
session.estimatedTokens = projected.estimatedTokens;
if (projected.projected && !projectionLogged) {
@@ -69,11 +77,17 @@ export class Runtime {
}
const state = session.changes.map(change => ({ path: change.path, status: change.status, id: change.id }));
const context = projectContext(run.project) + '\n当前文件提案状态:' + JSON.stringify(state).slice(0, 8000)
+ '\n当前任务目标与追加要求(来自用户,完整原文保存在任务记录):\n' + JSON.stringify({
goal: this.store.core.task.get(run.taskId).goal,
instructions: this.store.core.task.get(run.taskId).instructions.slice(-8),
acceptance: this.store.core.task.get(run.taskId).acceptance,
}).slice(0, 16000)
+ '\n' + sessionContext(session, run.project, config, value => this.provider.redact(value));
if (projected.estimatedTokens > config.contextLimit * 2) throw new Error('这次任务上下文仍超出预算,请缩小读取范围或新建会话。');
let requestEvent;
const response = await this.provider.complete(config, [{ role: 'system', content: context }, ...projected.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() });
stepRecord.modelAttempts = Math.max(stepRecord.modelAttempts, progress.attempt ?? 0);
requestEvent.phase = progress.phase; requestEvent.attempts = progress.attempt;
requestEvent.label = progress.phase === 'retrying' ? '正在重试模型连接' : progress.phase === 'completed' ? '模型响应已收到' : progress.phase === 'failed' ? '模型连接失败' : '请求模型服务';
requestEvent.output = progress.phase === 'retrying' ? '第 ' + progress.attempt + ' 次请求失败,将自动重试(最多 ' + progress.maxAttempts + ' 次)。'
@@ -90,10 +104,17 @@ export class Runtime {
const previousIds = new Set(session.providerMessages.flatMap(item => (item.tool_calls ?? []).map(call => call.id)));
if (new Set(calls.map(call => call.id)).size !== calls.length || calls.some(call => previousIds.has(call.id))) throw new Error('模型返回了重复的工具调用 ID,请重新发送任务。');
session.providerMessages.push(message);
session.usage = response.usage;
this.store.recordResponse(session, run.record, response.usage, projected.estimatedTokens + Math.ceil(JSON.stringify(message).length / 4));
for (const call of calls) {
const event = { id: randomUUID(), createdAt: now(), startedAt: now(), kind: 'tool', phase: 'queued',
toolCallId: call.id, tool: call.function.name, label: labels[call.function.name] ?? call.function.name, taskId: run.taskId, runId: run.id };
session.events.push(event); this.store.registerInvocation(session, call, run.record, event);
}
if (message.content) this.store.message(session, 'assistant', message.content);
else this.store.save(session);
checkBudget(run.record, this.store.core.task.get(run.taskId));
if (!calls.length) {
this.store.finishStep(session, stepRecord);
session.status = session.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'completed';
this.store.save(session); return;
}
@@ -103,20 +124,26 @@ export class Runtime {
else result = await this.executeTool(session, call, run);
session.providerMessages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) });
this.store.save(session);
signal.throwIfAborted();
}
this.store.finishStep(session, stepRecord);
}
session.status = 'paused';
run.stopReason = 'step_limit';
this.store.message(session, 'assistant', '已到达本次工具步数上限。执行记录已保存,可以查看结果后继续。');
this.store.save(session);
} catch (error) {
const repaired = repairToolPairs(session.providerMessages);
this.store.interruptPending(session, run.record);
const repaired = repairToolPairs(session.providerMessages, callId => this.store.savedToolResult(session.id, callId));
if (repaired.changed) session.providerMessages = repaired.messages;
if (signal.aborted) {
const budget = error instanceof BudgetExceeded ? error : signal.reason instanceof BudgetExceeded ? signal.reason : null;
if (signal.aborted || budget) {
run.stopReason = budget?.reason ?? 'cancelled';
for (const event of session.events.filter(item => item.kind === 'model' && item.runId === run.id && ['running', 'retrying'].includes(item.phase))) {
event.phase = 'interrupted'; event.finishedAt = now(); event.output = '模型请求已停止,已完成的工具结果保留。';
}
session.status = 'paused';
this.store.message(session, 'system', '任务已停止,执行记录已保存。');
this.store.message(session, 'system', budget?.message ?? '任务已停止,执行记录已保存。');
} else {
session.status = 'failed';
const diagnostic = error.diagnostic ?? { source: 'workbench', stage: 'agent_loop', kind: 'runtime',
@@ -132,14 +159,17 @@ export class Runtime {
}
async executeTool(session, call, run) {
const tool = call.function.name;
const event = this.store.event(session, { kind: 'tool', toolCallId: call.id, tool, label: labels[tool] ?? tool, phase: 'running', startedAt: now() });
const event = session.events.find(value => value.toolCallId === call.id && value.runId === run.id)
?? this.store.event(session, { kind: 'tool', toolCallId: call.id, tool, label: labels[tool] ?? tool, phase: 'queued', startedAt: now() });
this.store.beginInvocation(session, call, run.record, event);
let result;
try {
const args = JSON.parse(call.function.arguments);
validateToolArgs(tool, args);
event.input = args;
this.store.save(session);
if (tool === 'inspect_session') result = sessionSnapshot(session, run.project, this.store.config, value => this.provider.redact(value));
event.input = JSON.parse(this.provider.redact(JSON.stringify(args)));
if (tool !== 'run_command') 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 === '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);
@@ -157,17 +187,19 @@ export class Runtime {
}
event.finishedAt = now();
event.durationMs = Date.parse(event.finishedAt) - Date.parse(event.startedAt);
this.store.save(session);
result = JSON.parse(this.provider.redact(JSON.stringify(result)));
this.store.finishInvocation(session, event, result);
return result;
}
async requestCommand(session, event, args, run) {
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, command,
const approval = { id: randomUUID(), toolCallId: event.toolCallId, eventId: event.id, invocationId: event.invocationId,
taskId: run.taskId, runId: run.id, projectId: session.projectId, command,
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';
this.store.save(session);
this.store.invocationState(session, event, 'waiting');
const decision = await new Promise(resolve => {
const signal = run.controller.signal;
const settle = value => { this.waiters.delete(approval.id); signal.removeEventListener('abort', cancel); resolve(value); };
@@ -176,11 +208,11 @@ export class Runtime {
signal.addEventListener('abort', cancel, { once: true });
if (signal.aborted) cancel();
});
if (!decision) return { status: run.controller.signal.aborted ? 'interrupted' : 'denied', message: '命令未执行。' };
if (!decision) { if (!run.controller.signal.aborted) session.status = 'running'; return { status: run.controller.signal.aborted ? 'interrupted' : 'denied', message: '命令未执行。', requires_verification: false }; }
run.controller.signal.throwIfAborted();
const cwd = safePath(run.project);
event.phase = 'running'; event.executedAt = now(); session.status = 'running';
this.store.save(session);
this.store.invocationState(session, event, 'running');
return this.executeCommand(command, cwd, run.controller.signal, output => {
event.output = output; this.store.save(session);
});
@@ -189,7 +221,8 @@ export class Runtime {
const session = this.store.session(sessionId);
const approval = session.approvals.find(item => item.id === approvalId);
const waiter = this.waiters.get(approvalId);
if (!approval || approval.status !== 'pending' || !waiter || waiter.sessionId !== sessionId || waiter.runId !== session.runId) throw Object.assign(new Error('确认请求已过期;需要重新请求命令。'), { status: 409 });
if (!approval || approval.status !== 'pending' || !waiter || waiter.sessionId !== sessionId || waiter.runId !== session.runId
|| approval.runId !== session.runId || approval.taskId !== session.taskId || approval.projectId !== session.projectId) throw Object.assign(new Error('确认请求已过期;需要重新请求命令。'), { status: 409 });
approval.status = allow ? 'approved' : 'denied'; approval.decidedAt = now();
this.store.save(session); waiter.settle(!!allow);
}
+338 -31
View File
@@ -1,8 +1,12 @@
import fs from 'node:fs';
import path from 'node:path';
import { EventEmitter } from 'node:events';
import { randomUUID } from 'node:crypto';
import { randomUUID, createHash } from 'node:crypto';
import { lastSessionFailure } from './diagnostics.mjs';
import { StateDatabase, runtimeOwnerAlive } from '../core/storage/state-database.mjs';
import { ArtifactStore } from '../core/storage/artifact-store.mjs';
import { collectLegacy, cleanConfig } from '../core/storage/legacy-import.mjs';
import { ACTIVE_STATES, CORE_KINDS, createTask, createRun, ownedRecord, emptyUsage, accumulateUsage, accountedTokens, isId } from '../shared/contracts/execution.mjs';
export const now = () => new Date().toISOString();
@@ -29,9 +33,9 @@ export function readJson(file, fallback) {
return structuredClone(fallback);
}
export function repairToolPairs(messages) {
export function repairToolPairs(messages, lookupResult = null) {
const result = [];
let interrupted = 0, dropped = 0;
let interrupted = 0, dropped = 0, recovered = 0;
for (let index = 0; index < messages.length; index++) {
const message = messages[index];
if (message.role === 'tool') { dropped++; continue; }
@@ -44,8 +48,21 @@ export function repairToolPairs(messages) {
else dropped++;
}
for (const call of message.tool_calls) {
if (received.has(call.id)) result.push(received.get(call.id));
if (received.has(call.id)) {
const existing = received.get(call.id);
let placeholder = false;
try { const value = JSON.parse(existing.content); placeholder = value.status === 'interrupted' && value.requires_verification === true; } catch {}
const saved = placeholder ? lookupResult?.(call.id) : null;
if (saved && !['unknown', 'interrupted'].includes(saved.status)) { recovered++; result.push({ ...existing, content: JSON.stringify(saved) }); }
else result.push(existing);
}
else {
const saved = lookupResult?.(call.id);
if (saved) {
recovered++;
result.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(saved) });
continue;
}
interrupted++;
result.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify({
status: 'interrupted', requires_verification: true,
@@ -55,33 +72,118 @@ export function repairToolPairs(messages) {
}
dropped += [...received.keys()].filter(id => !message.tool_calls.some(call => call.id === id)).length;
}
return { messages: result, interrupted, changed: interrupted > 0 || dropped > 0 };
return { messages: result, interrupted, recovered, changed: interrupted > 0 || dropped > 0 || recovered > 0 };
}
export class Store extends EventEmitter {
constructor(directory) {
constructor(directory, options = {}) {
super();
this.directory = path.resolve(directory);
this.sessions = new Map();
fs.mkdirSync(path.join(this.directory, 'sessions'), { recursive: true });
this.config = readJson(path.join(this.directory, 'config.json'), {
mode: 'demo', baseUrl: 'http://127.0.0.1:11434/v1', model: '',
contextLimit: 16000, maxSteps: 12, maxOutputTokens: 2048,
});
delete this.config.apiKey;
this.projects = readJson(path.join(this.directory, 'projects.json'), []);
for (const file of fs.readdirSync(path.join(this.directory, 'sessions'))) {
if (!/^[a-f0-9-]+\.json$/.test(file)) continue;
const session = readJson(path.join(this.directory, 'sessions', file), null);
if (!session?.id || !session.projectId) continue;
fs.mkdirSync(this.directory, { recursive: true });
try { this.ensureStorage(options); this.recover(); }
catch (error) { this.storage?.close(); throw error; }
}
ensureStorage(options = {}) {
if (this.storage?.database) return;
this.core = Object.fromEntries(CORE_KINDS.map(kind => [kind, new Map()]));
this.persistedSessions = new Map();
this.artifactStore = new ArtifactStore(this.directory);
this.storage = new StateDatabase(this.directory, options);
if (runtimeOwnerAlive(this.storage.meta('runtimeOwner'))) throw new Error('另一个执行宿主正在使用这个数据目录。请使用独立数据目录。');
if (!this.storage.initialized) {
const existing = this.config && this.projects && this.sessions?.size ? { config: this.config, projects: this.projects, sessions: this.sessions } : null;
const imported = collectLegacy(this.directory, existing);
this.config = imported.config; this.projects = imported.projects;
this.sessions = new Map(imported.sessions.map(session => [session.id, session]));
for (const session of imported.sessions) this.importExecution(session);
const records = imported.sessions.flatMap(session => this.recordsFor(session));
this.storage.initialize({ ...imported, records });
}
const loaded = this.storage.load();
this.config = cleanConfig(loaded.config); this.projects = loaded.projects;
this.sessions ??= new Map();
for (const session of loaded.sessions) {
const current = this.sessions.get(session.id);
if (current) { for (const key of Object.keys(current)) delete current[key]; Object.assign(current, session); }
else this.sessions.set(session.id, session);
}
for (const record of loaded.records) this.core[record.kind]?.set(record.value.id, record.value);
for (const session of this.sessions.values()) this.persistedSessions.set(session.id, structuredClone(session));
}
importExecution(session) {
if (!session.messages.length && !session.providerMessages.length && !session.changes.length) return;
const goal = session.messages.find(message => message.role === 'user')?.content ?? session.title;
const task = createTask(session, goal, this.config); task.origin = 'legacy-import'; task.status = session.status;
task.createdAt = session.createdAt; task.updatedAt = session.updatedAt;
this.core.task.set(task.id, task); session.taskId = task.id;
const run = createRun(session, task, this.config);
if (isId(session.runId)) run.id = session.runId;
run.origin = 'legacy-import'; run.status = session.status;
run.model = { mode: session.executionMode ?? null, model: null, baseUrl: null, configRevision: null, verified: false };
run.startedAt = session.createdAt; run.finishedAt = ACTIVE_STATES.has(session.status) ? null : session.updatedAt;
this.core.run.set(run.id, run); session.runId = run.id; task.activeRunId = run.id;
const results = new Map(session.providerMessages.filter(message => message.role === 'tool').map(message => [message.tool_call_id, message.content]));
for (const message of session.providerMessages) for (const call of message.tool_calls ?? []) {
let result; try { if (results.has(call.id)) result = JSON.parse(results.get(call.id)); } catch {}
const event = session.events.find(item => item.toolCallId === call.id);
const invocation = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: task.id, runId: run.id,
callId: call.id, tool: call.function?.name ?? 'unknown', eventId: event?.id ?? null, stepId: null,
state: result ? ['error', 'failed'].includes(result.status) ? 'failed' : ['denied', 'interrupted', 'unknown'].includes(result.status) ? result.status : 'completed'
: event?.executedAt ? 'running' : 'queued', startedAt: event?.startedAt ?? session.createdAt, finishedAt: event?.finishedAt ?? null,
result: result ?? null, resultArtifactId: null, requiresVerification: !result && !!event?.executedAt, origin: 'legacy-import' };
this.core.tool.set(invocation.id, invocation);
}
}
recordsFor(session) {
return CORE_KINDS.flatMap(kind => [...this.core[kind].values()].filter(value => value.sessionId === session.id).map(value => {
const record = ownedRecord(kind, value, session);
if (record.taskId && kind !== 'task') {
const task = this.core.task.get(record.taskId);
if (!task || task.sessionId !== session.id || task.projectId !== session.projectId) throw new Error('记录不属于这个任务。');
}
if (record.runId && kind !== 'run') {
const run = this.core.run.get(record.runId);
if (!run || run.taskId !== record.taskId || run.sessionId !== session.id) throw new Error('记录不属于这个运行。');
}
return record;
}));
}
recover() {
for (const session of this.sessions.values()) {
let changed = false;
if (['running', 'awaiting_approval', 'stopping'].includes(session.status)) {
if (ACTIVE_STATES.has(session.status)) {
session.status = 'paused';
session.recoveryNotice = '上次任务被中断。继续前请核对执行记录与文件状态。';
changed = true;
}
for (const invocation of this.core.tool.values()) {
if (invocation.sessionId !== session.id || !['queued', 'validating', 'waiting', 'running'].includes(invocation.state)) continue;
invocation.requiresVerification = invocation.state === 'running';
invocation.state = invocation.requiresVerification ? 'unknown' : 'interrupted'; invocation.finishedAt = now();
invocation.result = { status: invocation.state, invocation_id: invocation.id, requires_verification: invocation.requiresVerification,
message: invocation.requiresVerification ? '执行阶段没有持久完成结果,实际副作用需要核对;不会自动重放。' : '调用未完成,恢复不会自动执行。' };
changed = true;
}
for (const run of this.core.run.values()) if (run.sessionId === session.id && ACTIVE_STATES.has(run.status)) {
run.status = 'paused'; run.finishedAt = now(); run.stopReason = 'restart'; changed = true;
}
for (const step of this.core.step.values()) if (step.sessionId === session.id && ['running', 'waiting'].includes(step.status)) {
step.status = 'interrupted'; step.finishedAt = now(); changed = true;
}
for (const job of this.core.check_job.values()) if (job.sessionId === session.id && ['running', 'waiting'].includes(job.status)) {
job.status = 'unknown'; job.finishedAt = now(); job.requiresVerification = true; changed = true;
}
for (const event of session.events ?? []) {
if (['running', 'waiting', 'retrying'].includes(event.phase)) {
const invocation = [...this.core.tool.values()].find(item => item.sessionId === session.id && item.callId === event.toolCallId);
if (invocation && ['completed', 'denied', 'failed'].includes(invocation.state)) {
event.phase = invocation.state; event.finishedAt = invocation.finishedAt; changed = true; continue;
}
event.phase = 'interrupted';
event.finishedAt = now();
event.output = event.kind === 'model' ? '上次模型请求已中断,可以从已保存记录继续。' : event.executedAt
@@ -96,31 +198,64 @@ export class Store extends EventEmitter {
changed = true;
}
}
const repaired = repairToolPairs(session.providerMessages ?? []);
const repaired = repairToolPairs(session.providerMessages ?? [], callId => this.savedToolResult(session.id, callId));
if (repaired.changed) {
session.providerMessages = repaired.messages;
session.recoveryNotice = repaired.interrupted
? '已为 ' + repaired.interrupted + ' 个未完成工具调用记录中断结果。不会自动重放命令。'
: '已整理会话中的工具结果配对。请核对执行记录后继续。';
: repaired.recovered ? '已从持久工具账本恢复 ' + repaired.recovered + ' 项实际结果。不会重新执行已完成工具。' : '已整理会话中的工具结果配对。请核对执行记录后继续。';
changed = true;
}
this.sessions.set(session.id, session);
if (changed) this.save(session);
for (const run of this.core.run.values()) if (run.sessionId === session.id) {
const tools = [...this.core.tool.values()].filter(value => value.runId === run.id);
const checkpoint = { ...run.checkpoint,
pendingToolIds: tools.filter(value => ['queued', 'validating', 'waiting', 'running'].includes(value.state)).map(value => value.id),
completedToolIds: tools.filter(value => !value.requiresVerification && ['completed', 'failed', 'denied', 'interrupted'].includes(value.state)).map(value => value.id),
requiresVerification: tools.filter(value => value.requiresVerification).map(value => value.id) };
if (JSON.stringify(checkpoint) !== JSON.stringify(run.checkpoint)) { run.checkpoint = checkpoint; changed = true; }
}
if (changed) this.save(session, { type: 'session.recovered' });
}
}
save(session) {
save(session, { type = 'session.updated', detail = {} } = {}) {
if (!this.storage?.database) throw new Error('任务存储已关闭。');
const before = this.persistedSessions.get(session.id);
session.revision = (session.revision ?? 0) + 1;
session.updatedAt = now();
atomicJson(path.join(this.directory, 'sessions', session.id + '.json'), session);
this.emit('update', this.publicSession(session));
const task = this.core.task.get(session.taskId), run = this.core.run.get(session.runId);
if (task) { task.status = session.status; task.updatedAt = session.updatedAt; }
if (run && !run.finishedAt) run.status = session.status;
let committed;
try { committed = this.storage.save(session, this.recordsFor(session), type, { status: session.status, ...detail }); }
catch (error) {
if (before) { for (const key of Object.keys(session)) delete session[key]; Object.assign(session, structuredClone(before)); }
else this.sessions.delete(session.id);
const loaded = this.storage.load();
for (const kind of CORE_KINDS) {
const persisted = loaded.records.filter(record => record.kind === kind);
const ids = new Set(persisted.map(record => record.value.id));
for (const id of this.core[kind].keys()) if (!ids.has(id)) this.core[kind].delete(id);
for (const { value } of persisted) {
const current = this.core[kind].get(value.id);
if (current) { for (const key of Object.keys(current)) delete current[key]; Object.assign(current, value); }
else this.core[kind].set(value.id, value);
}
}
throw error;
}
this.persistedSessions.set(session.id, structuredClone(session));
try { if (this.listenerCount('update')) { this.emit('update', this.publicSession(session)); this.storage.published(committed.seq); } }
catch { this.publicationNotice = '状态已保存,通知尚未发布;客户端可按事件游标补发。'; }
return session;
}
saveConfig(config) {
this.config = { ...this.config, ...config };
delete this.config.apiKey;
atomicJson(path.join(this.directory, 'config.json'), this.config);
const next = cleanConfig({ ...this.config, ...config, configRevision: (this.config.configRevision ?? 0) + 1 });
this.storage.transaction(() => this.storage.setMeta('config', next));
this.config = next;
// A compatibility export for old launchers; SQLite is authoritative after migration.
try { atomicJson(path.join(this.directory, 'config.json'), this.config); } catch { this.publicationNotice = '配置已保存,兼容JSON导出未更新。'; }
return this.config;
}
@@ -131,8 +266,9 @@ export class Store extends EventEmitter {
const existing = this.projects.find(project => (process.platform === 'win32' ? project.root.toLowerCase() : project.root) === key);
if (existing) return existing;
const project = { id: randomUUID(), root: resolved, name: path.basename(resolved), addedAt: now() };
this.projects.unshift(project);
atomicJson(path.join(this.directory, 'projects.json'), this.projects);
const next = [project, ...this.projects];
this.storage.transaction(() => this.storage.writeProjects(next));
this.projects = next;
return project;
}
@@ -161,8 +297,179 @@ export class Store extends EventEmitter {
return session;
}
beginRun(session, content, resume, config) {
let task = this.core.task.get(session.taskId);
const previousRunId = session.runId ?? null;
if (!task || !resume && ['completed', 'idle'].includes(task.status)) {
const previous = task?.id ?? null;
const goal = content || session.messages.find(message => message.role === 'user')?.content || session.title;
task = createTask(session, goal, config, previous); this.core.task.set(task.id, task);
} else if (!resume && content) task.instructions.push({ id: randomUUID(), content, createdAt: now() });
const run = createRun(session, task, config, resume ? previousRunId : null);
this.core.run.set(run.id, run); task.activeRunId = run.id;
session.taskId = task.id; session.runId = run.id; session.status = 'running';
this.save(session, { type: 'run.started', detail: { resumeOf: run.resumeOf } });
return run;
}
beginStep(session, run) {
const step = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id,
index: run.steps + 1, status: 'running', startedAt: now(), finishedAt: null, modelAttempts: 0 };
run.steps++; run.checkpoint.stepId = step.id; this.core.step.set(step.id, step);
this.save(session, { type: 'step.started', detail: { stepId: step.id, index: step.index } });
return step;
}
finishStep(session, step, status = 'completed') {
step.status = status; step.finishedAt = now();
this.save(session, { type: 'step.finished', detail: { stepId: step.id, status } });
}
recordResponse(session, run, usage, estimate) {
run.usage = accumulateUsage(run.usage, usage, estimate);
const task = this.core.task.get(run.taskId);
task.usage = accumulateUsage(task.usage, usage, estimate);
session.usage = usage;
}
finishRun(session, run, stopReason = null) {
run.status = session.status; run.finishedAt = now(); run.stopReason = stopReason;
const step = this.core.step.get(run.checkpoint.stepId);
if (step && ['running', 'waiting'].includes(step.status)) { step.status = session.status === 'completed' || session.status === 'needs_review' ? 'completed' : 'interrupted'; step.finishedAt = now(); }
this.save(session, { type: 'run.finished', detail: { stopReason } });
}
registerInvocation(session, call, run, event) {
const record = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id,
stepId: run.checkpoint.stepId, callId: call.id, eventId: event.id, tool: call.function.name,
argsSha256: createHash('sha256').update(String(call.function.arguments)).digest('hex'),
state: 'queued', startedAt: now(), finishedAt: null, result: null, resultArtifactId: null, requiresVerification: false, origin: 'workbench' };
event.invocationId = record.id; event.taskId = run.taskId; event.runId = run.id;
this.core.tool.set(record.id, record); run.toolCalls++; run.checkpoint.pendingToolIds.push(record.id);
return record;
}
beginInvocation(session, call, run, event) {
const record = [...this.core.tool.values()].find(value => value.runId === run.id && value.callId === call.id) ?? this.registerInvocation(session, call, run, event);
const actualHash = createHash('sha256').update(String(call.function.arguments)).digest('hex');
if (actualHash !== record.argsSha256 || record.state !== 'queued') throw new Error('工具输入已改变或这个调用已进入执行,不能重复执行。');
event.invocationId = record.id; event.phase = 'running'; record.state = 'validating';
this.save(session, { type: 'tool.validating', detail: { invocationId: record.id } });
return record;
}
interruptPending(session, run) {
for (const record of this.core.tool.values()) {
if (record.runId !== run.id || !['queued', 'validating', 'waiting', 'running'].includes(record.state)) continue;
record.requiresVerification = record.state === 'running';
record.state = record.requiresVerification ? 'unknown' : 'interrupted'; record.finishedAt = now();
record.result = { status: record.state, invocation_id: record.id, requires_verification: record.requiresVerification,
message: record.requiresVerification ? '执行进入后没有记录完成结果,需要核对实际状态。' : '任务中断,这次调用未执行。' };
const event = session.events.find(value => value.id === record.eventId);
if (event) { event.phase = 'interrupted'; event.finishedAt = record.finishedAt; event.output = record.result.message; }
}
run.checkpoint.pendingToolIds = [];
run.checkpoint.requiresVerification = [...this.core.tool.values()].filter(value => value.runId === run.id && value.requiresVerification).map(value => value.id);
}
invocationState(session, event, state) {
const record = this.core.tool.get(event.invocationId);
if (!record) throw new Error('工具执行账本不存在。');
record.state = state;
if (state === 'running') record.enteredExecutionAt = now();
this.save(session, { type: 'tool.' + state, detail: { invocationId: record.id } });
}
finishInvocation(session, event, result) {
const record = this.core.tool.get(event.invocationId);
if (!record) throw new Error('工具执行账本不存在。');
const run = this.core.run.get(record.runId);
const artifact = this.artifactStore.write(session, run, result);
this.core.artifact.set(artifact.id, artifact);
record.state = ['error', 'failed'].includes(result.status) ? 'failed' : ['denied', 'interrupted', 'unknown'].includes(result.status) ? result.status : 'completed';
record.finishedAt = now(); record.requiresVerification = result.requires_verification === true;
record.resultArtifactId = artifact.id;
record.result = Buffer.byteLength(JSON.stringify(result), 'utf8') <= 8000 ? structuredClone(result) : null;
event.artifactId = artifact.id;
run.checkpoint.pendingToolIds = run.checkpoint.pendingToolIds.filter(id => id !== record.id);
if (record.requiresVerification) run.checkpoint.requiresVerification.push(record.id);
else run.checkpoint.completedToolIds.push(record.id);
this.save(session, { type: 'tool.result', detail: { invocationId: record.id, state: record.state, artifactId: artifact.id } });
return artifact;
}
savedToolResult(sessionId, callId) {
const record = [...this.core.tool.values()].find(value => value.sessionId === sessionId && value.callId === callId);
if (!record || !['completed', 'failed', 'denied', 'interrupted', 'unknown'].includes(record.state)) return null;
if (record.resultArtifactId) {
try { return JSON.parse(this.artifactStore.read(this.core.artifact.get(record.resultArtifactId))); }
catch {
record.state = 'unknown'; record.requiresVerification = true;
record.result = { status: 'unknown', requires_verification: true, invocation_id: record.id, message: '工具结果工件缺失或校验失败,实际结果需要核对。' };
}
}
return record.result;
}
putRecord(kind, session, value, type) {
ownedRecord(kind, value, session); this.core[kind].set(value.id, value);
this.save(session, { type, detail: { recordId: value.id } }); return value;
}
owned(kind, id, sessionId) {
const record = this.core[kind]?.get(id);
if (!record || record.sessionId !== sessionId || record.projectId !== this.session(sessionId).projectId) throw Object.assign(new Error('执行记录不属于当前会话。'), { status: 404 });
return record;
}
executionSnapshot(session) {
const task = this.core.task.get(session.taskId), run = this.core.run.get(session.runId);
const tools = [...this.core.tool.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id);
const artifacts = [...this.core.artifact.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id).slice(-30);
const jobs = [...this.core.check_job.values()].filter(value => value.sessionId === session.id && value.taskId === task?.id).slice(-10);
return {
schemaVersion: 1, task: task ?? null, run: run ?? null,
runs: [...this.core.run.values()].filter(value => value.sessionId === session.id).slice(-20).map(value => ({ id: value.id, taskId: value.taskId,
status: value.status, startedAt: value.startedAt, finishedAt: value.finishedAt, steps: value.steps, toolCalls: value.toolCalls, usage: value.usage, stopReason: value.stopReason })),
progress: { totalTools: tools.length, completedTools: tools.filter(value => value.state === 'completed').length,
requiresVerification: tools.filter(value => value.requiresVerification).map(value => ({ id: value.id, tool: value.tool, callId: value.callId, state: value.state })) },
artifacts, checks: jobs, accountedTaskTokens: accountedTokens(task?.usage),
};
}
get eventCursor() { return this.storage.cursor; }
eventsAfter(cursor, options) { return this.storage.eventsAfter(cursor, options); }
storageInfo() {
return { engine: 'sqlite', schemaVersion: 1, eventCursor: this.eventCursor, pendingPublications: this.storage.pendingPublications(),
migratedFromJson: !!this.storage.meta('migration'), recovery: 'verify-unknown-results', compatibilityExport: 'explicit-json' };
}
claimRuntime() {
if (runtimeOwnerAlive(this.storage.meta('runtimeOwner'))) throw new Error('这个数据目录已被另一个执行宿主使用。');
this.ownerId ??= randomUUID();
this.storage.transaction(() => this.storage.setMeta('runtimeOwner', { pid: process.pid, id: this.ownerId, startedAt: now() }));
}
exportLegacyData(directory) {
const target = path.resolve(directory);
if (fs.existsSync(target) && fs.readdirSync(target).length) throw new Error('回退导出需要一个新的空目录。');
fs.mkdirSync(path.join(target, 'sessions'), { recursive: true });
atomicJson(path.join(target, 'config.json'), cleanConfig(this.config));
atomicJson(path.join(target, 'projects.json'), this.projects);
for (const session of this.sessions.values()) atomicJson(path.join(target, 'sessions', session.id + '.json'), session);
return { directory: target, sessions: this.sessions.size, projects: this.projects.length, source: 'sqlite-export' };
}
close() {
if (!this.storage?.database) return;
const owner = this.storage.meta('runtimeOwner');
if (this.ownerId && owner?.id === this.ownerId) this.storage.transaction(() => this.storage.setMeta('runtimeOwner', null));
this.storage.close();
}
publicSession(session) {
const { providerMessages, ...visible } = session;
visible.execution = this.executionSnapshot(session);
visible.lastFailure = lastSessionFailure(session);
if (session.status === 'failed' && !visible.failure && visible.lastFailure) {
visible.failure = { ...visible.lastFailure, messageId: session.messages.findLast(message => message.role === 'system')?.id };
@@ -178,7 +485,7 @@ export class Store extends EventEmitter {
}
event(session, event) {
const entry = { id: randomUUID(), createdAt: now(), ...event };
const entry = { id: randomUUID(), createdAt: now(), taskId: session.taskId ?? null, runId: session.runId ?? null, ...event };
session.events.push(entry);
this.save(session);
return entry;
+91
View File
@@ -0,0 +1,91 @@
import { randomUUID } from 'node:crypto';
export const EXECUTION_SCHEMA = 1;
export const ACTIVE_STATES = new Set(['running', 'awaiting_approval', 'stopping']);
export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job'];
export const timestamp = () => new Date().toISOString();
export const isId = value => typeof value === 'string' && /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(value);
export function modelSnapshot(config) {
return {
mode: config.mode, baseUrl: config.baseUrl, model: config.model,
contextLimit: config.contextLimit, maxOutputTokens: config.maxOutputTokens,
configRevision: config.configRevision ?? 1,
};
}
export function executionBudget(config) {
return {
maxSteps: config.maxSteps ?? 12,
maxDurationMs: config.maxRunDurationMs ?? 600000,
maxTokens: config.maxRunTokens ?? 200000,
maxTaskTokens: config.maxTaskTokens ?? 1000000,
};
}
export function emptyUsage() {
return { requests: 0, inputTokens: 0, outputTokens: 0, totalTokens: 0, unknownRequests: 0, estimatedUnknownTokens: 0 };
}
export function accumulateUsage(previous, received, estimate = 0) {
const usage = { ...emptyUsage(), ...previous, requests: (previous?.requests ?? 0) + 1 };
const valid = value => Number.isSafeInteger(value) && value >= 0;
if (received && (valid(received.total_tokens) || valid(received.prompt_tokens) && valid(received.completion_tokens))) {
usage.inputTokens += valid(received.prompt_tokens) ? received.prompt_tokens : 0;
usage.outputTokens += valid(received.completion_tokens) ? received.completion_tokens : 0;
usage.totalTokens += valid(received.total_tokens) ? received.total_tokens : received.prompt_tokens + received.completion_tokens;
} else {
usage.unknownRequests++;
usage.estimatedUnknownTokens += Math.max(0, Math.ceil(Number(estimate) || 0));
}
return usage;
}
export const accountedTokens = usage => (usage?.totalTokens ?? 0) + (usage?.estimatedUnknownTokens ?? 0);
export function createTask(session, goal, config, previousTaskId = null) {
const at = timestamp();
return {
schemaVersion: EXECUTION_SCHEMA, id: randomUUID(), sessionId: session.id, projectId: session.projectId,
goal: String(goal), instructions: [], constraints: [], acceptance: [], status: 'idle',
previousTaskId, createdAt: at, updatedAt: at, activeRunId: null,
usage: emptyUsage(), budget: { maxTokens: executionBudget(config).maxTaskTokens },
verification: { status: 'not_configured', checkJobId: null }, origin: 'workbench',
};
}
export function createRun(session, task, config, resumeOf = null) {
return {
schemaVersion: EXECUTION_SCHEMA, id: randomUUID(), taskId: task.id, sessionId: session.id, projectId: session.projectId,
engine: 'workbench', status: 'running', startedAt: timestamp(), finishedAt: null,
resumeOf, model: modelSnapshot(config), budget: executionBudget(config), usage: emptyUsage(),
steps: 0, toolCalls: 0, stopReason: null,
checkpoint: { stepId: null, completedToolIds: [], pendingToolIds: [], requiresVerification: [] }, origin: 'workbench',
};
}
export function ownedRecord(kind, value, session) {
if (!CORE_KINDS.includes(kind) || !isId(value.id) || value.sessionId !== session.id || value.projectId !== session.projectId) {
throw new Error('执行记录的身份或项目归属无效。');
}
if (value.taskId && !isId(value.taskId) || value.runId && !isId(value.runId)) throw new Error('任务或运行身份无效。');
return { kind, id: value.id, sessionId: value.sessionId, projectId: value.projectId,
taskId: value.taskId ?? (kind === 'task' ? value.id : null), runId: value.runId ?? (kind === 'run' ? value.id : null),
status: value.status ?? value.state ?? null, value };
}
export class BudgetExceeded extends Error {
constructor(reason, message) { super(message); this.name = 'BudgetExceeded'; this.reason = reason; }
}
export function checkBudget(run, task) {
if (run.budget.maxDurationMs > 0 && Date.now() - Date.parse(run.startedAt) >= run.budget.maxDurationMs) {
throw new BudgetExceeded('time_limit', '已到达本次运行的时间预算。执行记录已保存,可以核对后继续。');
}
if (run.budget.maxTokens > 0 && accountedTokens(run.usage) >= run.budget.maxTokens) {
throw new BudgetExceeded('token_limit', '已到达本次运行的 token 预算。已完成工具结果保留,可以核对后继续。');
}
if (task.budget.maxTokens > 0 && accountedTokens(task.usage) >= task.budget.maxTokens) {
throw new BudgetExceeded('task_token_limit', '这个任务的累计 token 预算已用完,请调整任务范围或新建任务。');
}
}
+25
View File
@@ -108,3 +108,28 @@
- docs/PRODUCT-PLAN.md、CAPABILITIES.md、RESEARCH-ROADMAP.md、GIT-WORKFLOW.md、research/targets.json及AGENTS.md已写入;M1–M6属于后续实现,没有在本轮规划中改运行源码。
- 首轮文件清单查询research目录时该目录尚不存在,rg返回1;随后随来源登记创建目录。批量输出曾被工具截断,改为按文件/关键段读取,未将截断结果当完整证据。
- 规划校验通过:9份Markdown、44个本地链接、8个来源目标、24项能力、22个来源/证据路径和8个元数据散列;git diff --check通过。4318仍由原PID23080监听。
## M1 可靠内核与首个工程交付切片(2026-10-29,当前任务)
用户已批准继续下一步开发。先实现自有Task/Run/Step/Tool/Event/Artifact与可靠保存恢复,在兼容现有界面的基础上提供实际工程检查与交付样本,按小步测试并本地提交。
1. [complete] 核对当前执行/保存/API/UI契约、升级机制和测试,确定兼容迁移与验收。
2. [complete] 实现独立任务/运行契约、SQLite状态与追加事件、JSON迁移/备份、恢复和工具结果核对;10个实际退出切点通过。
3. [in_progress] Runtime、模型配置/预算、审批/工具身份、事件游标和工件已接入;任务记录界面与前端补发处理尚待实现。
4. [pending] 实现有界工程识别、检查计划/真实job结果与Git基线,建立可复现的跨文件工程交付样本。
5. [in_progress] 构建和47项完整回归通过,追加时间预算/工件损坏/事件补发后的20项专项通过;UI与工程样本待实现,先保存内核提交。
6. [pending] 验证现有连接保留的升级路径,交付可运行版本和实际验收记录。
### 本轮执行边界与发现
- 4318原服务和仅内存的真实Key保留。开发/验证使用独立端口与数据;在保留连接的升级路径通过验证后再更新原进程,禁止提取/打印/复制Key。
- 从main的b047665开始,当前工作区干净;本轮不推送远端,不修改其他项目。
- 完成工具必须保留实际结果;执行结果未知的命令须核对,不能自动重放。JSON迁移失败不毁坏旧数据,SQLite状态与事件事务一致。
- 不将本轮样本通过宣称为M2三类工程或M6专业验收完成。
- 首次跟踪文件补丁错误地使用findings.md的不存在标题,整个补丁未应用;已按实际标题拆分修正。批量源代码输出按预算截断时只使用已收到部分,后续按相关文件补读。
- Node24.22.1已安装;当前Store为同步JSON替换,所有Runtime调用直接修改session再save,升级通过替换原对象原型保留连接。新存储须提供兼容save并在升级前初始化/备份。
- 首轮Store/Runtime12项检查11通过;旧隐私检查仍扫描JSON目录,迁移后目录不存在,已改为读取真实数据库/工件进行同一隐私断言。准备实际进程退出与事务/工具结果切点验证。
- 本轮随后完成25项针对性检查、完整构建/47项回归及新增后的20项专项,均通过。47项与20项包含重复覆盖,不能相加宣称67个独立测试。
- 执行被主动中断,后台测试已完成,原4318仍为PID23080;保留内核改动并保存本地检查点,尚未升级实际1.0.0服务或生成新发行包。
- 后续首先完成任务记录/前端事件补发与预算界面,随后工程识别/TestRunner/Git基线和跨文件样本。当前Step仅代表模型轮次,尚无依赖任务图/多文件ChangeSet。
- 恢复接续时核对:大型工件损坏的账本会标unknown,但旧工具事件的completed展示仍需一致性修正;串行热升级的handlerRoot路径和原进程停止时SQLite资源/owner释放仍需补充验证。真实内存Key保持,最终升级使用独立不可变发行目录避免模块缓存复用。
+30
View File
@@ -99,3 +99,33 @@ test('runtime diagnostics and model-response validation use actual records and p
assert.ok(!JSON.stringify(diagnostic).includes('fixture-saved-connection')); assert.ok(!JSON.stringify(diagnostic).includes('fixture-tentative-connection'));
await server.api('/config/models', {}); assert.equal(model.requests[1].headers.authorization, 'Bearer fixture-saved-connection');
});
test('persistent event replay is read-only and keeps run/artifact identity across reconnects', async t => {
const f = fixture(t), server = await launchWorkbench(t, f.data); f.onCleanup(server.close);
const cursor = server.bootstrap.eventCursor;
await server.api('/sessions/' + f.session.id + '/messages', { content: 'Use the controlled offline fixture' });
let waiting;
await until(async () => { waiting = await server.api('/sessions/' + f.session.id); return waiting.status === 'awaiting_approval'; }, 12000);
await server.api('/sessions/' + f.session.id + '/approvals/' + waiting.approvals[0].id, { allow: false });
let ended;
await until(async () => { ended = await server.api('/sessions/' + f.session.id); return ended.status === 'needs_review'; }, 12000);
const history = await server.api('/events/history?after=' + cursor + '&session=' + f.session.id + '&limit=3');
assert.equal(history.events.length, 3); assert.equal(history.hasMore, true);
assert.ok(history.events.every(value => value.taskId === ended.execution.task.id && value.runId === ended.execution.run.id));
const artifact = ended.execution.artifacts.find(value => value.kind === 'tool-result');
const result = await server.api('/artifacts/' + artifact.id + '?session=' + f.session.id);
assert.equal(result.sha256, artifact.sha256); assert.ok(result.content);
const other = await server.api('/sessions', { projectId: f.project.id });
await assert.rejects(server.api('/artifacts/' + artifact.id + '?session=' + other.id), { status: 404 });
const stop = new AbortController();
const stream = await fetch(server.url + '/api/events?token=' + encodeURIComponent(server.bootstrap.token) + '&after=' + cursor,
{ headers: { 'Last-Event-ID': String(history.nextCursor) }, signal: stop.signal });
const reader = stream.body.getReader(); const bytes = await reader.read(); const text = Buffer.from(bytes.value).toString('utf8');
stop.abort(); await reader.cancel().catch(() => {});
assert.match(text, /session_changed/); assert.match(text, /id: \d+/);
const replayedIds = [...text.matchAll(/^id: (\d+)$/gm)].map(match => Number(match[1]));
assert.ok(replayedIds.every(value => value > history.nextCursor));
const after = await server.api('/sessions/' + f.session.id);
assert.equal(after.runId, ended.runId); assert.equal(after.approvals.length, ended.approvals.length);
assert.deepEqual(after.changes, ended.changes); assert.equal(after.execution.run.toolCalls, ended.execution.run.toolCalls);
});
+1
View File
@@ -174,6 +174,7 @@ test('old session data receives a truthful historical connection diagnosis witho
const f = fixture(t);
f.store.message(f.session, 'system', 'fetch failed'); f.store.event(f.session, { kind: 'error', phase: 'failed', output: 'fetch failed' });
const originalMessages = structuredClone(f.session.messages), reloaded = new Store(f.data);
f.onCleanup(() => reloaded.close());
const session = reloaded.session(f.session.id), visible = reloaded.publicSession(session);
assert.equal(visible.lastFailure.source, 'model_connection'); assert.equal(visible.lastFailure.historical, true);
assert.deepEqual(session.messages, originalMessages); assert.equal(Object.hasOwn(visible, 'providerMessages'), false);
+180
View File
@@ -0,0 +1,180 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { Store, repairToolPairs } from '../src/server/store.mjs';
import { StateDatabase } from '../src/core/storage/state-database.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { fixture, workspace, call, until, assertToolPairs } from './helpers.mjs';
function legacyCase(f, name) {
f.store.message(f.session, 'user', 'Original fixture requirement');
fs.writeFileSync(path.join(f.projectRoot, 'worker-effect.mjs'), "import fs from 'node:fs'; const file='executions.txt'; const count=fs.existsSync(file)?Number(fs.readFileSync(file,'utf8')):0; fs.writeFileSync(file,String(count+1)); console.log('fixture-result');\n");
const directory = path.join(f.root, name); f.store.exportLegacyData(directory);
return directory;
}
async function crash(t, directory, phase) {
const env = { ...process.env }; delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/core-crash-worker.mjs'), directory, phase], { env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = ''; child.stdout.on('data', bytes => output += bytes.toString('utf8')); child.stderr.on('data', bytes => output += bytes.toString('utf8'));
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null || child.signalCode !== null, 15000);
assert.equal(child.exitCode, 66, output);
}
function reopen(f, directory) { const store = new Store(directory); f.onCleanup(() => store.close()); return store; }
test('JSON migration survives process exit before commit and retains exact legacy backups', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, 'migration');
const sessionFile = path.join(directory, 'sessions', f.session.id + '.json');
const before = fs.readFileSync(sessionFile);
await crash(t, directory, 'migration');
const raw = new StateDatabase(directory);
assert.equal(raw.initialized, false); assert.equal(raw.cursor, 0); assert.equal(raw.load().sessions.length, 0); raw.close();
assert.deepEqual(fs.readFileSync(sessionFile), before);
const recovered = reopen(f, directory), session = recovered.session(f.session.id);
assert.equal(session.messages[0].content, 'Original fixture requirement');
assert.equal(recovered.core.task.size, 1);
const migration = recovered.storage.meta('migration');
assert.deepEqual(fs.readFileSync(path.join(directory, migration.backupRelative, 'sessions', f.session.id + '.json')), before);
const expected = structuredClone(session); recovered.close();
fs.writeFileSync(sessionFile, '{}');
const again = reopen(f, directory);
assert.deepEqual(again.session(f.session.id), expected, 'A stale JSON mirror cannot overwrite migrated SQLite state');
const exported = path.join(f.root, 'rollback-export'); again.exportLegacyData(exported);
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(exported, 'sessions', session.id + '.json'), 'utf8')), expected);
});
for (const point of ['after_state', 'after_event']) test('process exit ' + point + ' rolls back state, event and outbox together', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, point), initial = reopen(f, directory);
const before = structuredClone(initial.session(f.session.id)), cursor = initial.eventCursor;
initial.close(); await crash(t, directory, point);
const recovered = reopen(f, directory);
assert.deepEqual(recovered.session(f.session.id), before); assert.equal(recovered.eventCursor, cursor);
assert.equal(recovered.eventsAfter(cursor).events.length, 0);
});
for (const phase of ['accepted', 'queued', 'approval_wait', 'approved', 'effect_unrecorded', 'result_committed', 'before_next_model']) {
test('crash recovery at ' + phase + ' retains execution identity and never replays side effects', { timeout: 30000 }, async t => {
const f = fixture(t), directory = legacyCase(f, phase);
await crash(t, directory, phase);
const recovered = reopen(f, directory), session = recovered.session(f.session.id), execution = recovered.executionSnapshot(session);
assert.equal(session.status, 'paused'); assert.equal(execution.task.goal, 'Crash fixture task');
assert.equal(execution.run.status, 'paused'); assert.equal(execution.run.stopReason, 'restart');
const marker = path.join(f.projectRoot, 'executions.txt');
const effectOccurred = ['effect_unrecorded', 'result_committed', 'before_next_model'].includes(phase);
assert.equal(fs.existsSync(marker), effectOccurred);
if (effectOccurred) assert.equal(fs.readFileSync(marker, 'utf8'), '1');
assertToolPairs(session.providerMessages);
const invocation = [...recovered.core.tool.values()].find(value => value.callId === 'crash-' + phase);
if (phase !== 'accepted') {
assert.ok(invocation); assert.equal(invocation.runId, execution.run.id); assert.equal(invocation.taskId, execution.task.id);
if (['result_committed', 'before_next_model'].includes(phase)) {
assert.equal(invocation.state, 'completed'); assert.equal(invocation.requiresVerification, false);
const result = JSON.parse(session.providerMessages.find(value => value.tool_call_id === invocation.callId).content);
assert.equal(result.status, 'completed'); assert.match(result.stdout, /fixture-result/);
} else if (['approved', 'effect_unrecorded'].includes(phase)) {
assert.equal(invocation.state, 'unknown'); assert.equal(invocation.requiresVerification, true);
assert.ok(execution.run.checkpoint.requiresVerification.includes(invocation.id));
} else { assert.equal(invocation.state, 'interrupted'); assert.equal(invocation.requiresVerification, false); }
}
const oldApproval = session.approvals[0]?.id;
const provider = new Provider(); let requests = 0;
provider.complete = async () => { requests++; return { message: { role: 'assistant', content: 'Recovered records inspected.' }, usage: null }; };
const runtime = new Runtime(recovered, provider); f.onCleanup(() => runtime.stopAll());
if (oldApproval) assert.throws(() => runtime.decide(session.id, oldApproval, true), { status: 409 });
if (phase !== 'accepted') {
const previousRunId = session.runId, taskId = session.taskId;
runtime.resume(session.id); await runtime.active.get(session.id).promise;
assert.equal(requests, 1); assert.equal(session.taskId, taskId); assert.notEqual(session.runId, previousRunId);
assert.equal(recovered.core.run.get(session.runId).resumeOf, previousRunId);
if (oldApproval) assert.throws(() => runtime.decide(session.id, oldApproval, true), { status: 409 });
assert.equal(fs.existsSync(marker), effectOccurred);
if (effectOccurred) assert.equal(fs.readFileSync(marker, 'utf8'), '1');
}
});
}
test('state and event failures publish no success and preserve the last committed memory snapshot', t => {
const f = fixture(t), before = structuredClone(f.session), cursor = f.store.eventCursor;
let notifications = 0; f.store.on('update', () => notifications++);
f.store.storage.faultInjector = point => { if (point === 'after_event') throw new Error('Controlled transaction failure'); };
assert.throws(() => f.store.message(f.session, 'user', 'must not publish'), /Controlled transaction failure/);
assert.deepEqual(f.session, before); assert.equal(f.store.eventCursor, cursor); assert.equal(notifications, 0);
f.store.storage.faultInjector = null;
f.store.message(f.session, 'user', 'committed'); assert.equal(notifications, 1);
});
test('tool result artifacts and event cursors retain project ownership across restart', async t => {
const f = fixture(t), provider = new Provider(); let round = 0;
provider.complete = async () => ({ message: round++ === 0 ? { role: 'assistant', content: '', tool_calls: [call('owned-read', 'read_file', { path: 'README.md' })] }
: { role: 'assistant', content: 'Read complete.' }, usage: { prompt_tokens: 11, completion_tokens: 3 } });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
const initial = f.store.eventCursor;
runtime.start(f.session.id, 'Read the fixture'); await runtime.active.get(f.session.id).promise;
const snapshot = f.store.executionSnapshot(f.session);
assert.equal(snapshot.run.usage.requests, 2); assert.equal(snapshot.run.usage.totalTokens, 28);
const artifact = snapshot.artifacts[0]; assert.equal(artifact.projectId, f.project.id);
assert.match(JSON.parse(f.store.artifactStore.read(artifact)).content, /Fixture project/);
const other = f.store.createSession(f.project.id);
assert.throws(() => f.store.owned('artifact', artifact.id, other.id), { status: 404 });
const page = f.store.eventsAfter(initial, { sessionId: f.session.id, limit: 3 });
assert.equal(page.events.length, 3); assert.equal(page.hasMore, true);
assert.ok(page.events.every(event => event.projectId === f.project.id && event.sessionId === f.session.id));
const next = f.store.eventsAfter(page.nextCursor, { sessionId: f.session.id });
assert.ok(next.events.every(event => event.seq > page.nextCursor));
const all = [...page.events, ...next.events]; assert.equal(new Set(all.map(event => event.seq)).size, all.length);
const expectedCursor = f.store.eventCursor;
f.store.close(); const recovered = reopen(f, f.data);
assert.equal(recovered.eventCursor, expectedCursor);
assert.deepEqual(recovered.owned('artifact', artifact.id, f.session.id), artifact);
});
test('token budgets pause before tools execute and continuation keeps original task and accumulated usage', async t => {
const f = fixture(t), provider = new Provider(); let round = 0;
f.store.saveConfig({ maxRunTokens: 25 });
provider.complete = async () => ({ message: { role: 'assistant', content: '', tool_calls: [call('budget-' + round++, 'run_command', { command: 'node --version', reason: 'must not execute over budget' })] },
usage: { prompt_tokens: 20, completion_tokens: 10 } });
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Original goal'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'paused'); assert.equal(f.session.approvals.length, 0);
const originalTaskId = f.session.taskId;
assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'token_limit');
assert.equal(f.store.core.task.get(originalTaskId).usage.totalTokens, 30); assertToolPairs(f.session.providerMessages);
f.store.saveConfig({ maxRunTokens: 1000 });
provider.complete = async () => ({ message: { role: 'assistant', content: 'Finished.' }, usage: { prompt_tokens: 5, completion_tokens: 5 } });
runtime.start(f.session.id, 'Also preserve the public API.'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.taskId, originalTaskId); assert.equal(f.store.core.task.get(originalTaskId).goal, 'Original goal');
assert.equal(f.store.core.task.get(originalTaskId).instructions[0].content, 'Also preserve the public API.');
assert.equal(f.store.core.task.get(originalTaskId).usage.totalTokens, 40);
assert.equal(f.store.core.run.get(f.session.runId).budget.maxTokens, 1000);
});
test('a time budget interrupts an unresponsive model and persists a resumable run', async t => {
const f = fixture(t), provider = new Provider(); f.store.saveConfig({ maxRunDurationMs: 80 });
provider.complete = async (_config, _messages, _project, signal) => new Promise((resolve, reject) => {
signal.addEventListener('abort', () => reject(signal.reason), { once: true });
if (signal.aborted) reject(signal.reason);
});
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
runtime.start(f.session.id, 'Keep this goal after timeout'); await runtime.active.get(f.session.id).promise;
assert.equal(f.session.status, 'paused'); assert.equal(f.store.core.run.get(f.session.runId).stopReason, 'time_limit');
assert.match(f.session.messages.at(-1).content, /时间预算/); assert.equal(runtime.isActive(f.session.id), false);
});
test('a corrupted large result artifact cannot become a recovered success', t => {
const f = fixture(t), run = f.store.beginRun(f.session, 'Read a large fixture', false, f.store.config);
const tool = call('large-result', 'read_file', { path: 'README.md' });
f.session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [tool] });
const event = { id: crypto.randomUUID(), kind: 'tool', tool: 'read_file', phase: 'queued', toolCallId: tool.id, runId: run.id, createdAt: new Date().toISOString() };
f.session.events.push(event); f.store.registerInvocation(f.session, tool, run, event);
f.store.beginInvocation(f.session, tool, run, event); f.store.invocationState(f.session, event, 'running');
event.phase = 'completed';
const artifact = f.store.finishInvocation(f.session, event, { status: 'completed', content: 'x'.repeat(20000) });
fs.writeFileSync(path.join(f.data, 'artifacts', artifact.relative), 'corrupted fixture result');
f.store.close(); const recovered = reopen(f, f.data), session = recovered.session(f.session.id);
const result = JSON.parse(session.providerMessages.find(value => value.tool_call_id === tool.id).content);
assert.equal(result.status, 'unknown'); assert.equal(result.requires_verification, true);
assert.equal(recovered.executionSnapshot(session).progress.requiresVerification.length, 1);
});
+54
View File
@@ -0,0 +1,54 @@
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { Store } from '../../src/server/store.mjs';
import { Runtime } from '../../src/server/runtime.mjs';
import { Provider } from '../../src/server/provider.mjs';
const [directory, phase] = process.argv.slice(2);
const store = new Store(directory, { faultInjector: point => { if (phase === 'migration' && point === 'migration_records') process.exit(66); } });
const session = [...store.sessions.values()][0];
const project = store.project(session.projectId);
const call = { id: 'crash-' + phase, type: 'function', function: { name: 'run_command', arguments: JSON.stringify({ command: 'node worker-effect.mjs', reason: 'Controlled crash fixture' }) } };
if (phase === 'after_state' || phase === 'after_event') {
store.storage.faultInjector = point => { if (point === phase) process.exit(66); };
store.message(session, 'user', 'This transaction must roll back.');
throw new Error('Transaction crash point was not reached.');
}
if (phase === 'accepted' || phase === 'queued') {
const run = store.beginRun(session, 'Crash fixture task', false, store.config);
if (phase === 'queued') {
session.providerMessages.push({ role: 'assistant', content: '', tool_calls: [call] });
const event = { id: randomUUID(), kind: 'tool', phase: 'queued', tool: 'run_command', toolCallId: call.id, runId: run.id, taskId: run.taskId, label: 'Controlled fixture', createdAt: new Date().toISOString() };
session.events.push(event); store.registerInvocation(session, call, run, event); store.save(session);
}
process.exit(66);
}
const provider = new Provider();
let requests = 0;
provider.complete = async () => {
requests++;
if (requests > 1 && phase === 'before_next_model') process.exit(66);
return { message: requests === 1 ? { role: 'assistant', content: '', tool_calls: [call] } : { role: 'assistant', content: 'Done.' }, usage: { prompt_tokens: 20, completion_tokens: 10 } };
};
const runtime = new Runtime(store, provider);
if (phase === 'approved') runtime.executeCommand = () => process.exit(66);
const finish = store.finishInvocation.bind(store);
store.finishInvocation = (...args) => {
if (phase === 'effect_unrecorded') process.exit(66);
const result = finish(...args);
if (phase === 'result_committed') process.exit(66);
return result;
};
runtime.start(session.id, 'Crash fixture task');
const poll = setInterval(() => {
const approval = session.approvals.find(value => value.status === 'pending');
if (!approval) return;
if (phase === 'approval_wait') process.exit(66);
runtime.decide(session.id, approval.id, true);
}, 10);
await runtime.active.get(session.id).promise;
clearInterval(poll); store.close();
throw new Error('Runtime crash point was not reached: ' + phase + ' at ' + path.basename(project.root));
+4 -2
View File
@@ -8,6 +8,7 @@ import { Store } from '../src/server/store.mjs';
export const workspace = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const fixtureRoot = path.join(workspace, '.qa');
const disposalLists = new WeakMap();
export function fixture(t) {
fs.mkdirSync(fixtureRoot, { recursive: true });
@@ -19,7 +20,8 @@ export function fixture(t) {
const store = new Store(data);
const project = store.addProject(projectRoot);
const session = store.createSession(project.id);
const disposers = [];
const disposers = [() => store.close()];
disposalLists.set(t, disposers);
t.after(async () => {
for (const dispose of disposers.reverse()) await dispose();
const resolved = path.resolve(root);
@@ -102,6 +104,6 @@ export async function launchWorkbench(t, data, extraEnv = {}) {
try { await api('/shutdown', {}); await until(() => child.exitCode !== null || child.signalCode !== null, 5000); }
finally { if (child.exitCode === null && child.signalCode === null) child.kill(); }
};
t.after(close);
if (disposalLists.has(t)) disposalLists.get(t).push(close); else t.after(close);
return { child, url, bootstrap, api, close };
}
+2 -1
View File
@@ -63,7 +63,8 @@ test('the compatible HTTP loop preserves call IDs, validates errors and waits be
assert.match(f.session.events.find(event => event.toolCallId === 'command').output, /fixture-command/);
assert.equal(runtime.isActive(f.session.id), false);
assert.ok(model.requests.every(request => request.headers.authorization === 'Bearer fixture-only-secret'));
for (const name of fs.readdirSync(path.join(f.data, 'sessions'))) assert.ok(!fs.readFileSync(path.join(f.data, 'sessions', name), 'utf8').includes('fixture-only-secret'));
assert.ok(!JSON.stringify(f.store.storage.load()).includes('fixture-only-secret'));
for (const artifact of f.store.core.artifact.values()) assert.ok(!f.store.artifactStore.read(artifact).includes('fixture-only-secret'));
assert.ok(!fs.readFileSync(path.join(f.data, 'config.json'), 'utf8').includes('fixture-only-secret'));
});
+2 -1
View File
@@ -29,7 +29,7 @@ test('tool-pair recovery fills missing results and drops orphan or duplicate res
});
test('restart pauses unfinished work, retains proposals and never replays a command', t => {
const { store, session, data, projectRoot } = fixture(t);
const f = fixture(t), { store, session, data, projectRoot } = f;
const proposal = proposeFile(store, session, { path: 'new.md', content: '# Pending\n' });
session.status = 'awaiting_approval';
session.providerMessages = [{ role: 'user', content: 'test' }, { role: 'assistant', content: '', tool_calls: [call('unfinished', 'run_command', { command: 'node --version', reason: 'test' })] }];
@@ -37,6 +37,7 @@ test('restart pauses unfinished work, retains proposals and never replays a comm
session.approvals.push({ id: 'approval', status: 'pending', command: 'node --version' });
store.save(session); const previousRevision = session.revision;
const recoveredStore = new Store(data), recovered = recoveredStore.session(session.id);
f.onCleanup(() => recoveredStore.close());
assert.equal(recovered.status, 'paused');
assert.equal(recovered.events[0].phase, 'interrupted');
assert.match(recovered.events[0].output, /进入执行阶段/);
+5 -2
View File
@@ -8,8 +8,9 @@ import { upgradeRunning } from '../scripts/upgrade-running.mjs';
test('the running 0.1.0 process upgrades in place and retains its project, session, files and volatile model connection', { timeout: 30000 }, async t => {
const f = fixture(t);
const legacyData = path.join(f.root, 'legacy-data'); f.store.exportLegacyData(legacyData);
const oldRoot = path.join(workspace, 'tests', 'fixtures', 'legacy-workbench-0.1');
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: oldRoot, AGENT_WORKBENCH_DATA_DIR: f.data, AGENT_WORKBENCH_PORT: '0' };
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: oldRoot, AGENT_WORKBENCH_DATA_DIR: legacyData, AGENT_WORKBENCH_PORT: '0' };
delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(process.execPath, [path.join(oldRoot, 'dist-server', 'server.mjs')], { cwd: oldRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let url = '', output = '', stderr = '';
@@ -37,7 +38,9 @@ test('the running 0.1.0 process upgrades in place and retains its project, sessi
assert.equal((await api('/diagnostics?session=' + f.session.id)).version, '1.0.0');
assert.deepEqual((await api('/config/models', {})).models, ['upgrade-fixture']);
assert.equal(model.requests[0].headers.authorization, 'Bearer fixture-upgrade-memory-key');
assert.ok(!fs.readFileSync(path.join(f.data, 'config.json'), 'utf8').includes('fixture-upgrade-memory-key'));
assert.ok(!fs.readFileSync(path.join(legacyData, 'config.json'), 'utf8').includes('fixture-upgrade-memory-key'));
assert.equal(bootstrap.storage.engine, 'sqlite');
assert.equal((await api('/sessions/' + f.session.id)).execution.task.origin, 'legacy-import');
const health = await (await fetch(url + '/api/health')).json(); assert.equal(health.pid, child.pid);
await assert.rejects(fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(500) }));
});