@@ -707,6 +707,7 @@ async function submitConfigChange(
707707 busyKey : ConfigSubmitBusyKey ,
708708 extraParams : Record < string , unknown > = { } ,
709709 onSubmitted ?: ( info : { raw : string ; ackHash : string | null } ) => void ,
710+ canDispatch : ( ) => boolean = ( ) => true ,
710711) : Promise < boolean > {
711712 const client = state . client ;
712713 if ( ! client || ! state . connected ) {
@@ -737,6 +738,9 @@ async function submitConfigChange(
737738 state . lastError = "Config hash missing; reload and retry." ;
738739 return false ;
739740 }
741+ if ( ! isCurrent ( ) || ! canDispatch ( ) ) {
742+ return false ;
743+ }
740744 // Dispatch-phase report (ackHash null): if the connection dies before the
741745 // ack arrives, reconnect reconciliation still needs the submitted bytes
742746 // to recognize its own committed write. The post-ack report below
@@ -812,12 +816,16 @@ function teardownFlushConfigDraft(
812816 state : ConfigState ,
813817 client : GatewayBrowserClient ,
814818 baseHash : string ,
819+ canDispatch : ( ) => boolean ,
815820) : void {
816821 // Must stay synchronous: page unload destroys the context before any
817822 // deferred work runs. If a JSON5 original parse is still pending, sanitize
818823 // passes placeholders through; the gateway restores restorable sentinels
819824 // (restoreRedactedValues) and rejects unrestorable ones, so the worst case
820825 // matches not flushing at all while the common case saves the draft.
826+ if ( ! canDispatch ( ) ) {
827+ return ;
828+ }
821829 const raw = serializeFormForSubmit ( state ) ;
822830 void client . request ( "config.set" , { raw, baseHash } ) . catch ( ( ) => undefined ) ;
823831}
@@ -832,6 +840,7 @@ function teardownFlushConfigDraft(
832840async function autoSaveConfig (
833841 state : ConfigState ,
834842 onAck ?: ( ackHash : string | null ) => void ,
843+ canDispatch : ( ) => boolean = ( ) => true ,
835844) : Promise < boolean > {
836845 const client = state . client ;
837846 if ( ! client || ! state . connected || ! state . configFormDirty || state . configFormMode !== "form" ) {
@@ -856,6 +865,9 @@ async function autoSaveConfig(
856865 state . lastError = "Config hash missing; reload and retry." ;
857866 return false ;
858867 }
868+ if ( ! isCurrent ( ) || ! canDispatch ( ) ) {
869+ return false ;
870+ }
859871 state . configAutoSaveStatus = "saving" ;
860872 state . lastError = null ;
861873 state . chatError = null ;
@@ -941,14 +953,22 @@ function resetStaleAutoSaveStatus(state: ConfigState) {
941953async function saveConfig (
942954 state : ConfigState ,
943955 onSubmitted ?: ( info : { raw : string ; ackHash : string | null } ) => void ,
956+ canDispatch ?: ( ) => boolean ,
944957) : Promise < boolean > {
945- return submitConfigChange ( state , "config.set" , "configSaving" , { } , onSubmitted ) ;
958+ return submitConfigChange ( state , "config.set" , "configSaving" , { } , onSubmitted , canDispatch ) ;
946959}
947960
948- async function applyConfig ( state : ConfigState ) : Promise < boolean > {
949- return submitConfigChange ( state , "config.apply" , "configApplying" , {
950- sessionKey : state . applySessionKey ,
951- } ) ;
961+ async function applyConfig ( state : ConfigState , canDispatch ?: ( ) => boolean ) : Promise < boolean > {
962+ return submitConfigChange (
963+ state ,
964+ "config.apply" ,
965+ "configApplying" ,
966+ {
967+ sessionKey : state . applySessionKey ,
968+ } ,
969+ undefined ,
970+ canDispatch ,
971+ ) ;
952972}
953973
954974async function patchConfig (
@@ -1514,9 +1534,13 @@ export function createRuntimeConfigCapability(
15141534 lastFlightSubmittedRaw = serializeFormForSubmit ( state ) ;
15151535 lastFlightAckHash = null ;
15161536 const flight = run ( ( ) =>
1517- autoSaveConfig ( state , ( ackHash ) => {
1518- lastFlightAckHash = ackHash ;
1519- } ) ,
1537+ autoSaveConfig (
1538+ state ,
1539+ ( ackHash ) => {
1540+ lastFlightAckHash = ackHash ;
1541+ } ,
1542+ ( ) => canCallConfigMethod ( "config.set" ) ,
1543+ ) ,
15201544 )
15211545 . catch ( ( ) => false )
15221546 . then ( ( saved ) => {
@@ -1990,9 +2014,13 @@ export function createRuntimeConfigCapability(
19902014 async ( ) => {
19912015 cancelAppliedRefresh ( ) ;
19922016 try {
1993- return await saveConfig ( state , ( info ) => {
1994- manualFlightInfo = info ;
1995- } ) ;
2017+ return await saveConfig (
2018+ state ,
2019+ ( info ) => {
2020+ manualFlightInfo = info ;
2021+ } ,
2022+ ( ) => canCallConfigMethod ( "config.set" ) ,
2023+ ) ;
19962024 } finally {
19972025 reconcileAppliedRefresh ( ) ;
19982026 }
@@ -2017,7 +2045,7 @@ export function createRuntimeConfigCapability(
20172045 return false ;
20182046 }
20192047 try {
2020- return await applyConfig ( state ) ;
2048+ return await applyConfig ( state , ( ) => canCallConfigMethod ( "config.apply" ) ) ;
20212049 } finally {
20222050 reconcileAppliedRefresh ( ) ;
20232051 }
@@ -2223,11 +2251,13 @@ export function createRuntimeConfigCapability(
22232251 // pre-save value reads configFormDirty=false while the persisted
22242252 // bytes are still the unreverted submission.
22252253 if ( ackHash && submittedRaw !== null && serializeFormForSubmit ( state ) !== submittedRaw ) {
2226- teardownFlushConfigDraft ( state , client , ackHash ) ;
2254+ teardownFlushConfigDraft ( state , client , ackHash , ( ) =>
2255+ canCallConfigMethod ( "config.set" ) ,
2256+ ) ;
22272257 }
22282258 } ) ;
22292259 } else if ( canFlush && state . configFormDirty ) {
2230- void autoSaveConfig ( state ) ;
2260+ void autoSaveConfig ( state , undefined , ( ) => canCallConfigMethod ( "config.set" ) ) ;
22312261 }
22322262 invalidateConfigConnection ( state ) ;
22332263 state . connected = false ;
0 commit comments