@@ -87,6 +87,23 @@ function applyConfigSchema(state: ConfigState, res: ConfigSchemaResponse) {
8787 state . configSchemaVersion = res . version ?? null ;
8888}
8989
90+ function asConfigRecord ( value : unknown ) : Record < string , unknown > | null {
91+ if ( ! value || typeof value !== "object" || Array . isArray ( value ) ) {
92+ return null ;
93+ }
94+ return value as Record < string , unknown > ;
95+ }
96+
97+ function resolveEditableSnapshotConfig (
98+ snapshot : ConfigSnapshot | null | undefined ,
99+ ) : Record < string , unknown > | null {
100+ return (
101+ asConfigRecord ( snapshot ?. sourceConfig ) ??
102+ asConfigRecord ( snapshot ?. resolved ) ??
103+ asConfigRecord ( snapshot ?. config )
104+ ) ;
105+ }
106+
90107export function applyConfigSnapshot (
91108 state : ConfigState ,
92109 snapshot : ConfigSnapshot ,
@@ -95,15 +112,16 @@ export function applyConfigSnapshot(
95112 const preservePendingChanges = state . configFormDirty && options . discardPendingChanges !== true ;
96113 const draftBaseHash = state . configDraftBaseHash ?? state . configSnapshot ?. hash ?? null ;
97114 state . configSnapshot = snapshot ;
115+ const editableConfig = resolveEditableSnapshotConfig ( snapshot ) ;
98116 const rawAvailable = typeof snapshot . raw === "string" ;
99117 if ( ! rawAvailable && state . configFormMode === "raw" ) {
100118 state . configFormMode = "form" ;
101119 }
102120 const rawFromSnapshot : string =
103121 typeof snapshot . raw === "string"
104122 ? snapshot . raw
105- : snapshot . config && typeof snapshot . config === "object"
106- ? serializeConfigForm ( snapshot . config )
123+ : editableConfig
124+ ? serializeConfigForm ( editableConfig )
107125 : state . configRaw ;
108126 if ( ! preservePendingChanges || state . configFormMode === "raw" ) {
109127 state . configRaw = rawFromSnapshot ;
@@ -116,8 +134,8 @@ export function applyConfigSnapshot(
116134 state . configIssues = Array . isArray ( snapshot . issues ) ? snapshot . issues : [ ] ;
117135
118136 if ( ! preservePendingChanges ) {
119- state . configForm = cloneConfigObject ( snapshot . config ?? { } ) ;
120- state . configFormOriginal = cloneConfigObject ( snapshot . config ?? { } ) ;
137+ state . configForm = cloneConfigObject ( editableConfig ?? { } ) ;
138+ state . configFormOriginal = cloneConfigObject ( editableConfig ?? { } ) ;
121139 state . configRawOriginal = rawFromSnapshot ;
122140 state . configFormDirty = false ;
123141 state . configDraftBaseHash = snapshot . hash ?? null ;
@@ -232,7 +250,7 @@ async function submitConfigChange(
232250
233251function syncConfigDraft ( state : ConfigState , nextForm : Record < string , unknown > ) {
234252 const original = cloneConfigObject (
235- state . configFormOriginal ?? state . configSnapshot ?. config ?? { } ,
253+ state . configFormOriginal ?? resolveEditableSnapshotConfig ( state . configSnapshot ) ?? { } ,
236254 ) ;
237255 const nextRaw = serializeConfigForm ( nextForm ) ;
238256 const originalRaw = serializeConfigForm ( original ) ;
@@ -284,7 +302,9 @@ export async function runUpdate(state: ConfigState) {
284302}
285303
286304function mutateConfigForm ( state : ConfigState , mutate : ( draft : Record < string , unknown > ) => void ) {
287- const base = cloneConfigObject ( state . configForm ?? state . configSnapshot ?. config ?? { } ) ;
305+ const base = cloneConfigObject (
306+ state . configForm ?? resolveEditableSnapshotConfig ( state . configSnapshot ) ?? { } ,
307+ ) ;
288308 mutate ( base ) ;
289309 syncConfigDraft ( state , base ) ;
290310}
@@ -374,12 +394,7 @@ export function updateConfigFormValue(
374394}
375395
376396export function stageConfigPreset ( state : ConfigState , patch : Record < string , unknown > ) {
377- const snapshotConfig =
378- state . configSnapshot ?. config &&
379- typeof state . configSnapshot . config === "object" &&
380- ! Array . isArray ( state . configSnapshot . config )
381- ? state . configSnapshot . config
382- : null ;
397+ const snapshotConfig = resolveEditableSnapshotConfig ( state . configSnapshot ) ;
383398 const baseSource = state . configForm ?? snapshotConfig ;
384399 if ( ! baseSource || ( ! state . configForm && ! state . configSnapshot ?. hash ) ) {
385400 return ;
@@ -393,12 +408,11 @@ export function stageConfigPreset(state: ConfigState, patch: Record<string, unkn
393408}
394409
395410export function resetConfigPendingChanges ( state : ConfigState ) {
396- state . configForm = cloneConfigObject (
397- state . configFormOriginal ?? state . configSnapshot ?. config ?? { } ,
398- ) ;
411+ const editableConfig = resolveEditableSnapshotConfig ( state . configSnapshot ) ;
412+ state . configForm = cloneConfigObject ( state . configFormOriginal ?? editableConfig ?? { } ) ;
399413 state . configRaw =
400414 state . configRawOriginal ??
401- serializeConfigForm ( state . configFormOriginal ?? state . configSnapshot ?. config ?? { } ) ;
415+ serializeConfigForm ( state . configFormOriginal ?? editableConfig ?? { } ) ;
402416 state . configFormDirty = false ;
403417 state . configDraftBaseHash = state . configSnapshot ?. hash ?? null ;
404418 autoAllowlistedPluginIdsByState . delete ( state ) ;
@@ -434,8 +448,7 @@ export function ensureAgentConfigEntry(state: ConfigState, agentId: string): num
434448 if ( ! normalizedAgentId ) {
435449 return - 1 ;
436450 }
437- const source =
438- state . configForm ?? ( state . configSnapshot ?. config as Record < string , unknown > | null ) ;
451+ const source = state . configForm ?? resolveEditableSnapshotConfig ( state . configSnapshot ) ;
439452 const existingIndex = findAgentConfigEntryIndex ( source , normalizedAgentId ) ;
440453 if ( existingIndex >= 0 ) {
441454 return existingIndex ;
@@ -451,8 +464,7 @@ export function stageDefaultAgentConfigEntry(state: ConfigState, agentId: string
451464 if ( ! normalizedAgentId ) {
452465 return false ;
453466 }
454- const source =
455- state . configForm ?? ( state . configSnapshot ?. config as Record < string , unknown > | null ) ;
467+ const source = state . configForm ?? resolveEditableSnapshotConfig ( state . configSnapshot ) ;
456468 const targetIndex = findAgentConfigEntryIndex ( source , normalizedAgentId ) ;
457469 if ( targetIndex < 0 ) {
458470 return false ;
0 commit comments