UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.59 kB
import{a as extractErrorCode}from"./error-coercion-DlKnt6w-.mjs";import"./error-runtime-DW-sj0iq.mjs";import{c as readWorkspaceText,i as listWorkspaceDirectory,l as renameWorkspacePath,o as makeWorkspaceDirectory,r as inspectWorkspaceFile,t as accessWorkspacePath}from"./memory-workspace-files-D24OuQu8.mjs";import{b as readMemoryCoreWorkspaceEntries,d as clearMemoryCoreWorkspaceNamespace,i as DREAMING_SESSION_INGESTION_SEEN_NAMESPACE,r as DREAMING_SESSION_INGESTION_FILES_NAMESPACE}from"./dreaming-state-CWg6oPZQ.mjs";import{nt as withMemoryWorkspaceLock}from"./dreaming-dreams-file-ILfImmSq.mjs";import{r as pruneArchivedShortTermRecallEntries}from"./short-term-promotion-g56auv8V.mjs";import path from"node:path";import{randomUUID}from"node:crypto"; //!extensions/memory-core/src/dreaming-repair.ts const DREAMS_FILENAMES=[`DREAMS.md`,`dreams.md`];const SESSION_CORPUS_RELATIVE_DIR=path.join(`memory`,`.dreams`,`session-corpus`);const SESSION_INGESTION_RELATIVE_PATH=path.join(`memory`,`.dreams`,`session-ingestion.json`);const REPAIR_ARCHIVE_RELATIVE_DIR=path.join(`.openclaw-repair`,`dreaming`);const DREAMING_NARRATIVE_RUN_PREFIX=`dreaming-narrative-`;const DREAMING_NARRATIVE_PROMPT_PREFIX=`Write a dream diary entry from these memory fragments`;const SESSION_INGESTION_NAMESPACES=[DREAMING_SESSION_INGESTION_FILES_NAMESPACE,DREAMING_SESSION_INGESTION_SEEN_NAMESPACE];function requireAbsoluteWorkspaceDir(e){const t=e.trim();if(!t)throw new Error(`workspaceDir is required`);if(!path.isAbsolute(t))throw new Error(`workspaceDir must be an absolute path`);return path.resolve(t)}async function resolveExistingDreamsPath(e){for(const t of DREAMS_FILENAMES){const n=path.join(e,t);try{await accessWorkspacePath(e,n);return n}catch(e){if(extractErrorCode(e)!==`ENOENT`)throw e}}}async function listSessionCorpusFiles(e,t){return(await listWorkspaceDirectory(e,t)).filter(e=>e.isFile()&&e.name.endsWith(`.txt`)).map(e=>path.join(t,e.name)).toSorted()}function isSuspiciousSessionCorpusLine(e){return e.includes(DREAMING_NARRATIVE_PROMPT_PREFIX)&&e.includes(DREAMING_NARRATIVE_RUN_PREFIX)}async function ensureArchivablePath(e,t){const n=await inspectWorkspaceFile(e,t,false).catch(e=>{if(extractErrorCode(e)===`ENOENT`)return null;throw e});if(!n)return false;if(n.isSymbolicLink())throw new Error(`Refusing to archive symlinked path: ${t}`);if(n.isDirectory()||n.isFile())return true;throw new Error(`Refusing to archive non-file artifact: ${t}`)}async function auditDreamingArtifacts(e){const t=requireAbsoluteWorkspaceDir(e.workspaceDir);const n=await resolveExistingDreamsPath(t);const r=path.join(t,SESSION_CORPUS_RELATIVE_DIR);const i=path.join(t,SESSION_INGESTION_RELATIVE_PATH);const a=[];let o=0;let s=0;let c=0;let l=false;const inspect=async(e,t,n)=>{try{await n()}catch(n){if(e===`diary`||extractErrorCode(n)!==`ENOENT`)a.push({severity:`error`,code:`dreaming-${e}-unreadable`,message:`${t} could not be inspected: ${extractErrorCode(n)??`error`}.`,fixable:false})}};if(n)await inspect(`diary`,`Dream diary`,()=>accessWorkspacePath(t,n));await inspect(`session-corpus`,`Dreaming session corpus`,async()=>{const e=await listSessionCorpusFiles(t,r);o=e.length;for(const n of e){const e=(await readWorkspaceText(t,n)).split(/\r?\n/).map(e=>e.trim()).filter(e=>e.length>0&&isSuspiciousSessionCorpusLine(e));if(e.length>0){s+=1;c+=e.length}}});await inspect(`session-ingestion`,`Dreaming session-ingestion state`,async()=>{await accessWorkspacePath(t,i);l=true});if(!l)try{for(const e of SESSION_INGESTION_NAMESPACES)if((await readMemoryCoreWorkspaceEntries({namespace:e,workspaceDir:t})).length>0){l=true;break}}catch{}if(c>0)a.push({severity:`warn`,code:`dreaming-session-corpus-self-ingested`,message:`Dreaming session corpus appears to contain self-ingested narrative content (${c} suspicious line${c===1?``:`s`}).`,fixable:true});return{...n?{dreamsPath:n}:{},sessionCorpusDir:r,sessionCorpusFileCount:o,suspiciousSessionCorpusFileCount:s,suspiciousSessionCorpusLineCount:c,sessionIngestionPath:i,sessionIngestionExists:l,issues:a}}async function repairDreamingArtifacts(e){const t=requireAbsoluteWorkspaceDir(e.workspaceDir);const n=[];const r=[];let i;let a=false;const archivePathIfPresent=a=>withMemoryWorkspaceLock(t,async()=>{try{i??=path.join(t,REPAIR_ARCHIVE_RELATIVE_DIR,(e.now??/* @__PURE__ */ new Date).toISOString().replace(/[:.]/g,`-`));if(!await ensureArchivablePath(t,a))return false;await makeWorkspaceDirectory(t,i);const n=path.join(i,`${path.basename(a)}.${randomUUID()}`);await renameWorkspacePath(t,a,n);r.push(n)}catch(e){n.push(e instanceof Error?e.message:String(e));return false}try{await pruneArchivedShortTermRecallEntries(t,a)}catch(e){n.push(`Archive completed but recall cleanup failed; run openclaw memory status --fix or wait for the next nightly recall repair: ${e instanceof Error?e.message:String(e)}`)}return true});const o=await archivePathIfPresent(path.join(t,SESSION_CORPUS_RELATIVE_DIR));const s=await archivePathIfPresent(path.join(t,SESSION_INGESTION_RELATIVE_PATH));if(o||s)try{await Promise.all(SESSION_INGESTION_NAMESPACES.map(e=>clearMemoryCoreWorkspaceNamespace({namespace:e,workspaceDir:t})))}catch(e){n.push(`Failed clearing dreaming session-ingestion SQLite state: ${e instanceof Error?e.message:String(e)}`)}if(e.archiveDiary){const e=await resolveExistingDreamsPath(t);if(e)a=await archivePathIfPresent(e)}return{changed:a||o||s,...i?{archiveDir:i}:{},archivedDreamsDiary:a,archivedSessionCorpus:o,archivedSessionIngestion:s,archivedPaths:r,warnings:n}}export{repairDreamingArtifacts as n,auditDreamingArtifacts as t};