char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
ecb3f3b
main
1import { assertEquals } from "@std/assert" ; 2import { type CompactionCap , defineExtension } from "@berry/api" ; 3import { envApiKeyAuth , type Model , type Provider } from "@earendil-works/pi-ai" ; 4import { setup } from "./fixture.ts" ; 5 6const model = ( id :string , contextWindow :number ) :Model < "openai-completions" > => ({ 7 id, 8name :id , 9api :"openai-completions" , 10provider :"sized" , 11baseUrl :"https://example.invalid" , 12input :[ "text" ], 13reasoning :false , 14 contextWindow, 15maxTokens :4096 , 16cost :{ input :0 , output :0 , cacheRead :0 , cacheWrite :0 }, 17}); 18 19const provider :Provider = { 20id :"sized" , 21name :"sized" , 22auth :{ apiKey :envApiKeyAuth ( "sized key" , [ "SIZED_KEY" ]) }, 23getModels :() => [ model ( "big" , 1_048_576 ), model ( "small" , 20_000 )], 24stream :() => { 25throw new Error ( "unused" ); 26}, 27streamSimple :() => { 28throw new Error ( "unused" ); 29}, 30}; 31 32const sized = () => 33defineExtension ({ 34name :"sized" , 35initial :() => null , 36setup ( ext ) { 37ext . provider ( provider ); 38}, 39}); 40 41const cap = ( name :string , value :CompactionCap | undefined ) => 42defineExtension ({ 43 name, 44initial :() => null , 45setup ( ext ) { 46ext . compactionCap (() => value ); 47}, 48}); 49 50Deno . test ( "generation sees windows capped at the threshold plus reserve, smaller ones alone" , async () => { 51await usingharness = await setup (); 52const { berry, faux, models} = harness ; 53await berry . install ( sized ()); 54await berry . install ( cap ( "cap" , { maxContextTokens :262_144 , backgroundTokens :32_768 })); 55const seen = berry . effectiveModels ; 56assertEquals ( seen . getModel ( "sized" , "big" )?. contextWindow , 262_144 + 16_384 ); 57assertEquals ( seen . getModel ( "sized" , "small" )?. contextWindow , 20_000 ); 58assertEquals ( seen . getModel ( "faux" , faux . getModel (). id )?. contextWindow , 128_000 ); 59// the stored catalog keeps its own windows 60assertEquals ( models . getModel ( "sized" , "big" )?. contextWindow , 1_048_576 ); 61assertEquals ( 62berry . compactionCap (), 63{ maxContextTokens :262_144 , backgroundTokens :32_768 }, 64); 65}); 66 67Deno . test ( "the tightest cap wins and lapses on uninstall" , async () => { 68await usingharness = await setup (); 69const { berry} = harness ; 70const seen = berry . effectiveModels ; 71await berry . install ( sized ()); 72await berry . install ( cap ( "loose" , { maxContextTokens :500_000 , backgroundTokens :1_000 })); 73await berry . install ( cap ( "tight" , { maxContextTokens :100_000 , backgroundTokens :2_000 })); 74assertEquals ( 75berry . compactionCap (), 76{ maxContextTokens :100_000 , backgroundTokens :2_000 }, 77); 78assertEquals ( seen . getModel ( "sized" , "big" )?. contextWindow , 100_000 + 16_384 ); 79berry . uninstall ( "tight" ); 80assertEquals ( 81berry . compactionCap (), 82{ maxContextTokens :500_000 , backgroundTokens :1_000 }, 83); 84assertEquals ( seen . getModel ( "sized" , "big" )?. contextWindow , 500_000 + 16_384 ); 85berry . uninstall ( "loose" ); 86assertEquals ( berry . compactionCap (), undefined ); 87assertEquals ( seen . getModel ( "sized" , "big" )?. contextWindow , 1_048_576 ); 88}); 89 90Deno . test ( "invalid and failing caps read as no cap" , async () => { 91await usingharness = await setup (); 92const { berry} = harness ; 93await berry . install ( sized ()); 94await berry . install ( cap ( "zero" , { maxContextTokens :0 , backgroundTokens :0 })); 95await berry . install ( cap ( "negative" , { maxContextTokens :100_000 , backgroundTokens :- 1 })); 96await berry . install ( defineExtension ({ 97name :"failing" , 98initial :() => null , 99setup ( ext ) { 100ext . compactionCap (() => { 101throw new Error ( "no cap here" ); 102}); 103}, 104})); 105assertEquals ( berry . compactionCap (), undefined ); 106assertEquals ( berry . effectiveModels . getModel ( "sized" , "big" )?. contextWindow , 1_048_576 ); 107});