fix: remove open execution record caps and preserve recovery history
This commit is contained in:
@@ -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('恢复包引用的原工具工件身份已变化。');
|
||||
|
||||
Reference in New Issue
Block a user