openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 11.4 kB
JavaScript
import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{n as sha256Hex}from"./node-crypto-DPk8agfR.mjs";import"./crypto-digest-T8X5EZni.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{a as recordLegacyMigrationReceipt,i as readLegacyMigrationReceiptFromDatabase,o as recordLegacyMigrationRun}from"./state-migrations.receipts-DhYgpkdJ.mjs";import{o as resolveSqliteTargetFromSessionStorePath}from"./session-sqlite-target-fDZeQkPI.mjs";import{s as recordStartupMigrationWarnings}from"./state-migrations.messages-BurXA9TV.mjs";import{a as shouldFilterLegacySessionRecordsByTarget,i as resolveLegacyTranscriptPaths,r as readLegacySessionStoreEntries,t as isLegacySessionRecordOwnedByTarget}from"./legacy-store-inspection-CVPBQ67e.mjs";import{D as statMigrationPath,E as sameSourceContent,T as sameMigrationArtifact,g as resolveSessionSqliteMigrationRunsDir,w as readMigrationArtifactIdentity}from"./session-sqlite-migration-manifest-1oxXdbak.mjs";import{a as resolveDeferredPluginSessionSourceKey,c as existingSessionSourcePaths,i as readDeferredPluginSessionImportReceipt,n as databaseIdentity,o as DeferredPluginSessionImportSchema,r as readDeferredPluginSessionImportBindingReceipt,s as collectArchivedSources,t as DEFERRED_PLUGIN_SESSION_IMPORT_RECEIPT_KIND}from"./deferred-plugin-session-source-read-hLiVx0-h.mjs";import path from"node:path";import{isDeepStrictEqual}from"node:util";
//!src/infra/state-migrations.deferred-session-import.ts
function parseSessionImportReceipt(e,t){const n=DeferredPluginSessionImportSchema.parse(JSON.parse(t.reportJson));const r=databaseIdentity(e.sqlitePath);const i=databaseIdentity(e.sqlitePath,`physical`);return{recorded:n,currentDatabaseIdentity:r,physicalIdentity:i,stale:Boolean(n.superseded)||n.databaseIdentity!==r&&n.databaseIdentity!==i}}function supersedeDeferredPluginSessionImport(e){const{receipt:t}=e;runOpenClawStateWriteTransaction(({db:n})=>{if(!isDeepStrictEqual(readLegacyMigrationReceiptFromDatabase(n,t.sourceKey),t)||databaseIdentity(e.sqlitePath)!==e.databaseIdentity||databaseIdentity(e.sqlitePath,`physical`)!==e.physicalIdentity)throw new Error(`Deferred session receipt or database changed before supersession; originals retained.`);const r=Date.now();const i=JSON.parse(t.reportJson);recordLegacyMigrationRun(n,{runId:`${t.sourceKey}:superseded:${sha256Hex(t.reportJson+`\0`+e.databaseIdentity)}`,startedAt:r,finishedAt:r,status:`superseded`,upsert:true,reportJson:JSON.stringify({receipt:i,databaseIdentity:e.databaseIdentity,reason:`Receipt bound to a different database; it cannot certify the live database's history.`})});executeSqliteQuerySync(n,getNodeSqliteKysely(n).updateTable(`migration_sources`).set({status:`superseded`,report_json:JSON.stringify({...i,superseded:`different-database`})}).where(`source_key`,`=`,t.sourceKey))},{env:e.env},{operationLabel:`state.supersede-plugin-session-source`})}
//!src/infra/deferred-plugin-session-sources.ts
function deferredPluginSessionStoreIds(e){if(e.target.storePath.endsWith(`.sqlite`))return[];return e.pending.map(e=>e.pluginId)}function preserveDeferredPluginSessionSource(e){if(deferredPluginSessionStoreIds(e).length>0)return true;const t=resolveSqliteTargetFromSessionStorePath(e.target.storePath,{agentId:e.target.agentId,env:e.env});return hasDeferredPluginSessionImport({target:{...e.target,sqlitePath:e.target.sqlitePath??t.path},sqlitePath:e.target.sqlitePath??t.path,env:e.env})}function prepareSessionSourceVerification(e){const t=/* @__PURE__ */ new Map;return{cfg:e.cfg,target:e.target,resolvedTarget:{agentId:e.target.agentId,storePath:e.target.storePath,sqlitePath:e.sqlitePath},sqlitePath:e.sqlitePath,env:e.env,verification:t}}function sessionSourceVerificationTarget(e,t,n){const r=JSON.stringify([resolveDeferredPluginSessionSourceKey(e),resolveSessionSqliteMigrationRunsDir(t)]);let i=n.get(r);if(!i){i={resolved:/* @__PURE__ */ new Map};n.set(r,i)}return i}function sessionSourceArchives(e,t,n){const r=sessionSourceVerificationTarget(e,t,n);return r.archives??=collectArchivedSources(e,t)}function resolveVerifiedSessionSource(e,t,n,r,i){const a=sessionSourceVerificationTarget(t,n,r);const o=a.resolved.get(e.path)??[];const s=o.find(({identity:t})=>sameMigrationArtifact(t,e.identity));if(s)return s.path;const c=statMigrationPath(e.path)?sameSourceContent(readMigrationArtifactIdentity(e.path),e.identity)?e.path:void 0:(i??sessionSourceArchives(t,n,r)).get(e.path)?.find(({identity:t,path:n})=>sameSourceContent(t,e.identity)&&statMigrationPath(n)&&sameSourceContent(readMigrationArtifactIdentity(n),e.identity))?.path;o.push({identity:{...e.identity},path:c});a.resolved.set(e.path,o);return c}function assertVerifiedSessionSources(e,t,n=/* @__PURE__ */ new Map,r){const i={...e.target,sqlitePath:e.sqlitePath};const a=/* @__PURE__ */ new Map;for(const o of t.sources){let t;try{t=resolveVerifiedSessionSource(o,i,e.env,n)}catch(e){if(!r)throw e}if(!t){if(r){r(o.path);for(const t of existingSessionSourcePaths(o.path,sessionSourceArchives(i,e.env,n)))if(t!==o.path)r(o.path,t);continue}throw new Error(`Retained session migration source changed: ${o.path}. Preserve the current file and its ${o.path}.bak-<pid>-<timestamp> siblings, restore the verified original at ${o.path}, then run openclaw doctor --session-sqlite recover --session-sqlite-all-agents against the same state directory. Canonical SQLite sessions were not replayed.`)}a.set(o.path,t)}const o=t.sources.find(t=>t.path===path.resolve(e.target.storePath));const s=o&&a.get(o.path);if(!o&&statMigrationPath(e.target.storePath)&&r)r(path.resolve(e.target.storePath));if(r&&(!o||!s)||!o&&!statMigrationPath(e.target.storePath))return;if(!o||!s)throw new Error(`Session source changed in ${path.dirname(e.target.storePath)}: the current index has no verified import receipt. Run ${formatCliCommand(`openclaw doctor --fix --non-interactive --yes`,e.env)} against the same state/config to preserve and reverify it.`);const c=[];const l=readLegacySessionStoreEntries(e.target,c,{sourcePath:s});if(c.some(e=>e.code!==`entry_invalid`)||!l.bytes||l.bytes.length!==o.identity.size||sha256Hex(l.bytes)!==o.identity.sha256)throw new Error(`Retained session migration source changed in ${path.dirname(e.target.storePath)}: ${e.target.storePath}. Run ${formatCliCommand(`openclaw doctor --fix --non-interactive --yes`,e.env)} against the same state/config to preserve and reverify it.`);for(const{entry:n,sessionKey:i}of l.entries){if(shouldFilterLegacySessionRecordsByTarget(e.target)&&!isLegacySessionRecordOwnedByTarget(e.cfg,e.target,i))continue;const{transcriptCandidates:o}=resolveLegacyTranscriptPaths(e.target,n);if(o.some(e=>a.has(path.resolve(e))||r&&t.sources.some(t=>t.path===path.resolve(e))))continue;for(const e of o)if(statMigrationPath(e)){if(r){r(e);continue}throw new Error(`Retained session migration source changed: ${e}. A previously unimported transcript appeared; run openclaw doctor --fix to preserve it in the migration archive.`)}}}function readDeferredPluginSessionImport(e){const t=readDeferredPluginSessionImportReceipt(e);if(!t)return;const n=parseSessionImportReceipt(e,t);if(n.stale){recordStartupMigrationWarnings([`Retained session import receipt belongs to a different database; sources remain protected. Run openclaw doctor --fix to recover retained history against the live database.`]);return}let r={...n.recorded,databaseIdentity:n.currentDatabaseIdentity};if(e.purpose)return r;const i=e.verification??/* @__PURE__ */ new Map;if(e.allowMissingIndex&&!statMigrationPath(e.target.storePath))r={...r,sources:r.sources.filter(t=>t.path!==path.resolve(e.target.storePath)||resolveVerifiedSessionSource(t,{...e.target,sqlitePath:e.sqlitePath},e.env,i)||existingSessionSourcePaths(t.path,sessionSourceArchives({...e.target,sqlitePath:e.sqlitePath},e.env,i)).length>0)};assertVerifiedSessionSources(e,r,i,e.onSourceConflict);return r}function hasDeferredPluginSessionImport(e){return Boolean(readDeferredPluginSessionImportReceipt(e))}function readStaleDeferredPluginSessionImport(e){const t=readDeferredPluginSessionImportReceipt(e);const n=t&&parseSessionImportReceipt(e,t);return n?.stale?n.recorded:void 0}function resolveDeferredPluginSessionStoreSource(e){const{path:t}=resolveSqliteTargetFromSessionStorePath(e.target.storePath,{agentId:e.target.agentId,env:e.env});const n=readDeferredPluginSessionImportBindingReceipt({...e,sqlitePath:t});const r=n&&parseSessionImportReceipt({sqlitePath:t},n);const i=r&&!r.stale?r.recorded.sources.find(t=>t.path===path.resolve(e.target.storePath)):void 0;return i?resolveVerifiedSessionSource(i,{...e.target,sqlitePath:t},e.env,/* @__PURE__ */ new Map):void 0}function prepareDeferredPluginSessionImportReader(e){const t=/* @__PURE__ */ new Map;return(n,r)=>{const i={...e.target,agentId:r};const a=resolveSqliteTargetFromSessionStorePath(i.storePath,{agentId:r,env:e.env});const o={...i,sqlitePath:a.path};const s=resolveDeferredPluginSessionSourceKey(o);const c=readLegacyMigrationReceiptFromDatabase(n,s);let l=t.get(s);if(!l||!isDeepStrictEqual(l.receipt,c)){l={receipt:c,binding:c?parseSessionImportReceipt(o,c):void 0};t.set(s,l)}const u=l.binding;if(!u)return{kind:`absent`};if(u.currentDatabaseIdentity!==databaseIdentity(o.sqlitePath))throw new Error(`The verified session import database changed for ${path.dirname(o.storePath)}; retained source was not replayed. Run ${formatCliCommand(`openclaw doctor --session-sqlite recover --session-sqlite-all-agents`,e.env)} against the same state/config.`);return u.stale?{kind:`stale`}:{kind:`canonical`,target:o}}}function recordDeferredPluginSessionImport(e){const t={databaseIdentity:databaseIdentity(e.sqlitePath),pluginIds:e.pluginIds,sources:e.sources};const n=e.sources.find(t=>t.path===path.resolve(e.target.storePath));runOpenClawStateWriteTransaction(({db:r})=>{assertVerifiedSessionSources(e,t);if(t.databaseIdentity!==databaseIdentity(e.sqlitePath))throw new Error(`Session import database changed for ${path.dirname(e.target.storePath)} before its receipt was recorded. Run ${formatCliCommand(`openclaw doctor --fix --non-interactive --yes`,e.env)} against the same state/config to retry.`);const i=resolveDeferredPluginSessionSourceKey({...e.target,sqlitePath:e.sqlitePath});const a=readLegacyMigrationReceiptFromDatabase(r,i);recordLegacyMigrationReceipt(r,{sourceKey:i,migrationKind:DEFERRED_PLUGIN_SESSION_IMPORT_RECEIPT_KIND,sourcePath:path.resolve(e.target.storePath),targetTable:`session_nodes`,sourceSha256:n?.identity.sha256??null,sourceSizeBytes:n?.identity.size??null,sourceRecordCount:e.recordCount,runId:i,reportJson:JSON.stringify(t),now:Date.now(),upsert:Boolean(a&&(a.removedSource||parseSessionImportReceipt(e,a).recorded.superseded))})},{env:e.env},{operationLabel:`state.retain-plugin-session-source`})}export{preserveDeferredPluginSessionSource as a,recordDeferredPluginSessionImport as c,sessionSourceArchives as d,parseSessionImportReceipt as f,prepareSessionSourceVerification as i,resolveDeferredPluginSessionStoreSource as l,hasDeferredPluginSessionImport as n,readDeferredPluginSessionImport as o,supersedeDeferredPluginSessionImport as p,prepareDeferredPluginSessionImportReader as r,readStaleDeferredPluginSessionImport as s,deferredPluginSessionStoreIds as t,resolveVerifiedSessionSource as u};