openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 7.62 kB
JavaScript
import{x as root}from"./fs-safe-Bxzu1j4h.mjs";import{i as isMissingPathError}from"./errno-DUBt6SQQ.mjs";import{l as resolveConfigIncludeWritePath,n as ConfigIncludeError,o as hashConfigIncludeRaw,p as isPathInside}from"./includes-gwK90iIZ.mjs";import"./errors-D37BJTfI.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{i as hashConfigRaw}from"./io.read-helpers-B0XuPBY2.mjs";import{D as rollbackConfigFileWriteIfUnchanged,b as createConfigFileWriteGuard,g as captureConfigFileWritePathProof}from"./write-lock-BXv5IgVt.mjs";import{t as ConfigMutationConflictError}from"./mutation-conflict-qTMp0e4U.mjs";import{r as createConfigWriteAuthorityGuard}from"./write-authority-YzEjC_aZ.mjs";import{g as withDeferredPluginMigrationsCurrent,h as withDeferredPluginConfigRollback}from"./deferred-plugin-migrations-D_BIDN9_.mjs";import{r as warnIfJSON5CommentsWillBeStripped}from"./io.warnings-Dshur5lA.mjs";import{_ as recordUpdateDoctorConfigFileWrite}from"./update-doctor-result-B09W3zwf.mjs";import{s as recoverConfigWriteFailure}from"./io.write-errors-CQyW8lY6.mjs";import{n as prepareConfigFileWrite}from"./backup-rotation-BmNg1h15.mjs";import{t as preparePluginCohortConfigPublication}from"./plugin-cohort-publication-B9Gj1sI1.mjs";import{t as rejectConfigNonFiniteNumbers}from"./value-tree-BXoqoxTN.mjs";import fs from"node:fs";import path from"node:path";import fs$1 from"node:fs/promises";
//!src/config/mutate.include-io.ts
function formatJsonFileValue(e){rejectConfigNonFiniteNumbers(e);return`${JSON.stringify(e,null,2)}\n`}async function resolveRootBoundIncludeFile(e){const t=resolveConfigIncludeWritePath(e);const n=[path.dirname(e.configPath),...e.allowedRoots];for(const e of n){const n=await fs$1.realpath(e).catch(()=>null);if(!n||!isPathInside(n,t))continue;const r=path.relative(n,t);if(!r||path.isAbsolute(r)||r.split(path.sep)[0]===`..`)continue;return{absolutePath:t,relativePath:r,root:await root(n,{hardlinks:`reject`,mkdir:true,mode:384,symlinks:`reject`})}}throw new Error(`Config include write path has no approved existing root: ${t}`)}async function resolveExpectedRootBoundIncludeFile(e){let t;try{t=await resolveRootBoundIncludeFile(e)}catch(e){if(e instanceof ConfigIncludeError||e instanceof Error&&e.message.startsWith(`Config include write path has no approved existing root:`))throw new ConfigMutationConflictError(`included config target changed since last load`);throw e}if(path.normalize(t.absolutePath)!==path.normalize(e.expectedAbsolutePath))throw new ConfigMutationConflictError(`included config target changed since last load`);return t}async function readRootBoundFileRawIfExists(e){try{return await e.root.readText(e.relativePath)}catch(e){if(isMissingPathError(e))return null;throw e}}async function assertRootConfigStillMatchesSnapshot(e){let t=null;try{t=await fs$1.readFile(e.path,`utf-8`)}catch(e){if(!isMissingPathError(e))throw e}if(hashConfigIncludeRaw(t)!==hashConfigIncludeRaw(e.exists?e.raw??null:null))throw new ConfigMutationConflictError(`config changed while preparing include write`)}async function assertIncludeGraphStillMatchesSnapshot(e){await assertRootConfigStillMatchesSnapshot(e.snapshot);for(const[t,n]of Object.entries(e.writeOptions?.includeFileHashesForWrite??{})){const r=e.writeOptions?.includeFileTargetsForWrite?.[t];if(!r)throw new ConfigMutationConflictError(`included config target changed since last load`);const i=await resolveExpectedRootBoundIncludeFile({configPath:e.snapshot.path,includePath:t,allowedRoots:[path.dirname(r)],expectedAbsolutePath:r});const a=t===e.includePath?e.includeHash:n;if(hashConfigIncludeRaw(await readRootBoundFileRawIfExists(i))!==a)throw new ConfigMutationConflictError(`included config changed while preparing write`)}}async function rollbackJsonFileWriteIfUnchanged(e){const t=await rollbackConfigFileWriteIfUnchanged({configPath:e.target.absolutePath,previousSnapshot:{path:e.target.absolutePath,exists:e.previousRaw!==null,raw:e.previousRaw},committedHash:hashConfigRaw(e.committedRaw),fsModule:fs,...e.pathProof.captureRollbackProof(),preserveDirectoryMode:true,durable:true,destinationHardlinks:`reject`,withPublication:e.pathProof.withPublication});if(t)recordUpdateDoctorConfigFileWrite(e.target.absolutePath,hashConfigRaw(e.committedRaw),hashConfigRaw(e.previousRaw));return t}async function writeRootBoundJsonFile(e){e.assertConfigPathForWrite();await e.preCommitRuntimePreflight?.();e.assertConfigPathForWrite();const t=await resolveExpectedRootBoundIncludeFile({configPath:e.configPath,includePath:e.includePath,allowedRoots:e.allowedRoots,expectedAbsolutePath:e.expectedTargetPath});e.assertConfigPathForWrite();await e.assertIncludeGraphForWrite();e.assertConfigPathForWrite();const n=await readRootBoundFileRawIfExists(t);e.assertConfigPathForWrite();if(hashConfigIncludeRaw(n)!==hashConfigIncludeRaw(e.expectedRaw))throw new ConfigMutationConflictError(`included config changed while preparing write`);const r=captureConfigFileWritePathProof(e.includePath,t.absolutePath,fs);const i=createConfigWriteAuthorityGuard(()=>{e.assertConfigPathForWrite();r.assertCurrent()});const a=formatJsonFileValue(e.value);e.assertConfigPathForWrite();warnIfJSON5CommentsWillBeStripped({raw:n,filePath:t.absolutePath,skipOutputLogs:e.skipOutputLogs});const o={phase:`unpublished`};const s=createConfigFileWriteGuard(t.absolutePath,fs,i,{snapshot:{path:t.absolutePath,exists:n!==null,raw:n},includeGraph:e.includeGraph,validateUtf8:true,targetPathProof:r,preserveDirectoryMode:true,onRootRemoved:()=>{o.phase=`removed`},onRootPublished:()=>{o.phase=`published`}});const c={...r,assertCurrent:()=>{e.assertOwnerForRollback();s.assertPublishedIdentity()},captureRollbackProof:()=>s.captureRollbackProof(e.assertOwnerForRollback),withPublication:(t,n)=>e.databaseAccess===`none`?t():withDeferredPluginConfigRollback({configPath:e.configPath,env:e.env,assertCurrent:e.assertOwnerForRollback},t,n)};try{try{var l=_usingCtx();const i=l.a(await prepareConfigFileWrite({configPath:t.absolutePath,previousRaw:n,content:a,warn:t=>{if(!e.skipOutputLogs)console.warn(t)},fsModule:s.fileSystem,assertCurrent:s.assertCurrent,assertBeforeMutation:s.assertBeforeMutation,onDestinationState:s.onDestinationState,destinationHardlinks:`reject`,durable:true}));await e.beforeCommit?.();preparePluginCohortConfigPublication({rootConfigPath:e.configPath,filePath:e.includePath,targetPath:t.absolutePath,pathSnapshot:r.snapshot,beforeRaw:n,afterRaw:a,...e.includeBoundaryPath?{include:{boundaryPath:[...e.includeBoundaryPath],includePath:e.includePath}}:{}},s.assertCurrent);s.assertCurrent();const publish=()=>{e.assertBeforePublish?.();i.publish();o.phase=`published`;recordUpdateDoctorConfigFileWrite(t.absolutePath,hashConfigRaw(n),hashConfigRaw(a))};if(e.databaseAccess===`none`)publish();else withDeferredPluginMigrationsCurrent({env:e.env,configPath:e.configPath,expectedPending:e.deferredPluginMigrations,phase:`config include publication`},publish);await e.assertIncludeGraphForWrite(hashConfigIncludeRaw(a));s.assertCurrent();s.assertPublishedIdentity()}catch(e){l.e=e}finally{await l.d()}}catch(e){if(o.phase===`unpublished`)throw e;return await recoverConfigWriteFailure({configPath:t.absolutePath,publication:o.phase===`removed`?`partial`:`complete`,cause:e,restoreFile:()=>rollbackJsonFileWriteIfUnchanged({target:t,previousRaw:n,committedRaw:o.phase===`published`?a:null,pathProof:c})})}return c}export{rollbackJsonFileWriteIfUnchanged as a,resolveExpectedRootBoundIncludeFile as i,formatJsonFileValue as n,writeRootBoundJsonFile as o,readRootBoundFileRawIfExists as r,assertIncludeGraphStillMatchesSnapshot as t};