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