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
+73 -33
View File
@@ -13,6 +13,18 @@ import { hostAuthority } from '../core/desktop/host-authority.mjs';
import { CredentialVault } from '../core/models/credential-vault.mjs';
import { NetworkDiagnostics } from '../core/models/network-diagnostics.mjs';
import { StateDatabase } from '../core/storage/state-database.mjs';
import { randomUUID } from 'node:crypto';
import { ownedChild } from '../core/desktop/file-state.mjs';
// Only the installing bundle owns this synchronous undo closure. Its ledger
// guard refuses any new/changed outcome before restoring the previous objects.
export function rollbackUnchangedUpgrade(context, { appDirectory } = {}) {
const adoption = context.server?.workbenchLedgerAdoption;
if (!adoption || adoption.root !== fs.realpathSync.native(path.resolve(appDirectory))
|| adoption.store !== context.store || adoption.provider !== context.provider || adoption.runtime !== context.runtime) throw new Error('No source-bound unchanged ledger adoption is available for rollback.');
adoption.restore();
return { restored: true, newRequestsDispatched: false };
}
// Keep the existing objects and their in-memory connection. No credential is
// extracted, transferred, returned, or persisted by this operation.
@@ -30,7 +42,11 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
const root = fs.realpathSync.native(path.resolve(appDirectory));
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 serviceInfoFile = ownedChild(store.directory, 'service-info.json', { missing: true });
const originalServiceInfo = fs.existsSync(serviceInfoFile) ? fs.readFileSync(serviceInfoFile) : null;
let publishedServiceInfo = null;
const oldCompanion = server.workbenchCompanion;
const oldLedgerAdoption = server.workbenchLedgerAdoption;
const oldAuthority = server.workbenchHostAuthority, oldAuthorityProperties = oldAuthority ? { ...oldAuthority } : null;
const oldAuthorityPrototype = oldAuthority ? Object.getPrototypeOf(oldAuthority) : null;
const oldPrototypes = [Object.getPrototypeOf(store), Object.getPrototypeOf(provider), Object.getPrototypeOf(runtime)];
@@ -41,9 +57,10 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
network: provider.hub?.networkChecks ? Object.getPrototypeOf(provider.hub.networkChecks) : null,
catalog: provider.hub?.catalog ? Object.getPrototypeOf(provider.hub.catalog) : null };
const oldStoreProperties = { ...store };
const oldHubProperties = provider.hub ? { ...provider.hub } : null, oldStoragePrototype = store.storage ? Object.getPrototypeOf(store.storage) : null;
const oldHubProperties = provider.hub ? { ...provider.hub } : null, oldHubDescriptors = provider.hub ? Object.getOwnPropertyDescriptors(provider.hub) : null;
const oldStoragePrototype = store.storage ? Object.getPrototypeOf(store.storage) : null;
let initializedLedger = null;
const oldRuntimeProperties = { filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp };
const oldRuntimeProperties = { results: runtime.results, filePreviews: runtime.filePreviews, browsing: runtime.browsing, previews: runtime.previews, evaluations:runtime.evaluations, environments: runtime.environments, appDirectory: runtime.appDirectory, workers: runtime.workers, controlledEngines: runtime.controlledEngines, checks: runtime.checks, deliveries: runtime.deliveries, codeIndex: runtime.codeIndex, changeSets: runtime.changeSets, contextPlans: runtime.contextPlans, workspaces: runtime.workspaces, research: runtime.research, engines: runtime.engines, browser: runtime.browser, taskPlans: runtime.taskPlans, projectMemories: runtime.projectMemories, taskCheckpoints: runtime.taskCheckpoints, skills: runtime.skills, mcp: runtime.mcp };
const oldUpdates = store.listeners('update');
const oldInputs = runtime.inputs;
const oldResumes = runtime.resumes;
@@ -51,6 +68,55 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
if (oldUpdates.length > 1) throw new Error('Unexpected Store observers; preserve their ownership before upgrading.');
const handler = createRequestHandler({ appDirectory: root, server, store, provider, runtime, token, clients, started,
exampleProject, shutdown, getPort: () => server.address().port });
const restoreOriginal = () => {
const readServiceInfo = () => fs.existsSync(serviceInfoFile) ? fs.readFileSync(ownedChild(store.directory, 'service-info.json')) : null;
const expectedServiceInfo = publishedServiceInfo ?? originalServiceInfo;
const currentServiceInfo = readServiceInfo();
if (expectedServiceInfo ? !currentServiceInfo?.equals(expectedServiceInfo) : currentServiceInfo !== null) throw new Error('The host index changed outside this adoption; preserve its current bytes before rollback.');
initializedLedger?.rollbackInitialization();
if (oldCompanion) server.workbenchCompanion = oldCompanion; else delete server.workbenchCompanion;
if (oldLedgerAdoption) server.workbenchLedgerAdoption = oldLedgerAdoption; else delete server.workbenchLedgerAdoption;
if (oldAuthority) {
for (const key of Object.keys(oldAuthority)) if (!Object.hasOwn(oldAuthorityProperties, key)) delete oldAuthority[key];
Object.assign(oldAuthority, oldAuthorityProperties); Object.setPrototypeOf(oldAuthority, oldAuthorityPrototype);
server.workbenchHostAuthority = oldAuthority;
} else delete server.workbenchHostAuthority;
server.removeListener('close', closeStorage);
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.assign(runtime, oldRuntimeProperties);
if (oldInputs === undefined) delete runtime.inputs; else runtime.inputs = oldInputs;
if (oldResumes === undefined) delete runtime.resumes; else runtime.resumes = oldResumes;
Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]);
if (oldStoragePrototype && store.storage === oldStoreProperties.storage) Object.setPrototypeOf(store.storage, oldStoragePrototype);
if (oldHubProperties) { for (const key of Object.keys(provider.hub)) if (!Object.hasOwn(oldHubProperties, key)) delete provider.hub[key]; Object.defineProperties(provider.hub, oldHubDescriptors); }
if (oldModelPrototypes.hub) Object.setPrototypeOf(provider.hub, oldModelPrototypes.hub);
if (oldModelPrototypes.ledger) Object.setPrototypeOf(provider.hub.ledger, oldModelPrototypes.ledger);
if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer);
if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault);
if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network);
if (oldModelPrototypes.catalog) Object.setPrototypeOf(provider.hub.catalog, oldModelPrototypes.catalog);
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
// Restoring an already-bound authority makes refreshInstallation a no-op.
// Restore its public index explicitly, after proving our published bytes
// are still current. Never re-create registration secrets or credentials.
if (publishedServiceInfo && !publishedServiceInfo.equals(originalServiceInfo ?? Buffer.alloc(0))) {
const latest = readServiceInfo();
if (!latest?.equals(publishedServiceInfo)) throw new Error('The published host index changed while restoring the original handler.');
if (originalServiceInfo) {
const temporary = ownedChild(store.directory, 'service-info.rollback-' + randomUUID() + '.json', { missing: true });
fs.writeFileSync(temporary, originalServiceInfo, { flag: 'wx', mode: 0o600 });
try { fs.renameSync(temporary, serviceInfoFile); } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); }
} else fs.unlinkSync(serviceInfoFile);
}
};
try {
Object.setPrototypeOf(store, Store.prototype);
Object.setPrototypeOf(provider, Provider.prototype);
@@ -80,41 +146,15 @@ export function installUpgrade(context, { appDirectory, updateServiceInfo = true
server.prependOnceListener('close', closeStorage);
if (updateServiceInfo) {
const authority = hostAuthority({ appDirectory: root, server, store, provider, runtime });
authority.initialize(); authority.refreshInstallation();
authority.initialize(); publishedServiceInfo = fs.readFileSync(serviceInfoFile);
authority.refreshInstallation(); publishedServiceInfo = fs.readFileSync(serviceInfoFile);
}
if (initializedLedger) server.workbenchLedgerAdoption = { root, store, provider, runtime, restore: restoreOriginal };
else delete server.workbenchLedgerAdoption;
void oldRuntimeProperties.filePreviews?.stopAll();
return { version: APP_VERSION, pid: process.pid, hasKey: provider.hasKey(), projects: store.projects.length, sessions: store.sessions.size, appDirectory: root };
} catch (error) {
initializedLedger?.rollbackInitialization();
if (oldCompanion) server.workbenchCompanion = oldCompanion; else delete server.workbenchCompanion;
if (oldAuthority) {
for (const key of Object.keys(oldAuthority)) if (!Object.hasOwn(oldAuthorityProperties, key)) delete oldAuthority[key];
Object.assign(oldAuthority, oldAuthorityProperties); Object.setPrototypeOf(oldAuthority, oldAuthorityPrototype);
server.workbenchHostAuthority = oldAuthority;
} else delete server.workbenchHostAuthority;
server.removeListener('close', closeStorage);
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.assign(runtime, oldRuntimeProperties);
if (oldInputs === undefined) delete runtime.inputs; else runtime.inputs = oldInputs;
if (oldResumes === undefined) delete runtime.resumes; else runtime.resumes = oldResumes;
Object.setPrototypeOf(store, oldPrototypes[0]); Object.setPrototypeOf(provider, oldPrototypes[1]); Object.setPrototypeOf(runtime, oldPrototypes[2]);
if (oldStoragePrototype && store.storage === oldStoreProperties.storage) Object.setPrototypeOf(store.storage, oldStoragePrototype);
if (oldHubProperties) { for (const key of Object.keys(provider.hub)) if (!Object.hasOwn(oldHubProperties, key)) delete provider.hub[key]; Object.assign(provider.hub, oldHubProperties); }
if (oldModelPrototypes.hub) Object.setPrototypeOf(provider.hub, oldModelPrototypes.hub);
if (oldModelPrototypes.ledger) Object.setPrototypeOf(provider.hub.ledger, oldModelPrototypes.ledger);
if (oldModelPrototypes.importer) Object.setPrototypeOf(provider.hub.importer, oldModelPrototypes.importer);
if (oldModelPrototypes.vault) Object.setPrototypeOf(provider.hub.credentialVault, oldModelPrototypes.vault);
if (oldModelPrototypes.network) Object.setPrototypeOf(provider.hub.networkChecks, oldModelPrototypes.network);
if (oldModelPrototypes.catalog) Object.setPrototypeOf(provider.hub.catalog, oldModelPrototypes.catalog);
store.removeAllListeners('update'); for (const observer of oldUpdates) store.on('update', observer);
server.removeAllListeners('request'); for (const previous of oldHandlers) server.on('request', previous);
restoreOriginal();
throw error;
}
}