@@ -362,6 +362,114 @@ it.effect("auto compaction estimates current content against the buffered prompt
362362 } ) ,
363363)
364364
365+ it . effect ( "clamps an oversized compaction buffer and waits for an exchange to summarize" , ( ) =>
366+ Effect . gen ( function * ( ) {
367+ const compaction = yield * SessionCompaction . Service
368+ const session = yield * insertSession ( Session . ID . make ( "ses_oversized_buffer" ) )
369+ yield * compaction . transform ( ( editor ) => editor . configure ( { buffer : 600_000 } ) )
370+
371+ const assistant = ( tokens : number ) =>
372+ Schema . decodeUnknownSync ( SessionMessage . Assistant ) ( {
373+ id : SessionMessage . ID . create ( ) ,
374+ type : "assistant" ,
375+ agent : Agent . defaultID ,
376+ model : { id : "summary-model" , providerID : "test" } ,
377+ content : [ { type : "text" , text : "Done" } ] ,
378+ tokens : { input : tokens , output : 0 , reasoning : 0 , cache : { read : 0 , write : 0 } } ,
379+ time : { created : 0 , completed : 0 } ,
380+ } )
381+ const user = ( text : string ) =>
382+ SessionMessage . User . make ( {
383+ id : SessionMessage . ID . create ( ) ,
384+ type : "user" ,
385+ text,
386+ time : { created : DateTime . makeUnsafe ( 0 ) } ,
387+ } )
388+ const context = ( messages : readonly SessionMessage . Info [ ] ) => ( {
389+ ...loaded ( session , messages ) ,
390+ model : SessionRunnerModel . resolved ( model , {
391+ capabilities : { tools : true , input : [ "text" ] , output : [ "text" ] } ,
392+ cost : [ ] ,
393+ limit : { context : 100_000 , output : 10_000 } ,
394+ } ) ,
395+ agent : {
396+ id : Agent . defaultID ,
397+ info : { ...Agent . Info . default ( Agent . defaultID ) , system : "You are a helpful assistant." } ,
398+ } ,
399+ initial : "Project instructions." ,
400+ } )
401+ const status = ( messages : readonly SessionMessage . Info [ ] ) =>
402+ compaction . compact ( { reason : "auto" , context : context ( messages ) } ) . pipe ( Effect . map ( ( outcome ) => outcome . status ) )
403+
404+ // 600k free tokens on a 100k window is treated as unset, so the ceiling is the default 16k reserve
405+ // below the window.
406+ expect ( yield * status ( [ assistant ( 83_999 ) ] ) ) . toBe ( "skipped" )
407+ expect ( yield * status ( [ assistant ( 84_000 ) ] ) ) . toBe ( "completed" )
408+ // The opening prompt alone has no reply to summarize; it must not compact or surface a failure.
409+ expect ( yield * status ( [ user ( "x" . repeat ( 400_000 ) ) ] ) ) . toBe ( "skipped" )
410+ } ) ,
411+ )
412+
413+ // Provider-switch regression suggested by @intrepid37 on the PR.
414+ it . effect ( "auto compaction stays model-aware when switching between differently sized providers" , ( ) =>
415+ Effect . gen ( function * ( ) {
416+ const compaction = yield * SessionCompaction . Service
417+ const session = yield * insertSession ( Session . ID . make ( "ses_synthetic_provider_switch" ) )
418+ // A user may configure this for a large model, then change providers mid-thread.
419+ yield * compaction . transform ( ( editor ) => editor . configure ( { buffer : 600_000 } ) )
420+
421+ const models = {
422+ large : LanguageModel . make ( { id : "synthetic-large" , provider : "synthetic-large" , route : OpenAIChat . route } ) ,
423+ small : LanguageModel . make ( { id : "synthetic-small" , provider : "synthetic-small" , route : OpenAIChat . route } ) ,
424+ }
425+ const assistant = ( provider : "large" | "small" , inputTokens : number ) =>
426+ Schema . decodeUnknownSync ( SessionMessage . Assistant ) ( {
427+ id : SessionMessage . ID . create ( ) ,
428+ type : "assistant" ,
429+ agent : Agent . defaultID ,
430+ model : { id : models [ provider ] . id , providerID : models [ provider ] . provider } ,
431+ content : [ { type : "text" , text : "Synthetic response" } ] ,
432+ tokens : { input : inputTokens , output : 0 , reasoning : 0 , cache : { read : 0 , write : 0 } } ,
433+ time : { created : 0 , completed : 0 } ,
434+ } )
435+ const user = ( text : string ) =>
436+ SessionMessage . User . make ( {
437+ id : SessionMessage . ID . create ( ) ,
438+ type : "user" ,
439+ text,
440+ time : { created : DateTime . makeUnsafe ( 0 ) } ,
441+ } )
442+ const status = ( provider : "large" | "small" , messages : readonly SessionMessage . Info [ ] ) =>
443+ compaction
444+ . compact ( {
445+ reason : "auto" ,
446+ context : {
447+ ...loaded ( session , messages ) ,
448+ model : SessionRunnerModel . resolved ( models [ provider ] , {
449+ capabilities : { tools : true , input : [ "text" ] , output : [ "text" ] } ,
450+ cost : [ ] ,
451+ limit : { context : provider === "large" ? 1_000_000 : 200_000 , output : 32_000 } ,
452+ } ) ,
453+ } ,
454+ } )
455+ . pipe ( Effect . map ( ( outcome ) => outcome . status ) )
456+
457+ const big = assistant ( "large" , 250_000 )
458+ const start = [ user ( "Start synthetic task" ) , big ]
459+ expect ( yield * status ( "large" , start ) ) . toBe ( "skipped" ) // 1M - 600k remains usable.
460+
461+ const switched = [ ...start , user ( "Switch to smaller provider" ) ]
462+ expect ( yield * status ( "small" , switched ) ) . toBe ( "skipped" ) // Ignore large provider's token count.
463+ const small = [ ...switched , assistant ( "small" , 25_000 ) ]
464+ expect ( yield * status ( "small" , small ) ) . toBe ( "skipped" ) // Oversized buffer falls back to default.
465+
466+ expect ( yield * status ( "large" , [ ...small , user ( "Switch back" ) ] ) ) . toBe ( "skipped" )
467+
468+ // Real pressure on the smaller provider still triggers normal auto-compaction (200k - 30k reserve).
469+ expect ( yield * status ( "small" , [ ...switched , assistant ( "small" , 180_000 ) ] ) ) . toBe ( "completed" )
470+ } ) ,
471+ )
472+
365473/** Seeds the global project plus one session row, returning the projected session. */
366474const insertSession = ( id : Session . ID , overrides ?: Partial < typeof SessionTable . $inferInsert > ) =>
367475 Effect . gen ( function * ( ) {
0 commit comments