@@ -17,6 +17,84 @@ process.env.HOME = root;
1717process . env . OPENCLAW_HOME = root ;
1818process . env . OPENCLAW_STATE_DIR = path . join ( root , "state" ) ;
1919
20+ function createSyntheticConfig ( modelCount : number ) : OpenClawConfig {
21+ const makeModels = ( provider : string , baseWindow : number ) =>
22+ Array . from ( { length : modelCount } , ( _ , index ) => ( {
23+ id : index === 0 ? "shared-model" : `${ provider } -model-${ index } ` ,
24+ name : `${ provider } model ${ index } ` ,
25+ reasoning : false ,
26+ input : [ "text" as const ] ,
27+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
28+ contextWindow : baseWindow + ( index % 17 ) ,
29+ maxTokens : 8_192 ,
30+ } ) ) ;
31+ return {
32+ agents : {
33+ defaults : {
34+ workspace : workspaceDir ,
35+ model : { primary : "synthetic-a/shared-model" } ,
36+ models : {
37+ "synthetic-a/shared-model" : { agentRuntime : { id : "openclaw" } } ,
38+ "synthetic-b/shared-model" : { agentRuntime : { id : "openclaw" } } ,
39+ } ,
40+ } ,
41+ } ,
42+ models : {
43+ providers : {
44+ "synthetic-a" : {
45+ baseUrl : "http://127.0.0.1:1/v1" ,
46+ api : "openai-completions" as const ,
47+ models : makeModels ( "synthetic-a" , 128_000 ) ,
48+ } ,
49+ "synthetic-b" : {
50+ baseUrl : "http://127.0.0.1:2/v1" ,
51+ api : "openai-completions" as const ,
52+ models : makeModels ( "synthetic-b" , 64_000 ) ,
53+ } ,
54+ } ,
55+ } ,
56+ } ;
57+ }
58+
59+ async function measureCacheLoad ( load : ( ) => Promise < void > ) {
60+ const heartbeatGaps : number [ ] = [ ] ;
61+ let lastHeartbeatAt = performance . now ( ) ;
62+ const heartbeat = setInterval ( ( ) => {
63+ const now = performance . now ( ) ;
64+ heartbeatGaps . push ( now - lastHeartbeatAt ) ;
65+ lastHeartbeatAt = now ;
66+ } , 20 ) ;
67+ const delayMonitor = monitorEventLoopDelay ( { resolution : 20 } ) ;
68+ delayMonitor . enable ( ) ;
69+ const eluStart = performance . eventLoopUtilization ( ) ;
70+ const immediateSpy = vi . spyOn ( globalThis , "setImmediate" ) ;
71+ const startedAt = performance . now ( ) ;
72+ let completedAt = startedAt ;
73+ let immediateCallCount : number ;
74+ try {
75+ await load ( ) ;
76+ completedAt = performance . now ( ) ;
77+ await new Promise < void > ( ( resolve ) => {
78+ setTimeout ( resolve , 40 ) ;
79+ } ) ;
80+ } finally {
81+ immediateCallCount = immediateSpy . mock . calls . length ;
82+ immediateSpy . mockRestore ( ) ;
83+ delayMonitor . disable ( ) ;
84+ clearInterval ( heartbeat ) ;
85+ }
86+ const elu = performance . eventLoopUtilization ( performance . eventLoopUtilization ( ) , eluStart ) ;
87+ return {
88+ warmMs : completedAt - startedAt ,
89+ maxHeartbeatGapMs : Math . max ( ...heartbeatGaps ) ,
90+ delayMaxMs : delayMonitor . max / 1_000_000 ,
91+ delayP99Ms : delayMonitor . percentile ( 99 ) / 1_000_000 ,
92+ eventLoopUtilization : elu . utilization ,
93+ immediateCallCount,
94+ heartbeatCount : heartbeatGaps . length ,
95+ } ;
96+ }
97+
2098afterAll ( ( ) => {
2199 resetContextWindowCacheForTest ( ) ;
22100 resetPreparedModelRuntimeSnapshotsForTest ( ) ;
@@ -37,42 +115,7 @@ afterAll(() => {
37115describe ( "post-ready context cache prewarm" , ( ) => {
38116 it ( "yields through the real prepared catalog lifecycle and converges atomically" , async ( ) => {
39117 const modelCount = Number . parseInt ( process . env . SYNTHETIC_MODEL_COUNT ?? "20000" , 10 ) ;
40- const makeModels = ( provider : string , baseWindow : number ) =>
41- Array . from ( { length : modelCount } , ( _ , index ) => ( {
42- id : index === 0 ? "shared-model" : `${ provider } -model-${ index } ` ,
43- name : `${ provider } model ${ index } ` ,
44- reasoning : false ,
45- input : [ "text" as const ] ,
46- cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
47- contextWindow : baseWindow + ( index % 17 ) ,
48- maxTokens : 8_192 ,
49- } ) ) ;
50- const config = {
51- agents : {
52- defaults : {
53- workspace : workspaceDir ,
54- model : { primary : "synthetic-a/shared-model" } ,
55- models : {
56- "synthetic-a/shared-model" : { agentRuntime : { id : "openclaw" } } ,
57- "synthetic-b/shared-model" : { agentRuntime : { id : "openclaw" } } ,
58- } ,
59- } ,
60- } ,
61- models : {
62- providers : {
63- "synthetic-a" : {
64- baseUrl : "http://127.0.0.1:1/v1" ,
65- api : "openai-completions" as const ,
66- models : makeModels ( "synthetic-a" , 128_000 ) ,
67- } ,
68- "synthetic-b" : {
69- baseUrl : "http://127.0.0.1:2/v1" ,
70- api : "openai-completions" as const ,
71- models : makeModels ( "synthetic-b" , 64_000 ) ,
72- } ,
73- } ,
74- } ,
75- } satisfies OpenClawConfig ;
118+ const config = createSyntheticConfig ( modelCount ) ;
76119
77120 const runtimeModule = await import ( "./prepared-model-runtime.js" ) ;
78121 const contextModule = await import ( "./context.js" ) ;
@@ -83,54 +126,22 @@ describe("post-ready context cache prewarm", () => {
83126 } ) ;
84127 contextModule . resetContextWindowCacheForTest ( ) ;
85128
86- const heartbeatGaps : number [ ] = [ ] ;
87- let lastHeartbeatAt = performance . now ( ) ;
88- const heartbeat = setInterval ( ( ) => {
89- const now = performance . now ( ) ;
90- heartbeatGaps . push ( now - lastHeartbeatAt ) ;
91- lastHeartbeatAt = now ;
92- } , 20 ) ;
93- const delayMonitor = monitorEventLoopDelay ( { resolution : 20 } ) ;
94- delayMonitor . enable ( ) ;
95- const eluStart = performance . eventLoopUtilization ( ) ;
96- const immediateSpy = vi . spyOn ( globalThis , "setImmediate" ) ;
97- let immediateCallCount : number ;
98- const warmStartedAt = performance . now ( ) ;
99- let warmCompletedAt = warmStartedAt ;
100- try {
101- await contextModule . prewarmContextWindowCacheAfterReady ( {
129+ const metrics = await measureCacheLoad ( ( ) =>
130+ contextModule . prewarmContextWindowCacheAfterReady ( {
102131 config : process . env . SYNTHETIC_CLONE_CONFIG === "0" ? config : structuredClone ( config ) ,
103- } ) ;
104- warmCompletedAt = performance . now ( ) ;
105- await new Promise < void > ( ( resolve ) => {
106- setTimeout ( resolve , 40 ) ;
107- } ) ;
108- } finally {
109- immediateCallCount = immediateSpy . mock . calls . length ;
110- immediateSpy . mockRestore ( ) ;
111- delayMonitor . disable ( ) ;
112- clearInterval ( heartbeat ) ;
113- }
114- const warmMs = warmCompletedAt - warmStartedAt ;
115- const elu = performance . eventLoopUtilization ( performance . eventLoopUtilization ( ) , eluStart ) ;
116- const maxHeartbeatGapMs = Math . max ( ...heartbeatGaps ) ;
117- const delayMaxMs = delayMonitor . max / 1_000_000 ;
132+ } ) ,
133+ ) ;
118134 console . log (
119135 `CONTEXT_PREWARM_GREEN ${ JSON . stringify ( {
120136 modelCount,
121- warmMs,
122- maxHeartbeatGapMs,
123- delayMaxMs,
124- delayP99Ms : delayMonitor . percentile ( 99 ) / 1_000_000 ,
125- eventLoopUtilization : elu . utilization ,
126- immediateCallCount,
137+ ...metrics ,
127138 } ) } `,
128139 ) ;
129140
130- expect ( immediateCallCount ) . toBeGreaterThan ( 0 ) ;
131- expect ( heartbeatGaps . length ) . toBeGreaterThan ( 0 ) ;
132- expect ( maxHeartbeatGapMs ) . toBeLessThan ( 500 ) ;
133- expect ( delayMaxMs ) . toBeLessThan ( 500 ) ;
141+ expect ( metrics . immediateCallCount ) . toBeGreaterThan ( 0 ) ;
142+ expect ( metrics . heartbeatCount ) . toBeGreaterThan ( 0 ) ;
143+ expect ( metrics . maxHeartbeatGapMs ) . toBeLessThan ( 500 ) ;
144+ expect ( metrics . delayMaxMs ) . toBeLessThan ( 500 ) ;
134145 expect (
135146 contextModule . lookupContextTokens ( "shared-model" , {
136147 allowAsyncLoad : false ,
@@ -154,4 +165,46 @@ describe("post-ready context cache prewarm", () => {
154165 } ) ,
155166 ) . toBe ( 64_000 ) ;
156167 } , 30_000 ) ;
168+
169+ it ( "keeps first-request configured projection cooperative through the real catalog owner" , async ( ) => {
170+ const modelCount = Number . parseInt ( process . env . SYNTHETIC_MODEL_COUNT ?? "20000" , 10 ) ;
171+ const config = createSyntheticConfig ( modelCount ) ;
172+ const runtimeModule = await import ( "./prepared-model-runtime.js" ) ;
173+ const contextModule = await import ( "./context.js" ) ;
174+ await runtimeModule . refreshPreparedModelRuntimeSnapshots ( config , {
175+ gatewayLifecycle : true ,
176+ catalogMode : "static" ,
177+ defaultWorkspaceDir : workspaceDir ,
178+ } ) ;
179+ contextModule . resetContextWindowCacheForTest ( ) ;
180+
181+ const lastConfiguredModel = `synthetic-a-model-${ modelCount - 1 } ` ;
182+ const metrics = await measureCacheLoad ( ( ) => {
183+ const loadPromise = contextModule . ensureContextWindowCacheLoaded ( config ) ;
184+ expect (
185+ contextModule . lookupContextTokens ( lastConfiguredModel , {
186+ allowAsyncLoad : false ,
187+ skipRuntimeConfigLoad : true ,
188+ } ) ,
189+ ) . toBeUndefined ( ) ;
190+ return loadPromise ;
191+ } ) ;
192+ console . log (
193+ `CONTEXT_REQUEST_LOAD_GREEN ${ JSON . stringify ( {
194+ modelCount,
195+ ...metrics ,
196+ } ) } `,
197+ ) ;
198+
199+ expect ( metrics . immediateCallCount ) . toBeGreaterThan ( 0 ) ;
200+ expect ( metrics . heartbeatCount ) . toBeGreaterThan ( 0 ) ;
201+ expect ( metrics . maxHeartbeatGapMs ) . toBeLessThan ( 500 ) ;
202+ expect ( metrics . delayMaxMs ) . toBeLessThan ( 500 ) ;
203+ expect (
204+ contextModule . lookupContextTokens ( lastConfiguredModel , {
205+ allowAsyncLoad : false ,
206+ skipRuntimeConfigLoad : true ,
207+ } ) ,
208+ ) . toBe ( 128_000 + ( ( modelCount - 1 ) % 17 ) ) ;
209+ } , 30_000 ) ;
157210} ) ;
0 commit comments