fix: remove open execution record caps and preserve recovery history

This commit is contained in:
Agent Workbench Dev
2026-11-07 03:17:05 +08:00
parent affd733d03
commit 32780bee8d
22 changed files with 595 additions and 74 deletions
+16 -7
View File
@@ -3,6 +3,8 @@ import { randomBytes, randomUUID } from 'node:crypto';
import { companionError, companionHash, companionName, deviceGrant, COMPANION_SCHEMA } from '../../shared/contracts/companion.mjs';
import { assertLocalTree } from '../desktop/file-state.mjs';
import { isId } from '../../shared/contracts/execution.mjs';
import { ceiling } from '../../shared/contracts/execution-policy.mjs';
import { recordPage } from '../../shared/contracts/record-page.mjs';
const key = 'mobileCompanion';
const instant = value => new Date(value).toISOString();
@@ -26,8 +28,7 @@ export class DeviceRegistry {
read() {
const state = this.store.storage.meta(key);
if (state && (state.schema !== COMPANION_SCHEMA || state.binding !== this.binding || !isId(state.gatewayId) || !Number.isSafeInteger(state.revision) || state.revision < 0
|| !dictionary(state.devices) || Object.keys(state.devices).length > 128 || !Array.isArray(state.audit) || state.audit.length > 500
|| !dictionary(state.operations) || Object.keys(state.operations).length > 2000
|| !dictionary(state.devices) || !Array.isArray(state.audit) || !dictionary(state.operations)
|| Object.entries(state.devices).some(([id, device]) => !dictionary(device) || id !== device.id || !isId(id)
|| typeof device.name !== 'string' || !device.name.trim() || device.name.length > 60 || /[\x00-\x1f\x7f]/.test(device.name)
|| !Number.isSafeInteger(device.revision) || device.revision < 1 || !date(device.createdAt) || !date(device.expiresAt)
@@ -50,13 +51,21 @@ export class DeviceRegistry {
}
audit(state, action, deviceId, status, extra = {}) {
state.audit.push({ id: randomUUID(), at: instant(this.clock()), deviceId, action, status, ...extra });
state.audit = state.audit.slice(-500);
}
initialize() { return this.mutate(state => ({ gatewayId: state.gatewayId })); }
view() {
const state = this.read();
const state = this.read(), page = this.auditPage({}, state);
return { gatewayId: state.gatewayId, revision: state.revision, devices: Object.values(state.devices).map(({ credentialHash, ...device }) => ({ ...device,
status: device.revokedAt ? 'revoked' : Date.parse(device.expiresAt) <= this.clock() ? 'expired' : credentialHash ? 'active' : 'reconnect_required' })), audit: state.audit.slice(-50).reverse() };
status: device.revokedAt ? 'revoked' : Date.parse(device.expiresAt) <= this.clock() ? 'expired' : credentialHash ? 'active' : 'reconnect_required' })), audit: page.items, auditPage: page };
}
auditPage(options = {}, state = this.read()) {
return recordPage([...state.audit].reverse(), options, 'companion-audit:' + state.gatewayId);
}
operationsPage(device, options = {}, state = this.read()) {
const records = Object.values(state.operations).filter(value => value.deviceId === device.id && device.projectIds.includes(value.projectId)).reverse()
.map(value => ({ id: value.requestId, requestId: value.requestId, action: value.action, status: value.status === 'pending' ? 'unknown' : value.status,
taskId: value.taskId, runId: value.runId, createdAt: value.createdAt }));
return recordPage(records, options, 'companion-operations:' + state.gatewayId + ':' + device.id + ':' + device.revision);
}
pair(grant, generation) {
this.assertHost(); deviceGrant(this.store, grant);
@@ -72,7 +81,7 @@ export class DeviceRegistry {
deviceGrant(this.store, pairing.grant);
const tokenValue = secret();
const device = this.mutate(state => {
if (Object.keys(state.devices).length >= 128) throw companionError('设备登记达到128条上限,请保留原审计并检查设备登记。', 409);
if (Object.keys(state.devices).length >= ceiling(this.store, 128)) throw companionError('设备登记达到128条上限,请保留原审计并检查设备登记。', 409);
const at = this.clock(), device = { id: randomUUID(), name, projectIds: pairing.grant.projectIds, permissions: pairing.grant.permissions,
revision: 1, createdAt: instant(at), expiresAt: instant(at + pairing.grant.expiresInHours * 3600000), revokedAt: null,
credentialHash: companionHash(tokenValue) };
@@ -121,7 +130,7 @@ export class DeviceRegistry {
if (existing.hash !== hash) throw companionError('同一请求ID不能更换操作或输入。', 409);
return { ...existing, duplicate: true };
}
if (Object.keys(state.operations).length >= 2000) throw companionError('手机操作账本达到上限,请在电脑处理。', 409);
if (Object.keys(state.operations).length >= ceiling(this.store, 2000)) throw companionError('手机操作账本达到上限,请在电脑处理。', 409);
const operation = { hash, deviceId: device.id, requestId: input.requestId, action, sessionId: session.id,
taskId: session.taskId, runId: session.runId, projectId: session.projectId, status: 'pending', createdAt: instant(this.clock()) };
state.operations[id] = operation;
+9 -2
View File
@@ -17,7 +17,7 @@ export class CredentialVault {
}
reload() {
const state = this.store.storage.meta(metadata, empty());
this.blocked = !state || state.schema !== 1 || !Number.isSafeInteger(state.revision) || !state.entries || typeof state.entries !== 'object' || Array.isArray(state.entries) || Object.keys(state.entries).length > 101;
this.blocked = !state || state.schema !== 1 || !Number.isSafeInteger(state.revision) || state.revision < 0 || !state.entries || typeof state.entries !== 'object' || Array.isArray(state.entries);
this.state = this.blocked ? empty() : state;
}
binding(connection) {
@@ -30,8 +30,15 @@ export class CredentialVault {
if (connection.credentialSource) return { status: 'reference', storage: 'source-reference', sourceId: connection.credentialSource.connectionId,
message: '共用上游源连接的凭据与加载状态,请在源连接管理;没有复制或额外保存 Key。' };
if (this.blocked) return { status: 'unavailable', storage: 'windows-user-dpapi', message: '本机保存登记格式损坏或来自新版本,未覆盖原记录。' };
if (!Object.hasOwn(this.state.entries, connection.id)) return { status: 'not_saved', storage: 'memory', message: '当前凭据只在宿主内存中。' };
const value = this.state.entries[connection.id];
if (!value) return { status: 'not_saved', storage: 'memory', message: '当前凭据只在宿主内存中。' };
if (!value || typeof value !== 'object' || Array.isArray(value) || value.schema !== 1 || value.connectionId !== connection.id
|| !Number.isSafeInteger(value.connectionRevision) || value.connectionRevision < 1 || !Number.isSafeInteger(value.credentialRevision) || value.credentialRevision < 1
|| typeof value.enabled !== 'boolean' || typeof value.savedAt !== 'string' || !Number.isFinite(Date.parse(value.savedAt))
|| value.userSid !== null && typeof value.userSid !== 'string' || !/^[a-f0-9]{64}$/.test(value.dataIdentity) || !/^[a-f0-9]{64}$/.test(value.bindingSha256)
|| typeof value.ciphertext !== 'string' || !value.ciphertext.length || value.ciphertext.length > 200000 || !/^[A-Za-z0-9+/]+={0,2}$/.test(value.ciphertext)) {
return { status: 'invalid_record', storage: 'windows-user-dpapi', message: '此连接的原保存登记无法核对,原记录已保留;其他有效连接继续加载。' };
}
const issue = value.userSid !== this.userSid ? 'user_mismatch' : value.dataIdentity !== this.dataIdentity ? 'directory_mismatch'
: value.connectionRevision !== connection.revision || value.bindingSha256 !== this.binding(connection) ? 'source_changed'
: this.issues.get(connection.id);
+15
View File
@@ -41,4 +41,19 @@ export class ArtifactStore {
if (bytes.length !== artifact.bytes || createHash('sha256').update(bytes).digest('hex') !== artifact.sha256) throw new Error('执行工件校验失败,实际结果需要核对。');
return bytes;
}
verify(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 handle = fs.openSync(file, 'r');
try {
const stat = fs.fstatSync(handle);
if (!stat.isFile() || stat.size !== artifact.bytes) throw new Error('执行工件校验失败,实际结果需要核对。');
const hash = createHash('sha256'), buffer = Buffer.allocUnsafe(Math.min(1024 * 1024, Math.max(1, stat.size)));
let length = 0, bytes;
while ((bytes = fs.readSync(handle, buffer, 0, buffer.length, null)) > 0) { length += bytes; hash.update(buffer.subarray(0, bytes)); }
if (length !== artifact.bytes || hash.digest('hex') !== artifact.sha256) throw new Error('执行工件校验失败,实际结果需要核对。');
return { bytes: length, sha256: artifact.sha256 };
} finally { fs.closeSync(handle); }
}
}
+46 -11
View File
@@ -8,6 +8,8 @@ import { pendingDeliveryReview } from '../../shared/contracts/delivery-review.mj
import { assertChangeSetsResolved } from '../../shared/contracts/change-sets.mjs';
import { assertWorkspacesResolved } from '../../shared/contracts/workspaces.mjs';
import { assertMcpResolved } from '../tools/mcp-contract.mjs';
import { ceiling } from '../../shared/contracts/execution-policy.mjs';
import { recordPage } from '../../shared/contracts/record-page.mjs';
const at = () => new Date().toISOString();
const failure = (message, status = 409) => Object.assign(new Error(message), { status });
@@ -19,25 +21,25 @@ const counts = state => ({ tools: state.tools.length, completed: state.tools.fil
export class TaskCheckpointManager {
constructor(store, { taskPlans, redact = String }) { this.store = store; this.taskPlans = taskPlans; this.redact = redact; }
list(session) {
return [...this.store.core.task_checkpoint.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId)
.slice(-TASK_CHECKPOINT_LIMITS.checkpoints).reverse().map(value => structuredClone(value));
list(session, page = null) {
const records = [...this.store.core.task_checkpoint.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId).reverse();
return structuredClone(page ? recordPage(records, page, 'checkpoints:' + session.id + ':' + session.taskId) : records);
}
artifact(session, id, taskId) {
const artifact = this.store.owned('artifact', id, session.id);
if (artifact.taskId !== taskId || this.store.owned('run', artifact.runId, session.id).taskId !== taskId) throw failure('恢复依据的原工件不属于当前任务。');
this.store.artifactStore.readBytes(artifact);
this.store.artifactStore.verify(artifact);
return artifact;
}
ledger(session, task) {
const tools = [...this.store.core.tool.values()].filter(value => value.sessionId === session.id && value.taskId === task.id);
if (tools.length > TASK_CHECKPOINT_LIMITS.tools) throw failure('当前任务超过恢复包2000项工具引用的上限,请缩小任务范围。');
if (tools.length > ceiling(this.store, TASK_CHECKPOINT_LIMITS.tools)) throw failure('当前任务超过恢复包2000项工具引用的上限,请缩小任务范围。');
let resultBytes = 0;
for (const tool of tools) if (tool.resultArtifactId) {
resultBytes += this.store.owned('artifact', tool.resultArtifactId, session.id).bytes;
if (resultBytes > TASK_CHECKPOINT_LIMITS.resultBytes) throw failure('恢复检查点的原结果核验超过64MB上限,请缩小任务范围。');
if (resultBytes > ceiling(this.store, TASK_CHECKPOINT_LIMITS.resultBytes)) throw failure('恢复检查点的原结果核验超过64MB上限,请缩小任务范围。');
}
const calls = new Map(session.providerMessages.flatMap(value => value.tool_calls ?? []).map(value => [value.id, value]));
return tools.map(tool => {
@@ -71,10 +73,41 @@ export class TaskCheckpointManager {
})));
}
sealTools(session, run, tools) {
const segments = [];
// Segment size affects the index layout, not the number of retained tools.
for (let start = 0; start < tools.length; start += 256) {
const entries = tools.slice(start, start + 256);
const artifact = this.store.artifactStore.write(session, run, { schemaVersion: 1, kind: 'task-checkpoint-tools',
sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id, start, tools: entries }, { kind: 'task-checkpoint-tools' });
this.store.core.artifact.set(artifact.id, artifact);
segments.push({ start, count: entries.length, artifact: identity(artifact) });
}
return { count: tools.length, segments };
}
expandTools(session, record, state) {
const index = state.toolIndex;
if (!index || !Number.isSafeInteger(index.count) || index.count < 0 || !Array.isArray(index.segments) || 'tools' in state) throw failure('恢复检查点工具索引无效。');
const tools = [];
for (const segment of index.segments) {
const artifact = this.artifact(session, segment.artifact.id, record.taskId);
if (artifact.runId !== record.runId || artifact.kind !== 'task-checkpoint-tools' || planHash(identity(artifact)) !== planHash(segment.artifact)) throw failure('恢复检查点工具分段身份已变化。');
const original = JSON.parse(this.store.artifactStore.read(artifact));
if (original.schemaVersion !== 1 || original.kind !== 'task-checkpoint-tools' || original.sessionId !== session.id || original.projectId !== session.projectId
|| original.taskId !== record.taskId || original.runId !== record.runId || segment.start !== tools.length || original.start !== tools.length
|| !Array.isArray(original.tools) || !original.tools.length || original.tools.length !== segment.count) throw failure('恢复检查点工具分段顺序或内容不一致。');
for (const tool of original.tools) tools.push(tool);
}
if (tools.length !== index.count) throw failure('恢复检查点工具分段不完整。');
const { toolIndex, ...rest } = state;
return { tools, ...rest };
}
create(session) { return this.taskPlans.mutate(session, 'user', async () => {
const { task, run } = this.taskPlans.task(session), taskId = task.id, runId = run.id;
if (['running', 'awaiting_approval', 'stopping'].includes(session.status)) throw failure('先停止当前任务,再封存稳定的恢复检查点。');
if (this.list(session).length >= TASK_CHECKPOINT_LIMITS.checkpoints) throw failure('当前任务已达到30个恢复检查点上限。');
if (this.list(session).length >= ceiling(this.store, TASK_CHECKPOINT_LIMITS.checkpoints)) throw failure('当前任务已达到30个恢复检查点上限。');
const requirements = taskRequirements(task), requirementsSha256 = planHash(requirements);
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
const inspection = await inspectProject(this.store.project(session.projectId));
@@ -84,15 +117,16 @@ export class TaskCheckpointManager {
if (lastContext) verifyPlanEvidence(this.store, session, lastContext);
if (session.taskId !== taskId || session.runId !== runId || planHash(taskRequirements(task)) !== requirementsSha256) throw failure('保存期间任务或要求已变化,请重新核对。');
const safeRequirements = JSON.parse(this.redact(JSON.stringify(requirements)));
const bundle = { schemaVersion: 1, kind: 'task-recovery-checkpoint', sessionId: session.id,
const { tools, ...rest } = state;
const bundle = { schemaVersion: 2, kind: 'task-recovery-checkpoint', sessionId: session.id,
projectId: session.projectId, taskId, runId, savedAt: at(), requirements: safeRequirements, requirementsSha256,
workspace: projectWorkspaceIdentity(this.store.project(session.projectId)), sourceFingerprint: inspection.sourceFingerprint,
sourceScope: inspection.scope, git: { head: inspection.git.head, branch: inspection.git.branch }, state,
sourceScope: inspection.scope, git: { head: inspection.git.head, branch: inspection.git.branch }, state: { ...rest, toolIndex: this.sealTools(session, run, tools) },
lastRequest: lastContext ? { planId: lastContext.id, artifactId: lastContext.requestArtifactId, sha256: lastContext.requestSha256 } : null,
note: '恢复包是原任务数据与结果引用,不授予权限,不重放工具,不将未知调用改为成功。' };
const artifact = this.store.artifactStore.write(session, run, bundle, { kind: 'task-recovery-checkpoint' });
this.store.core.artifact.set(artifact.id, artifact);
const record = { schemaVersion: 1, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId, runId,
const record = { schemaVersion: 2, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId, runId,
status: 'stored', createdAt: bundle.savedAt, artifactId: artifact.id, sha256: artifact.sha256,
sourceFingerprint: inspection.sourceFingerprint, requirementsSha256, stateSha256: planHash(state),
counts: counts(state) };
@@ -102,12 +136,13 @@ export class TaskCheckpointManager {
original(session, id) {
const record = this.store.owned('task_checkpoint', id, session.id), artifact = this.artifact(session, record.artifactId, record.taskId);
const bundle = JSON.parse(this.store.artifactStore.read(artifact));
if (![1, 2].includes(bundle.schemaVersion) || bundle.schemaVersion !== record.schemaVersion) throw failure('恢复检查点版本无法核对。');
if (bundle.schemaVersion === 2) bundle.state = this.expandTools(session, record, bundle.state);
if (artifact.kind !== 'task-recovery-checkpoint' || artifact.sha256 !== record.sha256 || artifact.runId !== record.runId
|| bundle.kind !== 'task-recovery-checkpoint' || bundle.sessionId !== session.id || bundle.projectId !== session.projectId
|| bundle.taskId !== record.taskId || bundle.runId !== record.runId || bundle.requirementsSha256 !== record.requirementsSha256
|| bundle.sourceFingerprint !== record.sourceFingerprint || planHash(bundle.state) !== record.stateSha256
|| planHash(counts(bundle.state)) !== planHash(record.counts)) throw failure('恢复检查点与原封存身份或内容不一致。');
if (bundle.state.tools.length > TASK_CHECKPOINT_LIMITS.tools || bundle.state.tools.reduce((sum, tool) => sum + (tool.resultArtifact?.bytes ?? 0), 0) > TASK_CHECKPOINT_LIMITS.resultBytes) throw failure('恢复包的原工具结果范围超过核验上限。');
for (const tool of bundle.state.tools) if (tool.resultArtifact) {
const source = this.artifact(session, tool.resultArtifact.id, record.taskId);
if (planHash(identity(source)) !== planHash(tool.resultArtifact)) throw failure('恢复包引用的原工具工件身份已变化。');
+8 -3
View File
@@ -4,6 +4,7 @@ import path from 'node:path';
import os from 'node:os';
import { createHash, randomUUID } from 'node:crypto';
import { McpStdio } from './mcp-stdio.mjs';
import { recordPage } from '../../shared/contracts/record-page.mjs';
import { MCP_PROTOCOL, MCP_LIMITS, MCP_ACTIVE, mcpError, mcpHash, checkMcpSchema, validateMcpArguments, mcpCallInput } from './mcp-contract.mjs';
const at = () => new Date().toISOString();
@@ -55,7 +56,8 @@ export class McpRegistry {
return { ...value, status, reason, tools };
});
return JSON.parse(this.redact(JSON.stringify({ projectId, revision: state.revision, registrations, protocolVersion: MCP_PROTOCOL,
limits: MCP_LIMITS, boundary: '所属独立进程,不是OS沙箱;只锁定程序/入口与实际握手契约,依赖文件未全量封存。' })));
limits: Object.fromEntries(Object.entries(MCP_LIMITS).map(([key, value]) => [key, ceiling(this.store, value)])),
executionMode: this.store.config.executionMode, boundary: '所属独立进程,不是OS沙箱;只锁定程序/入口与实际握手契约,依赖文件未全量封存。' })));
}
register(projectId, input) {
this.idle(projectId); fields(input, ['name', 'executable', 'entry', 'timeoutMs']);
@@ -112,13 +114,16 @@ export class McpRegistry {
&& entry.configurationRevision === value.configurationRevision && entry.contractSha256 === input.contract_sha256 && entry.tool === input.tool && mcpHash(entry.inputSchema) === mcpHash(tool.inputSchema))) throw mcpError('工具不在本次发送的MCP目录范围内。');
return value;
}
jobs(session) { return [...this.store.core.mcp_job.values()].filter(value => value.sessionId === session.id).slice(-30).reverse(); }
jobs(session, page = null) {
const records = [...this.store.core.mcp_job.values()].filter(value => value.sessionId === session.id).reverse();
return page ? recordPage(records, page, 'mcp:' + session.id) : records;
}
start(session, input, { source = 'user', signal = null, call = null, invocationId = null } = {}) {
fields(input, ['id', 'configurationRevision', 'confirm']);
if (session.archived || !Number.isSafeInteger(input.configurationRevision) || input.confirm !== true) throw mcpError('启动MCP程序需要确认当前登记身份。', 400);
const value = this.find(session.projectId, input.id, input.configurationRevision); this.verifyLaunch(value);
if (this.taskPlans.projectActive(session.projectId) || this.projectBusy(session.projectId, source === 'agent' ? session.id : null)) throw mcpError('项目正在执行其他操作,请等待结束后核验MCP。');
if (this.jobs(session).filter(job => job.taskId === session.taskId).length >= 30) throw mcpError('当前任务已达到30次MCP操作上限。');
if (this.jobs(session).filter(job => job.taskId === session.taskId).length >= ceiling(this.store, 30)) throw mcpError('当前任务已达到30次MCP操作上限。');
if ([...this.store.core.mcp_job.values()].some(job => job.projectId === session.projectId && job.requiresVerification)) throw mcpError('项目有外部MCP结果待核对,请先审查所属原记录。');
if (!this.store.core.task.get(session.taskId)) {
if (source !== 'user' || this.projectBusy(session.projectId)) throw mcpError('当前项目有任务正在执行。');