@@ -4,9 +4,12 @@ import {
44 acquireAgentRunPreparedModelRuntime ,
55 acquireReadOnlyPreparedModelRuntime ,
66 activateStandalonePreparedModelRuntime ,
7+ getPreparedModelRuntimeSnapshot ,
78 markPreparedModelRuntimeSnapshotsStale ,
89 prepareModelRuntimeSnapshot ,
910 publishPreparedModelRuntimeSnapshot ,
11+ rejectPendingPreparedModelRuntimeReplacement ,
12+ registerPreparedModelRuntimePublicationListener ,
1013 refreshPreparedModelRuntimeSnapshots ,
1114} from "./prepared-model-runtime.js" ;
1215import {
@@ -33,6 +36,100 @@ describe("prepared model runtime snapshots", () => {
3336 expect ( mocks . ensureOpenClawModelsJson ) . not . toHaveBeenCalled ( ) ;
3437 } ) ;
3538
39+ it ( "publishes invalidation before the replacement generation" , async ( ) => {
40+ mocks . configuredAgentIds = [ "default" ] ;
41+ const events : string [ ] = [ ] ;
42+ const unregister = registerPreparedModelRuntimePublicationListener ( ( event ) => {
43+ events . push ( event . phase ) ;
44+ } ) ;
45+
46+ await refreshPreparedModelRuntimeSnapshots ( { } , { gatewayLifecycle : true } ) ;
47+ unregister ( ) ;
48+ await refreshPreparedModelRuntimeSnapshots ( { } ) ;
49+
50+ expect ( events ) . toEqual ( [ "invalidated" , "published" ] ) ;
51+ } ) ;
52+
53+ it ( "makes the replacement owner readable before announcing publication" , async ( ) => {
54+ mocks . configuredAgentIds = [ "default" ] ;
55+ const initialConfig = { } ;
56+ const replacementConfig = { agents : { defaults : { model : "openai/gpt-5.5" } } } ;
57+ await refreshPreparedModelRuntimeSnapshots ( initialConfig , { gatewayLifecycle : true } ) ;
58+ let publishedOwner : ReturnType < typeof getPreparedModelRuntimeSnapshot > ;
59+ const unregister = registerPreparedModelRuntimePublicationListener ( ( event ) => {
60+ if ( event . phase === "published" ) {
61+ publishedOwner = getPreparedModelRuntimeSnapshot ( {
62+ agentId : "default" ,
63+ agentDir : "/tmp/unused-agent" ,
64+ inheritedAuthDir : "/tmp/unused-agent" ,
65+ config : replacementConfig ,
66+ } ) ;
67+ }
68+ } ) ;
69+
70+ await refreshPreparedModelRuntimeSnapshots ( replacementConfig ) ;
71+ unregister ( ) ;
72+
73+ expect ( publishedOwner ) . toMatchObject ( { config : replacementConfig } ) ;
74+ } ) ;
75+
76+ it ( "announces invalidation from the direct stale owner boundary" , async ( ) => {
77+ mocks . configuredAgentIds = [ "default" ] ;
78+ await refreshPreparedModelRuntimeSnapshots ( { } , { gatewayLifecycle : true } ) ;
79+ const events : string [ ] = [ ] ;
80+ const unregister = registerPreparedModelRuntimePublicationListener ( ( event ) => {
81+ events . push ( event . phase ) ;
82+ } ) ;
83+
84+ markPreparedModelRuntimeSnapshotsStale ( "test direct reload stale edge" , {
85+ waitForReplacement : true ,
86+ } ) ;
87+ unregister ( ) ;
88+
89+ expect ( events ) . toEqual ( [ "invalidated" ] ) ;
90+ } ) ;
91+
92+ it ( "terminates direct invalidation when no replacement owns it" , async ( ) => {
93+ mocks . configuredAgentIds = [ "default" ] ;
94+ await refreshPreparedModelRuntimeSnapshots ( { } , { gatewayLifecycle : true } ) ;
95+ const events : Array < { phase : string ; error ?: Error } > = [ ] ;
96+ const unregister = registerPreparedModelRuntimePublicationListener ( ( event ) => {
97+ events . push ( event ) ;
98+ } ) ;
99+
100+ markPreparedModelRuntimeSnapshotsStale ( "direct invalidation has no replacement" ) ;
101+ unregister ( ) ;
102+
103+ expect ( events ) . toEqual ( [
104+ { phase : "invalidated" } ,
105+ {
106+ phase : "failed" ,
107+ error : expect . objectContaining ( { message : "direct invalidation has no replacement" } ) ,
108+ } ,
109+ ] ) ;
110+ } ) ;
111+
112+ it ( "announces a failed replacement so lifecycle readers do not wait indefinitely" , async ( ) => {
113+ mocks . configuredAgentIds = [ "default" ] ;
114+ await refreshPreparedModelRuntimeSnapshots ( { } , { gatewayLifecycle : true } ) ;
115+ const events : Array < { phase : string ; error ?: Error } > = [ ] ;
116+ const unregister = registerPreparedModelRuntimePublicationListener ( ( event ) => {
117+ events . push ( event ) ;
118+ } ) ;
119+ const replacementError = new Error ( "replacement aborted" ) ;
120+
121+ const gateId = markPreparedModelRuntimeSnapshotsStale ( "test failed reload" , {
122+ waitForReplacement : true ,
123+ } ) ;
124+ rejectPendingPreparedModelRuntimeReplacement ( gateId , replacementError ) ;
125+ unregister ( ) ;
126+
127+ expect ( events ) . toEqual ( [
128+ { phase : "invalidated" } ,
129+ { phase : "failed" , error : replacementError } ,
130+ ] ) ;
131+ } ) ;
132+
36133 it ( "does not let a read-only draft replace a configured gateway owner" , async ( ) => {
37134 mocks . configuredAgentIds = [ "default" ] ;
38135 const configured = { agents : { defaults : { model : "openai/gpt-5.5" } } } ;
0 commit comments