@@ -44,15 +44,35 @@ describe('estimateMaxTokensForZodSchema', () => {
4444 expect ( nestedBudget ) . toBeGreaterThanOrEqual ( 1024 ) ;
4545 } ) ;
4646
47- it ( 'clamps to the maximum (8192) for very large schemas' , ( ) => {
47+ it ( 'sizes the budget from a string field\'s .max() so large-output schemas do not truncate' , ( ) => {
48+ // Regression: z.string() was treated as a flat ~30-token leaf regardless
49+ // of .max(), so a `source: z.string().max(80_000)` field (designed for
50+ // ~20K tokens of generated code) auto-estimated to the 512 floor and the
51+ // provider truncated the JSON mid-string. The estimate must scale with the
52+ // declared max length.
53+ const big = z . object ( {
54+ source : z . string ( ) . max ( 80_000 ) ,
55+ rationale : z . string ( ) . max ( 500 ) ,
56+ } ) ;
57+ const small = z . object ( {
58+ source : z . string ( ) . max ( 2_000 ) ,
59+ rationale : z . string ( ) . max ( 500 ) ,
60+ } ) ;
61+ const bigBudget = estimateMaxTokensForZodSchema ( big ) ;
62+ const smallBudget = estimateMaxTokensForZodSchema ( small ) ;
63+ expect ( bigBudget ) . toBeGreaterThan ( smallBudget ) ;
64+ expect ( bigBudget ) . toBeGreaterThanOrEqual ( 16_000 ) ;
65+ } ) ;
66+
67+ it ( 'clamps to the maximum (32000) for very large schemas' , ( ) => {
4868 // Build a wide schema with many fields and deeply nested arrays so the
4969 // raw walk well exceeds the 8192 cap.
5070 const wide = z . object ( {
5171 a : z . array ( z . array ( z . array ( z . object ( { x : z . string ( ) , y : z . string ( ) , z : z . string ( ) } ) ) ) ) ,
5272 b : z . array ( z . array ( z . array ( z . object ( { x : z . string ( ) , y : z . string ( ) , z : z . string ( ) } ) ) ) ) ,
5373 c : z . array ( z . array ( z . array ( z . object ( { x : z . string ( ) , y : z . string ( ) , z : z . string ( ) } ) ) ) ) ,
5474 } ) ;
55- expect ( estimateMaxTokensForZodSchema ( wide ) ) . toBe ( 8192 ) ;
75+ expect ( estimateMaxTokensForZodSchema ( wide ) ) . toBe ( 32000 ) ;
5676 } ) ;
5777
5878 it ( 'unwraps optional / nullable / default wrappers' , ( ) => {
@@ -107,6 +127,6 @@ describe('estimateMaxTokensForZodSchema', () => {
107127 expect ( ( ) => estimateMaxTokensForZodSchema ( cyclic ) ) . not . toThrow ( ) ;
108128 const tokens = estimateMaxTokensForZodSchema ( cyclic ) ;
109129 expect ( tokens ) . toBeGreaterThanOrEqual ( 512 ) ;
110- expect ( tokens ) . toBeLessThanOrEqual ( 8192 ) ;
130+ expect ( tokens ) . toBeLessThanOrEqual ( 32000 ) ;
111131 } ) ;
112132} ) ;
0 commit comments