@@ -69,6 +69,7 @@ const hoisted = vi.hoisted(() => {
6969 } ) ) ;
7070 const ensureOpenClawModelsJson = vi . fn ( async ( ) => { } ) ;
7171 const ensureRuntimePluginsLoaded = vi . fn ( ) ;
72+ const ensureContextWindowCacheLoaded = vi . fn ( async ( ) => { } ) ;
7273 const clearCurrentProviderAuthState = vi . fn ( ) ;
7374 const warmCurrentProviderAuthStateOffMainThread = vi . fn (
7475 async ( _cfg ?: unknown , _options ?: unknown ) => { } ,
@@ -108,6 +109,7 @@ const hoisted = vi.hoisted(() => {
108109 getModelRefStatus,
109110 ensureOpenClawModelsJson,
110111 ensureRuntimePluginsLoaded,
112+ ensureContextWindowCacheLoaded,
111113 clearCurrentProviderAuthState,
112114 warmCurrentProviderAuthStateOffMainThread,
113115 setAuthProfileFailureHook,
@@ -217,6 +219,10 @@ vi.mock("../agents/runtime-plugins.js", () => ({
217219 ensureRuntimePluginsLoaded : hoisted . ensureRuntimePluginsLoaded ,
218220} ) ) ;
219221
222+ vi . mock ( "../agents/context.js" , ( ) => ( {
223+ ensureContextWindowCacheLoaded : hoisted . ensureContextWindowCacheLoaded ,
224+ } ) ) ;
225+
220226vi . mock ( "../agents/model-provider-auth.js" , ( ) => ( {
221227 warmCurrentProviderAuthStateOffMainThread : hoisted . warmCurrentProviderAuthStateOffMainThread ,
222228} ) ) ;
@@ -249,6 +255,7 @@ vi.mock("./server-tailscale.js", () => ({
249255
250256const { startGatewayPostAttachRuntime, startGatewaySidecars, testing } =
251257 await import ( "./server-startup-post-attach.js" ) ;
258+ const { scheduleContextCachePrewarm } = await import ( "./server-startup-context-cache-prewarm.js" ) ;
252259const { STARTUP_UNAVAILABLE_GATEWAY_METHODS } = await import ( "./methods/core-descriptors.js" ) ;
253260const { createGatewayCloseHandler } = await import ( "./server-close.js" ) ;
254261const { createChatRunState } = await import ( "./server-chat-state.js" ) ;
@@ -359,6 +366,8 @@ describe("startGatewayPostAttachRuntime", () => {
359366 hoisted . ensureOpenClawModelsJson . mockReset ( ) ;
360367 hoisted . ensureOpenClawModelsJson . mockResolvedValue ( undefined ) ;
361368 hoisted . ensureRuntimePluginsLoaded . mockReset ( ) ;
369+ hoisted . ensureContextWindowCacheLoaded . mockReset ( ) ;
370+ hoisted . ensureContextWindowCacheLoaded . mockResolvedValue ( undefined ) ;
362371 hoisted . clearCurrentProviderAuthState . mockClear ( ) ;
363372 hoisted . warmCurrentProviderAuthStateOffMainThread . mockReset ( ) ;
364373 hoisted . warmCurrentProviderAuthStateOffMainThread . mockResolvedValue ( undefined ) ;
@@ -1138,7 +1147,7 @@ describe("startGatewayPostAttachRuntime", () => {
11381147 await vi . advanceTimersToNextTimerAsync ( ) ;
11391148 expect ( postReadyRequestTurn ) . toHaveBeenCalledTimes ( 1 ) ;
11401149 expect ( onPostReadySidecars . mock . calls [ 0 ] ?. [ 0 ] ) . toHaveLength ( 0 ) ;
1141- expect ( onGatewayLifetimeSidecars . mock . calls [ 0 ] ?. [ 0 ] ) . toHaveLength ( 2 ) ;
1150+ expect ( onGatewayLifetimeSidecars . mock . calls [ 0 ] ?. [ 0 ] ) . toHaveLength ( 3 ) ;
11421151 await vi . dynamicImportSettled ( ) ;
11431152 await vi . waitFor ( ( ) => {
11441153 expect ( hoisted . setAuthProfileFailureHook ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -1170,7 +1179,7 @@ describe("startGatewayPostAttachRuntime", () => {
11701179 await vi . waitFor ( ( ) => {
11711180 expect ( hoisted . setAuthProfileFailureHook ) . toHaveBeenCalledTimes ( 1 ) ;
11721181 } ) ;
1173- expect ( onGatewayLifetimeSidecars . mock . calls [ 0 ] ?. [ 0 ] ) . toHaveLength ( 2 ) ;
1182+ expect ( onGatewayLifetimeSidecars . mock . calls [ 0 ] ?. [ 0 ] ) . toHaveLength ( 3 ) ;
11741183
11751184 await vi . advanceTimersByTimeAsync ( 10_000 ) ;
11761185 expect ( hoisted . warmCurrentProviderAuthStateOffMainThread ) . not . toHaveBeenCalled ( ) ;
@@ -1220,6 +1229,37 @@ describe("startGatewayPostAttachRuntime", () => {
12201229 expect ( testing . agentRuntimePluginPrewarmStartDelayMs ) . toBe ( 0 ) ;
12211230 } ) ;
12221231
1232+ it ( "defers context-window cache prewarm to a post-ready sidecar" , async ( ) => {
1233+ vi . useFakeTimers ( ) ;
1234+ const cfg = { agents : { defaults : { model : "openai/gpt-5.5" } } } as never ;
1235+ const sidecar = scheduleContextCachePrewarm ( {
1236+ cfgAtStart : cfg ,
1237+ log : { warn : vi . fn ( ) } ,
1238+ } ) ;
1239+
1240+ expect ( hoisted . ensureContextWindowCacheLoaded ) . not . toHaveBeenCalled ( ) ;
1241+ await vi . advanceTimersByTimeAsync ( 4_999 ) ;
1242+ expect ( hoisted . ensureContextWindowCacheLoaded ) . not . toHaveBeenCalled ( ) ;
1243+ await vi . advanceTimersByTimeAsync ( 1 ) ;
1244+ await vi . dynamicImportSettled ( ) ;
1245+ await vi . waitFor ( ( ) => {
1246+ expect ( hoisted . ensureContextWindowCacheLoaded ) . toHaveBeenCalledWith ( cfg ) ;
1247+ } ) ;
1248+ await sidecar . stop ( ) ;
1249+ } ) ;
1250+
1251+ it ( "cancels context-window cache prewarm when the gateway stops first" , async ( ) => {
1252+ vi . useFakeTimers ( ) ;
1253+ const sidecar = scheduleContextCachePrewarm ( {
1254+ cfgAtStart : { } as never ,
1255+ log : { warn : vi . fn ( ) } ,
1256+ } ) ;
1257+
1258+ await sidecar . stop ( ) ;
1259+ await vi . runAllTimersAsync ( ) ;
1260+ expect ( hoisted . ensureContextWindowCacheLoaded ) . not . toHaveBeenCalled ( ) ;
1261+ } ) ;
1262+
12231263 it ( "keeps provider auth prewarm alive when Gmail post-ready sidecars stop" , async ( ) => {
12241264 vi . useFakeTimers ( ) ;
12251265 const onPostReadySidecars = vi . fn ( ) ;
@@ -1263,7 +1303,7 @@ describe("startGatewayPostAttachRuntime", () => {
12631303 | { stop : ( ) => void } [ ]
12641304 | undefined ;
12651305 expect ( gmailSidecars ) . toHaveLength ( 1 ) ;
1266- expect ( lifetimeSidecars ) . toHaveLength ( 2 ) ;
1306+ expect ( lifetimeSidecars ) . toHaveLength ( 3 ) ;
12671307
12681308 for ( const sidecar of gmailSidecars ?? [ ] ) {
12691309 sidecar . stop ( ) ;
@@ -1325,7 +1365,7 @@ describe("startGatewayPostAttachRuntime", () => {
13251365 | Array < { stop : ( ) => Promise < void > | void } >
13261366 | undefined ;
13271367 expect ( gmailSidecars ) . toHaveLength ( 1 ) ;
1328- expect ( lifetimeSidecars ) . toHaveLength ( 2 ) ;
1368+ expect ( lifetimeSidecars ) . toHaveLength ( 3 ) ;
13291369
13301370 await vi . waitFor ( ( ) => {
13311371 expect ( hoisted . transcriptsAutoStartService . start ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -1820,7 +1860,7 @@ describe("startGatewayPostAttachRuntime", () => {
18201860 name : "sidecars.ready" ,
18211861 metrics : [
18221862 [ "loadedPluginCount" , 2 ] ,
1823- [ "postReadySidecarCount" , 1 ] ,
1863+ [ "postReadySidecarCount" , 2 ] ,
18241864 ] ,
18251865 } ) ;
18261866 } ) ;
0 commit comments