okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
0a29dfa
main
1//// Pure archive bounds and portable compaction cursors guard edge cases 2//// that require unrealistic provider history sizes or projections in E2E. 3//// Frames fitting a provider's edge needs a provider that declares one, and 4//// the E2E fake provider declares none. 5 6import albedo/harness/compaction 7import albedo/harness/extensions/snapcompact/extension as snapcompact 8import albedo/openai_api/types 9import gleam/json 10import gleam/list 11import gleam/option .{ None } 12import gleam/string 13import gleeunit/should 14 15pub fn bound_keeps_the_first_and_newest_frames_test () -> Nil { 16let shape = snapcompact . Shape ( 11 , 16 , 256 , 1 ) 17// 23 cells per frame: ten frames, bounded to four. 18let pages = 19list . map ([ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" ], string . repeat ( 20 _, 2123 , 22)) 23let #( kept , dropped ) = snapcompact . bound ( shape , 4 , string . concat ( pages )) 24snapcompact . paginate ( shape , kept ) 25|> should . equal ([ 26string . repeat ( "a" , 23 ), 27string . repeat ( "h" , 23 ), 28string . repeat ( "i" , 23 ), 29string . repeat ( "j" , 23 ), 30]) 31dropped |> should . equal ( 6 * 23 ) 32snapcompact . bound ( shape , 10 , string . concat ( pages )) 33|> should . equal (#( string . concat ( pages ), 0 )) 34} 35 36pub fn a_full_frame_renders_inside_the_provider_edge_test () -> Nil { 37let limits = types . ImageLimits ( max_edge : 1000 , max_images : None ) 38use model <- list . each ([ "gemini-3-pro" , "gpt-5" , "claude-opus-5-5" ]) 39let shape = snapcompact . fit ( snapcompact . shape ( model ), limits ) 40let assert [ page , ..] = 41snapcompact . paginate ( shape , string . repeat ( "x\u{2588}" , 100_000 )) 42let assert Ok (#( width , height , _ )) = 43snapcompact . render_frame_test ( page , shape . advance , shape . pitch , shape . width ) 44{ width <= 1000 && height <= 1000 } |> should . be_true 45} 46 47pub fn paginate_never_splits_a_multibyte_cell_test () -> Nil { 48let shape = snapcompact . Shape ( 11 , 16 , 256 , 1 ) 49// The 23rd cell, which fills the first frame, is the 3-byte newline cell. 50let first = string . repeat ( "a" , 22 ) <> "\u{2588}" 51let second = string . repeat ( "b" , 23 ) 52snapcompact . paginate ( shape , first <> second ) 53|> should . equal ([ first , second ]) 54} 55 56pub fn split_tail_keeps_whole_units_within_budget_test () -> Nil { 57let history = [ 58types . User ( "first" ), 59types . Assistant ( "a1" ), 60types . ToolOutput ( "t1" , "o1" , []), 61types . User ( "second" ), 62types . Assistant ( "a2" ), 63types . User ( "third" ), 64] 65// A one-token budget still keeps the newest unit. 66compaction . split_tail ( history , 1 ) 67|> should . equal (#( list . take ( history , 5 ), [ types . User ( "third" )])) 68// A single unit is never split. 69compaction . split_tail ([ types . User ( "only" ), types . Assistant ( "a" )], 1 ) 70|> should . equal (#([], [ types . User ( "only" ), types . Assistant ( "a" )])) 71} 72 73pub fn split_tail_never_orphans_a_tool_output_test () -> Nil { 74let history = [ 75types . User ( "first" ), 76types . User ( "second" ), 77types . Assistant ( "calling" ), 78types . ToolOutput ( "t9" , "result" , []), 79types . User ( "third" ), 80] 81let #( evicted , tail ) = compaction . split_tail ( history , 1 ) 82evicted |> list . last |> should . equal ( Ok ( types . ToolOutput ( "t9" , "result" , []))) 83tail |> should . equal ([ types . User ( "third" )]) 84} 85 86pub fn cut_resumes_across_projection_changes_test () -> Nil { 87let prefix = [ types . User ( "one" ), types . Assistant ( "a" ), types . User ( "two" )] 88let cut = compaction . cut_of ( prefix ) 89cut . users |> should . equal ( 2 ) 90// Another provider merges or drops assistant output; users stay put. 91let other = [ types . User ( "one" ), types . User ( "two" ), types . Assistant ( "b" )] 92let assert Ok (#( evicted , rest )) = 93compaction . resume ( list . append ( other , [ types . User ( "three" )]), cut ) 94evicted |> should . equal ( other ) 95rest |> should . equal ([ types . User ( "three" )]) 96// A rewritten user message no longer matches. 97compaction . resume ( 98[ types . User ( "uno" ), types . User ( "two" ), types . User ( "three" )], 99cut , 100) 101|> should . equal ( Error ( Nil )) 102// No user message left after the cut: nothing to resume into. 103compaction . resume ( prefix , cut ) |> should . equal ( Error ( Nil )) 104} 105 106fn replay ( protocol : types . Protocol , body : String ) -> types . Input { 107let assert Ok ( item ) = json . parse ( body , types . replay_decoder ( protocol )) 108types . Replay ( item ) 109} 110 111pub fn responses_items_render_as_text_and_calls_without_reasoning_test () -> Nil { 112let archive = 113snapcompact . normalize ([ 114replay ( 115types . Responses , 116"{\"type\":\"reasoning\",\"id\":\"rs_1\",\"encrypted_content\":\"OPAQUEBLOB\"}" , 117), 118replay ( 119types . Responses , 120"{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"checking\"}]}" , 121), 122replay ( 123types . Responses , 124"{\"type\":\"function_call\",\"call_id\":\"c1\",\"name\":\"python\",\"arguments\":\"{\\\"code\\\":\\\"print(1)\\\"}\"}" , 125), 126]) 127string . contains ( archive , "OPAQUEBLOB" ) |> should . be_false 128string . contains ( archive , "¶turn:" ) |> should . be_false 129string . contains ( archive , "¶ai: checking" ) |> should . be_true 130string . contains ( archive , "→ python(code = print(1))" ) |> should . be_true 131} 132 133pub fn evicted_capability_notes_do_not_reach_the_archive_test () -> Nil { 134let archive = 135snapcompact . normalize ([ 136types . User ( 137"<system-note origin=\"capabilities changed\">old catalog</system-note>" , 138), 139types . User ( 140"<system-note origin=\"python kernel\">kept note</system-note>" , 141), 142types . User ( "real question" ), 143]) 144string . contains ( archive , "old catalog" ) |> should . be_false 145string . contains ( archive , "kept note" ) |> should . be_true 146string . contains ( archive , "real question" ) |> should . be_true 147}