feat(memory): release Mnemo project bridge and schema2 recovery
Official contracts and Windows source CI / windows-source (push) Has been cancelled

This commit is contained in:
Agent Workbench Dev
2026-11-11 03:03:43 +08:00
parent f7705d9634
commit e306f40cae
76 changed files with 10475 additions and 127 deletions
+47 -2
View File
@@ -4,6 +4,8 @@ import { completionBody, requestMetrics, requestVariants, requestHistory, output
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
import { mnemoMemoryMessage } from '../memory/mnemo-bridge.mjs';
import { contextDigest as mnemoContextDigest, entryRef as mnemoEntryRef, digest as mnemoDigest } from '../memory/mnemo-protocol.mjs';
import { skillCatalogMessage, SKILL_LIMITS } from '../skills/skill-registry.mjs';
import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs';
import { modelReservation,taskConsumption,verifyModelReservation } from '../../shared/contracts/model-reservation.mjs';
@@ -193,7 +195,7 @@ export class RequestPlanner {
return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length };
}
prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, progressReminder = null, transportReservationTokens = 0,engineStepOnly=false }) {
prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, mnemoMemory = null, skillCatalog = null, mcpCatalog = null, progressReminder = null, transportReservationTokens = 0,engineStepOnly=false }) {
const task = this.store.owned('task', run.taskId, session.id);
this.store.owned('run', run.id, session.id);
const history = providerHistory(session, run);
@@ -401,6 +403,26 @@ export class RequestPlanner {
}
}
}
let mnemoReference = null, mnemoRecallReference = null, mnemoDocument = null, mnemoIssue = mnemoMemory?.issue ?? null;
if (mnemoMemory?.projectId === session.projectId) {
try {
mnemoRecallReference = this.artifact(session, run, { ...mnemoMemory, sessionId: session.id, taskId: run.taskId, runId: run.id }, 'context-mnemo-recall');
if (mnemoMemory.entries?.length && !mnemoIssue) {
const { trace, ...source } = mnemoMemory;
mnemoDocument = { ...source, sessionId: session.id, taskId: run.taskId, runId: run.id, entries: [] };
for (const entry of mnemoMemory.entries) {
if (mnemoDocument.entries.length >= (mnemoMemory.selectionLimit ?? 50)) break;
mnemoDocument.entries.push(entry);
const candidate = addContext(messages, mnemoMemoryMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, mnemoDocument));
if (requestMetrics(completionBody(config, candidate, tools)).estimatedInputTokens > Math.min(inputBudget, targetInputTokens)) mnemoDocument.entries.pop();
}
if (mnemoDocument.entries.length) {
mnemoReference = this.artifact(session, run, mnemoDocument, 'context-mnemo-memory');
messages.splice(0, messages.length, ...addContext(messages, mnemoMemoryMessage(mnemoReference, mnemoDocument)));
}
}
} catch { mnemoIssue = { code: 'PROJECT_SNAPSHOT_UNAVAILABLE', message: '可选远端记忆未能保存,本次原任务继续。' }; mnemoReference = null; }
}
const body = completionBody(config, messages, tools), metrics = requestMetrics(body);
if (metrics.estimatedInputTokens > inputBudget) throw pause(budget, metrics, breakdown);
const tokenReservation=engineStepOnly?null:modelReservation(config,body,taskConsumption(this.store,task.id));
@@ -446,7 +468,7 @@ export class RequestPlanner {
// sizing reserves that observed framing growth before returning bytes;
// the next complete request is still checked against its original limit.
readStateGrowthTokens: 1536,
originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0),
originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0) - (mnemoReference ? 1 : 0),
archivedGroups: omitted.length, retainedGroups: groups.filter(group => selected.has(group.index)).map(group => group.index),
historyIndexArtifactId: reference?.id ?? null, historyIndexSha256: reference?.sha256 ?? null, actualUsage: null,
workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length,
@@ -456,6 +478,10 @@ export class RequestPlanner {
projectMemoryArtifactId: memoryReference?.id ?? null, projectMemorySha256: memoryReference?.sha256 ?? null,
projectMemoryEntries: memoryReference ? memoryDocument.entries.length : 0,
...(memoryIssue ? { projectMemoryIssue: memoryIssue } : {}),
...(mnemoMemory ? { mnemoRecallArtifactId: mnemoRecallReference?.id ?? null, mnemoRecallSha256: mnemoRecallReference?.sha256 ?? null,
mnemoMemoryArtifactId: mnemoReference?.id ?? null, mnemoMemorySha256: mnemoReference?.sha256 ?? null,
mnemoRecallContextDigest: mnemoMemory.recallContextDigest ?? null, mnemoSelectedContextDigest: mnemoContextDigest(mnemoReference ? mnemoDocument.entries : []),
mnemoSelectedRefs: mnemoReference ? mnemoDocument.entries.map(mnemoEntryRef) : [], mnemoIssue } : {}),
skillCatalogArtifactId: skillReference?.id ?? null, skillCatalogSha256: skillReference?.sha256 ?? null,
skillCatalogEntries: skillReference ? skillDocument.entries.length : 0,
mcpCatalogArtifactId: mcpReference?.id ?? null, mcpCatalogSha256: mcpReference?.sha256 ?? null,
@@ -593,6 +619,25 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
const message = projectMemoryMessage(artifact, document);
if (!carriesContext(message.content)) throw new Error('项目记忆与实际发送内容不匹配。');
}
for (const key of ['mnemoRecallArtifactId', 'mnemoRecallSha256', 'mnemoMemoryArtifactId', 'mnemoMemorySha256', 'mnemoRecallContextDigest', 'mnemoSelectedContextDigest', 'mnemoSelectedRefs', 'mnemoIssue']) {
if (mnemoDigest(frozen[key] ?? null) !== mnemoDigest(plan[key] ?? null)) throw new Error('Mnemo选择与发送前封存记录不匹配。');
}
if (plan.mnemoRecallArtifactId) {
const recallArtifact = get(plan.mnemoRecallArtifactId, 'context-mnemo-recall', plan.mnemoRecallSha256);
const recall = JSON.parse(store.artifactStore.read(recallArtifact));
if (recallArtifact.taskId !== plan.taskId || recallArtifact.runId !== plan.runId || recall.projectId !== session.projectId || recall.sessionId !== session.id
|| recall.taskId !== plan.taskId || recall.runId !== plan.runId || recall.kind !== 'mnemo-authorized-project-memory') throw new Error('Mnemo召回工件归属不匹配。');
if (plan.mnemoMemoryArtifactId) {
const artifact = get(plan.mnemoMemoryArtifactId, 'context-mnemo-memory', plan.mnemoMemorySha256), document = JSON.parse(store.artifactStore.read(artifact));
if (artifact.taskId !== plan.taskId || artifact.runId !== plan.runId || document.projectId !== session.projectId || document.sessionId !== session.id
|| document.taskId !== plan.taskId || document.runId !== plan.runId || document.kind !== 'mnemo-authorized-project-memory'
|| document.bindingId !== recall.bindingId || document.producerId !== recall.producerId || document.connectionRevision !== recall.connectionRevision
|| mnemoContextDigest(recall.entries) !== plan.mnemoRecallContextDigest || mnemoDigest(document.snapshot) !== mnemoDigest(recall.snapshot)
|| mnemoContextDigest(document.entries) !== plan.mnemoSelectedContextDigest || mnemoDigest(document.entries.map(mnemoEntryRef)) !== mnemoDigest(plan.mnemoSelectedRefs)
|| document.entries.some(e => !recall.entries.some(original => mnemoDigest(original) === mnemoDigest(e)))
|| !carriesContext(mnemoMemoryMessage(artifact, document).content)) throw new Error('Mnemo整条选择与实际发送内容不匹配。');
}
}
if ((frozen.skillCatalogArtifactId ?? null) !== (plan.skillCatalogArtifactId ?? null)
|| (frozen.skillCatalogSha256 ?? null) !== (plan.skillCatalogSha256 ?? null)
|| (frozen.skillCatalogEntries ?? 0) !== (plan.skillCatalogEntries ?? 0)) throw new Error('技能目录与发送前的封存记录不匹配。');