UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 6.88 kB
import{r as collectNestedErrorCandidates}from"./error-coercion-B-1Loz4b.mjs";import{i as isSqliteWorkerError,r as hasSqliteWorkerOutcomeUnknown}from"./sqlite-worker-error-27MOaEMJ.mjs";import{c as isSqliteLockError,h as sqliteErrorCode,o as formatSqliteReadOnlyInspectionFailure,s as isSqliteCorruptionError}from"./sql-connection-D2qSU3Um.mjs";import"./sleep-BeLUM6My.mjs";import"./subsystem-BMw5Czn-.mjs";import"./errors-BKYtsKt6.mjs";import"./openclaw-state-db.paths-BAgG4Sc_.mjs";import{n as normalizeAgentId}from"./agent-id-0RKGHEYi.mjs";import{t as formatCliCommand}from"./command-format-D4NTwCWL.mjs";import{a as tryResolveAmbientOwnerAgentId,h as listAgentIds,o as tryResolveLegacyCompatibilityAgentId,y as isReservedSystemAgentId}from"./agent-scope-config-CGe7-g8s.mjs";import"./session-key-B7vzF6rJ.mjs";import{p as AgentDatabaseStartupPublicationError}from"./openclaw-agent-db-resources-Ce80pC0J.mjs";import{ut as isTerminalSqliteIntegrityError}from"./openclaw-state-db-cache-btIGXlEI.mjs";import{o as WorkerTaskError}from"./worker-request-diagnostics-0PPR2fhf.mjs";import"./worker-task-pool-_ul4-uBt.mjs";import{o as resolveSqliteDatabaseFilePaths,r as resolveUpdateRehearsalRoot}from"./update-rehearsal-paths-BO303u2t.mjs";import"./sqlite-database-admission-turn-IZQs_HQ6.mjs";import"./session-row-changes-CbypkYZd.mjs";import{AsyncLocalStorage}from"node:async_hooks";import fsSync from"node:fs"; //!src/cli/quote-cli-arg.ts function quotePowerShellArg(e){return`'${e.replace(/['‘-‛]/gu,`$&$&`)}'`}function quoteCliArg(e){if(/^[A-Za-z0-9_/:=.,@%+-]+$/.test(e))return e;return`'${e.replaceAll(`'`,`'\\''`)}'`} //!src/infra/sqlite-recovery-files.ts function planSqliteRecoveryMoves(e){const t=`.corrupt-${Date.now()}`;for(let n=0;n<100;n+=1){const r=n===0?t:`${t}.${n}`;const i=e.map(e=>({destinationPath:`${e}${r}`,sourcePath:e}));if(i.every(e=>!fsSync.lstatSync(e.destinationPath,{throwIfNoEntry:false})))return i}throw new Error(`Could not choose recovery paths for ${e[0]??`SQLite files`}`)} //!src/infra/state-migrations.agent-owner-guidance.ts function formatAgentDatabaseCorruptionRepairHint(e,t){if(formatSqliteReadOnlyInspectionFailure(t).includes(`creating its private snapshot:`))return;if(!collectNestedErrorCandidates(t).some(e=>isSqliteCorruptionError(e)||[`SQLITE_CORRUPT`,`SQLITE_NOTADB`].includes(sqliteErrorCode(e)??``)||e instanceof Error&&isTerminalSqliteIntegrityError(e)))return;return`Stop the Gateway, back up this database and its SQLite sidecars, then run "${formatCliCommand(`openclaw doctor --session-sqlite recover --session-sqlite-agent ${quoteCliArg(e)}`)}" against the same state/config. Recovery repairs supported index corruption or preserves damaged files with a .corrupt-<timestamp> suffix. Restore a verified backup if you need the previous sessions, then restart the Gateway.`}function formatAgentDatabaseOwnershipRepairHint(e){const t=planSqliteRecoveryMoves(resolveSqliteDatabaseFilePaths(e).filter(e=>fsSync.lstatSync(e,{throwIfNoEntry:false})));t.sort((t,n)=>Number(t.sourcePath===e)-Number(n.sourcePath===e));const n=t.map(({sourcePath:e,destinationPath:t})=>process.platform===`win32`?`Move-Item -LiteralPath ${quotePowerShellArg(e)} -Destination ${quotePowerShellArg(t)} -ErrorAction Stop`:`mv -n -- ${quoteCliArg(e)} ${quoteCliArg(t)}`).join(process.platform===`win32`?`; `:` && `);return`Preserve and inspect this database before accepting a fresh agent. With all OpenClaw processes stopped, the explicit quarantine move is${process.platform===`win32`?` (PowerShell)`:``}:\n${n}\nThen run openclaw doctor --fix and restart the Gateway.`} //!src/state/agent-database-admission.ts const refusalCauses=/* @__PURE__ */ new WeakMap;const preparation=new AsyncLocalStorage;function isAgentDatabasePreparationRetryable(e){return!hasSqliteWorkerOutcomeUnknown(e)&&(isSqliteLockError(e)||isSqliteWorkerError(e,`overloaded`)||e instanceof WorkerTaskError&&e.code===`overloaded`)}function createAgentDatabaseInspectionRefusal(e){const t=resolveUpdateRehearsalRoot(process.env)&&e.reason.includes(`creating its private snapshot:`)&&/(?:code=(?:ENOSPC|EDQUOT)\b|errcode=13\b)/u.test(e.reason);const n=collectNestedErrorCandidates(e.cause).some(e=>e instanceof Error&&e.name===`DataCloneError`);const r=!e.pending&&isSqliteLockError(e.cause);const i=e.cause instanceof AgentDatabaseStartupPublicationError;const a={agentId:e.agentId,paths:e.paths,code:e.pending?`agent-database-inspection-pending`:`agent-database-inspection-failed`,reason:r?`database locked by another process`:e.reason,repairHint:!e.pending&&isAgentDatabasePreparationRetryable(e.cause)?`Wait for competing database work to finish, then restart the Gateway to retry admission. Lock or worker capacity contention does not require database repair.`:t?`Free space in the reported snapshot cache, then retry the update. This candidate snapshot failure does not require repairing the serving database.`:n?`A worker request could not be serialized. Update OpenClaw and restart the Gateway; repeating database migration will not repair this request.`:i?`Sessions remain unavailable because newer config or plugin changes kept replacing this agent's runtime preparation. Restart the Gateway to retry admission. This failure does not require database repair.`:e.pending?`Startup preparation is in progress or waiting for capacity. Retry the request after startup preparation finishes. Pending startup does not indicate a database failure.`:`Sessions remain unavailable. ${formatAgentDatabaseCorruptionRepairHint(e.agentId,e.cause)??`Stop the Gateway, run "openclaw doctor --fix" to inspect and repair this agent database, and restart.`}`};refusalCauses.set(a,e.cause);return a}function createAgentDatabaseHeldRefusal(e,t){return{agentId:e,paths:t,code:`agent-database-held`,reason:`Agent ${e} database is held for explicit restoration.`,repairHint:`Run openclaw doctor --fix for restoration guidance, then restore the preserved store with agents add. Held databases are not live agents.`}}function inspectAgentDatabaseAdmission(e){const t=normalizeAgentId(e.agentId);const n=e.metadata;if(n?.role!==`agent`||!n.agentId||n.agentId===t)return;return{agentId:t,paths:[e.path],embeddedOwnerId:n.agentId,code:`agent-database-ownership-mismatch`,reason:`Refused agent ${t}: database ${e.path} belongs to agent ${n.agentId}; requested agent ${t}.`,repairHint:formatAgentDatabaseOwnershipRepairHint(e.path)}}function canIsolateAgentDatabase(e,t){return listAgentIds(e).includes(t)&&!isReservedSystemAgentId(t)&&t!==tryResolveAmbientOwnerAgentId(e)&&t!==tryResolveLegacyCompatibilityAgentId(e)}const noStartupReadAdmission=()=>false;let isAgentDatabaseStartupReadReady=noStartupReadAdmission;export{isAgentDatabaseStartupReadReady as a,inspectAgentDatabaseAdmission as i,createAgentDatabaseHeldRefusal as n,createAgentDatabaseInspectionRefusal as r,canIsolateAgentDatabase as t};