openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
13 lines • 8.52 kB
JavaScript
import{i as resolveGlobalSingleton,r as resolveGlobalSet}from"./global-singleton-CTnqrmaI.js";import{g as normalizeAgentId}from"./runtime-doctor-migrations-D8alsOFs.js";import{u as normalizeProfileName}from"./account-core-34NfXidI.js";import{B as collectNestedErrorCandidates}from"./subsystem-ovCC_dQD.js";import{x as AgentDatabaseStartupPublicationError}from"./sqlite-readonly-worker-6BW7upAM.js";import{a as tryResolveLegacyCompatibilityAgentId,i as tryResolveAmbientOwnerAgentId,m as listAgentIds,v as isReservedSystemAgentId}from"./agent-scope-config-DaUbqOtM.js";import{ut as isTerminalSqliteIntegrityError,vt as resolveUpdateRehearsalRoot,xt as resolveSqliteDatabaseFilePaths}from"./openclaw-state-db-cache-BDxC5KuM.js";import"./openclaw-agent-db.paths-D0bfBkSQ.js";import{Ct as isSqliteCorruptionError,St as formatSqliteReadOnlyInspectionFailure,Ut as hasSqliteWorkerOutcomeUnknown,Wt as isSqliteWorkerError,jt as sqliteErrorCode,wt as isSqliteLockError}from"./sqlite-worker-request-class-GkmU-e0P.js";import"./openclaw-state-db-async-lifecycle-qylDsiRN.js";import"./errors-DKa3Vi4q.js";import{h as WorkerTaskError}from"./worker-request-diagnostics-0mUBI4hH.js";import"./sqlite-database-admission-turn-BDa8xspJ.js";import"./logger-file-worker-DsGWGQt0.js";import fs from"node:fs";import{AsyncLocalStorage}from"node:async_hooks";
//!src/cli/command-format.ts
const CLI_PREFIX_RE=/^(?:(?:pnpm|npm|bunx|npx)\s+)?openclaw(?=\s|$)/;const CONTAINER_FLAG_RE=/(?:^|\s)--container(?:\s|=|$)/;const PROFILE_FLAG_RE=/(?:^|\s)--profile(?:\s|=|$)/;const DEV_FLAG_RE=/(?:^|\s)--dev(?:\s|$)/;const UPDATE_RE=/^(?:\s+--(?:dev|no-color|(?:profile|log-level)[=\s]+\S+))*\s+update(?:\s|$)/;const CONTAINER_HINT_RE=/^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,127}$/;function formatCliCommand(e,t=process.env){const n=t.OPENCLAW_CONTAINER_HINT?.trim();const r=n&&CONTAINER_HINT_RE.test(n)?n:void 0;const i=normalizeProfileName(t.OPENCLAW_PROFILE);if(!r&&!i)return e;if(!CLI_PREFIX_RE.test(e))return e;let a;if(r&&!CONTAINER_FLAG_RE.test(e)&&!UPDATE_RE.test(e.replace(CLI_PREFIX_RE,``)))a=`--container ${r}`;if(!r&&i&&!PROFILE_FLAG_RE.test(e)&&!DEV_FLAG_RE.test(e))a=`--profile ${i}`;return a?e.replace(CLI_PREFIX_RE,e=>`${e} ${a}`):e}
//!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=>!fs.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=>fs.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/sessions/session-row-changes.ts
const listeners=resolveGlobalSet(Symbol.for(`openclaw.sessionRowChanges`),`close-and-restart`);const factListeners=resolveGlobalSet(Symbol.for(`openclaw.sessionRowFactChanges`),`close-and-restart`);const projectionListeners=resolveGlobalSet(Symbol.for(`openclaw.sessionRowProjectionChanges`),`close-and-restart`);const invalidationSources=resolveGlobalSingleton(Symbol.for(`openclaw.sessionRowInvalidationSources`),()=>/* @__PURE__ */ new WeakMap);const privateFacts=resolveGlobalSingleton(Symbol.for(`openclaw.sessionRowPrivateFacts`),()=>/* @__PURE__ */ new WeakMap);const captures=resolveGlobalSingleton(Symbol.for(`openclaw.sessionRowChangeCaptures`),()=>/* @__PURE__ */ new WeakMap);const rowRevision=resolveGlobalSingleton(Symbol.for(`openclaw.sessionRowChangeRevision`),()=>({value:0}));
//!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,formatCliCommand as o,createAgentDatabaseInspectionRefusal as r,canIsolateAgentDatabase as t};