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
+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'; }
}