11import { isDeepStrictEqual } from "node:util" ;
22import { uniqueValues } from "@openclaw/normalization-core/string-normalization" ;
3+ import { resolveManagedUnsetPathsForWrite } from "../config/config-path-mutation.js" ;
34import { replaceConfigFile } from "../config/config.js" ;
45import { AUTO_MANAGED_CONFIG_META_PATHS } from "../config/io.meta.js" ;
6+ import { prepareConfigWriteTopology } from "../config/io.write-topology.js" ;
57import { formatConfigIssueLines } from "../config/issue-format.js" ;
68import { ConfigMutationConflictError } from "../config/mutation-conflict.js" ;
79import { resolveConfigPath } from "../config/paths.js" ;
@@ -30,13 +32,16 @@ import {
3032import {
3133 assertNonDestructiveReplacement ,
3234 formatConfigSetPath ,
35+ formatConfigUnsetMissingPathMessage ,
3336 getAtPath ,
3437 mergeAtPath ,
38+ parseConfigSetPath ,
3539 setAtPath ,
3640 type JsonSchemaRecord ,
3741 type PathSegment ,
3842 unsetAtPath ,
3943} from "./config-cli-path.js" ;
44+ import { ConfigMutationAgentRoster } from "./config-cli-roster.js" ;
4045import {
4146 assertStrictConfigForMutation ,
4247 collectDryRunRefs ,
@@ -138,15 +143,6 @@ function formatAutoManagedMetaError(paths: readonly PathSegment[][]): string {
138143 ] . join ( "\n" ) ;
139144}
140145
141- export function assertConfigPathIsNotAutoManaged ( path : PathSegment [ ] ) : void {
142- const targets = findAutoManagedMetaTargets ( [
143- { inputMode : "json" , requestedPath : path , setPath : path , value : undefined , mutation : "delete" } ,
144- ] ) ;
145- if ( targets . length > 0 ) {
146- throw new Error ( formatAutoManagedMetaError ( targets ) ) ;
147- }
148- }
149-
150146function pruneInactiveGatewayAuthCredentials ( params : {
151147 root : Record < string , unknown > ;
152148 operations : ConfigSetOperation [ ] ;
@@ -222,7 +218,7 @@ function expandActualChangedPaths(
222218 return [ ...expanded ] ;
223219}
224220
225- export function configApplyHintForOperations (
221+ function configApplyHintForOperations (
226222 operations : ReadonlyArray < { requestedPath ?: PathSegment [ ] } > ,
227223 beforeConfig : OpenClawConfig ,
228224 afterConfig : OpenClawConfig ,
@@ -303,18 +299,73 @@ export async function runConfigOperations(params: {
303299 structuredClone ( snapshot . resolved ) as OpenClawConfig ,
304300 ) ;
305301 const mutationSchema = await loadMutationSchema ( ) ;
306- const unsetPaths : PathSegment [ ] [ ] = [ ] ;
302+ const roster = new ConfigMutationAgentRoster ( next , snapshot . sourceConfigBeforeMigrations ) ;
303+ let unsetPaths : PathSegment [ ] [ ] = [ ] ;
307304 const explicitSetPaths : PathSegment [ ] [ ] = [ ] ;
305+ const appliedOperations : ConfigSetOperation [ ] = [ ] ;
306+ const recordOperation = ( operation : ConfigSetOperation ) : PathSegment [ ] => {
307+ const setPath = roster . writePath ( operation . setPath ) ;
308+ let touchedSecretTargetPath = operation . touchedSecretTargetPath ;
309+ if ( touchedSecretTargetPath ) {
310+ const path = parseConfigSetPath ( touchedSecretTargetPath ) ;
311+ const writePath = roster . writePath ( path ) ;
312+ if ( writePath !== path ) {
313+ touchedSecretTargetPath = toDotPath ( writePath ) ;
314+ }
315+ }
316+ appliedOperations . push ( {
317+ ...operation ,
318+ setPath,
319+ ...( touchedSecretTargetPath ? { touchedSecretTargetPath } : { } ) ,
320+ } ) ;
321+ return setPath ;
322+ } ;
308323 for ( const operation of operations ) {
324+ const merge =
325+ operation . mutation === "merge" || ( options . merge && operation . mutation !== "replace" ) ;
326+ roster . prepare ( operation , Boolean ( merge ) ) ;
309327 if ( operation . mutation === "delete" ) {
328+ const writePath = recordOperation ( operation ) ;
310329 const unsetResult = unsetAtPath ( next , operation . setPath ) ;
330+ if ( ! unsetResult . removed && operation . inputMode === "unset" ) {
331+ const runtimeOnly = getAtPath ( snapshot . runtimeConfig , operation . setPath ) . found ;
332+ const message = formatConfigUnsetMissingPathMessage ( {
333+ path : formatConfigSetPath ( operation . requestedPath , operation . pathTokens ) ,
334+ runtimeOnly,
335+ } ) ;
336+ if ( options . dryRun && options . json ) {
337+ throw new ConfigSetDryRunValidationError ( {
338+ ok : false ,
339+ operations : 1 ,
340+ configPath : snapshot . path ,
341+ inputModes : [ "unset" ] ,
342+ checks : { schema : false , resolvability : false , resolvabilityComplete : false } ,
343+ refsChecked : 0 ,
344+ skippedExecRefs : 0 ,
345+ errors : [
346+ {
347+ kind : "missing-path" ,
348+ message : runtimeOnly
349+ ? message
350+ : `Config path not found: ${ formatConfigSetPath ( operation . requestedPath , operation . pathTokens ) } . Nothing was changed.` ,
351+ } ,
352+ ] ,
353+ } ) ;
354+ }
355+ if ( ! options . dryRun ) {
356+ assertStrictConfigForMutation (
357+ currentConfig ,
358+ mutationStart . writeOptions . basePluginMetadataSnapshot ,
359+ ) ;
360+ }
361+ throw new Error ( message ) ;
362+ }
311363 if ( ! unsetResult . removed || unsetResult . leafContainer !== "array" ) {
312- unsetPaths . push ( operation . setPath ) ;
364+ unsetPaths . push ( writePath ) ;
313365 }
314366 continue ;
315367 }
316- explicitSetPaths . push ( operation . setPath ) ;
317- if ( operation . mutation === "merge" || ( options . merge && operation . mutation !== "replace" ) ) {
368+ if ( merge ) {
318369 mergeAtPath ( next , operation . setPath , operation . value , {
319370 numericObjectKeys : params . successMode === "patch" ,
320371 ...( operation . pathTokens ? { pathTokens : operation . pathTokens } : { } ) ,
@@ -339,18 +390,33 @@ export async function runConfigOperations(params: {
339390 schema : mutationSchema ,
340391 } ) ;
341392 }
393+ explicitSetPaths . push ( recordOperation ( operation ) ) ;
342394 }
395+ roster . finish ( ) ;
396+ // A later operation can recreate a deleted path, including through a roster alias.
397+ // Only final deletions may be replayed by the persistence owner.
398+ unsetPaths = unsetPaths . filter ( ( path ) => ! getAtPath ( next , path ) . found ) ;
343399 const removedGatewayAuthPaths = pruneInactiveGatewayAuthCredentials ( { root : next , operations } ) ;
344- const nextConfig = normalizeConfigMutationModelRefs ( next as OpenClawConfig ) ;
400+ let nextConfig = normalizeConfigMutationModelRefs ( next as OpenClawConfig ) ;
345401 const normalizedExplicitSetPaths = explicitSetPaths . map ( normalizeConfigMutationExplicitSetPath ) ;
402+ if ( options . dryRun ) {
403+ nextConfig = prepareConfigWriteTopology ( {
404+ snapshot,
405+ pluginMetadataSnapshot : mutationStart . writeOptions . basePluginMetadataSnapshot ,
406+ nextConfig,
407+ options : { explicitSetPaths : normalizedExplicitSetPaths } ,
408+ unsetPaths : resolveManagedUnsetPathsForWrite ( unsetPaths ) ,
409+ env : process . env ,
410+ } ) . nextConfig ;
411+ }
346412 const policyIssueLines = formatConfigIssueLines (
347413 collectUnsupportedSecretRefPolicyIssues ( nextConfig ) ,
348414 "" ,
349415 { normalizeRoot : true } ,
350416 ) . map ( ( line ) => line . trim ( ) ) ;
351417 const pluginIntegrationErrors = collectPluginIntegrationProviderErrors ( {
352418 config : nextConfig ,
353- operations,
419+ operations : appliedOperations ,
354420 } ) ;
355421 if ( options . dryRun ) {
356422 const hasJsonMode = operations . some ( ( { inputMode } ) => inputMode === "json" ) ;
@@ -362,7 +428,9 @@ export async function runConfigOperations(params: {
362428 ( operation . inputMode === "json" && operation . schemaValidated !== true ) ,
363429 ) ;
364430 const checksRefs = hasJsonMode || hasBuilderMode || hasUnsetMode ;
365- const refs = checksRefs ? collectDryRunRefs ( { config : nextConfig , operations } ) : [ ] ;
431+ const refs = checksRefs
432+ ? collectDryRunRefs ( { config : nextConfig , operations : appliedOperations } )
433+ : [ ] ;
366434 const selectedRefs = selectDryRunRefsForResolution ( {
367435 refs,
368436 allowExecInDryRun : Boolean ( options . allowExec ) ,
@@ -371,7 +439,7 @@ export async function runConfigOperations(params: {
371439 const modelRefCheck = await checkTouchedTextModelRefs ( {
372440 config : nextConfig ,
373441 previousConfig : currentConfig ,
374- touchedPaths : operations . map ( ( { setPath } ) => setPath ) ,
442+ touchedPaths : appliedOperations . map ( ( { setPath } ) => setPath ) ,
375443 redactDependencyValues : true ,
376444 } ) ;
377445 errors . push ( ...modelRefCheck . errors . map ( ( message ) => ( { kind : "model" as const , message } ) ) ) ;
@@ -479,7 +547,7 @@ export async function runConfigOperations(params: {
479547 const modelRefCheck = await checkTouchedTextModelRefs ( {
480548 config : nextConfig ,
481549 previousConfig : currentConfig ,
482- touchedPaths : operations . map ( ( { setPath } ) => setPath ) ,
550+ touchedPaths : appliedOperations . map ( ( { setPath } ) => setPath ) ,
483551 redactDependencyValues : true ,
484552 } ) ;
485553 if ( modelRefCheck . errors [ 0 ] ) {
0 commit comments