okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
049ae1c67
main
1"""Round 0213 TUICFG, step 9: the output cap strata connect writes (#5006; PR #4230's idea, written here) and the OpenCode block (#4528).""" 2from __future__import annotations 3 4import json 5import tempfile 6import unittest 7from pathlib import Path 8 9from strata_cli .launch import harness 10 11URL = "http://127.0.0.1:18900" 12 13 14def render (name ,context ): 15return harness .render (harness .find (name ),harness .target (URL ,None ,"m" ,context )) 16 17 18class OutputCap (unittest .TestCase ): 19def test_the_cap_grows_with_the_window_and_the_tested_window_keeps_4096 (self ): 20caps = {c :harness .target (URL ,None ,"m" ,c ).output_cap for c in (8192 ,32768 ,65536 ,131072 ,204800 ,524288 )} 21self .assertEqual (caps , {8192 :4096 ,32768 :4096 ,65536 :8192 ,131072 :16384 ,204800 :25600 ,524288 :65536 }) 22 23def test_opencode_gets_room_for_a_compaction_summary_on_a_wide_server (self ): 24text = render ("opencode" ,131072 )["files" ][0 ]["text" ] 25cfg = json .loads (text ) 26limit = next (iter (cfg ["provider" ]["strata" ]["models" ].values ()))["limit" ] 27self .assertEqual (limit , {"context" :131072 ,"input" :126976 ,"output" :16384 }) 28trigger = limit ["input" ]- cfg ["compaction" ]["buffer" ] 29self .assertLess (trigger + limit ["output" ],limit ["context" ],"compaction fires with room left for its own summary and the reply" ) 30small = json .loads (render ("opencode" ,32768 )["files" ][0 ]["text" ]) 31self .assertEqual (next (iter (small ["provider" ]["strata" ]["models" ].values ()))["limit" ], {"context" :32768 ,"input" :28672 ,"output" :4096 }) 32 33def test_every_config_that_names_a_cap_uses_it (self ): 34r = render ("claude-code" ,131072 ) 35self .assertEqual (r ["env" ]["CLAUDE_CODE_MAX_OUTPUT_TOKENS" ],"16384" ) 36self .assertTrue (any ("CLAUDE_CODE_MAX_OUTPUT_TOKENS=16384" in n for n in r ["limits" ])) 37self .assertNotIn ("maxTokens: 4096" ,render ("omp" ,131072 )["files" ][0 ]["text" ]) 38self .assertIn ("maxTokens: 16384" ,render ("omp" ,131072 )["files" ][0 ]["text" ]) 39self .assertIn ('"maxTokens": 16384' ,render ("pi" ,131072 )["files" ][0 ]["text" ]) 40self .assertIn ('"maxTokens": 16384' ,render ("openclaw" ,131072 )["files" ][0 ]["text" ]) 41self .assertIn ("CLAUDE_CODE_MAX_OUTPUT_TOKENS=4096" ," " .join (render ("claude-code" ,32768 )["limits" ])) 42 43def test_the_opencode_block_keeps_the_fields_the_tested_clients_read (self ): 44"""#4528: the block is the documented (not the newer 'providers') schema; 2.2.1 and 2.2.3 take it, per the thread. A change of schema is the owner's call.""" 45cfg = json .loads (render ("opencode" ,131072 )["files" ][0 ]["text" ]) 46p = cfg ["provider" ]["strata" ] 47self .assertEqual ((p ["npm" ],sorted (p ["options" ])), ("@ai-sdk/openai-compatible" , ["apiKey" ,"baseURL" ])) 48model = next (iter (p ["models" ].values ())) 49self .assertTrue (model ["tool_call" ]and model ["reasoning" ]) 50 51 52class RewriteKeepsTheirCap (unittest .TestCase ): 53def write (self ,old ,ctx = 131072 ): 54with tempfile .TemporaryDirectory ()as tmp : 55path = Path (tmp )/ "opencode.json" 56if old is not None : 57path .write_text (json .dumps (old ),encoding = "utf-8" ) 58spec = render ("opencode" ,ctx )["files" ][0 ] 59spec ["path" ]= str (path ) 60harness .write_file (spec ,backup = False ) 61return json .loads (path .read_text (encoding = "utf-8" )) 62 63@staticmethod 64def out (cfg ): 65return next (iter (cfg ["provider" ]["strata" ]["models" ].values ()))["limit" ]["output" ] 66 67def old_file (self ,output ): 68return {"theme" :"mine" ,"provider" : {"strata" : {"models" : {"m" : {"limit" : {"context" :131072 ,"input" :126976 ,"output" :output }}}}}} 69 70def test_the_4096_an_older_strata_wrote_is_replaced (self ): 71cfg = self .write (self .old_file (4096 )) 72self .assertEqual (self .out (cfg ),16384 ) 73self .assertEqual (cfg ["theme" ],"mine" ) 74 75def test_a_cap_the_person_set_stays (self ): 76for value in (8192 ,32768 ): 77self .assertEqual (self .out (self .write (self .old_file (value ))),value ) 78 79def test_a_new_file_gets_the_generated_cap (self ): 80self .assertEqual (self .out (self .write (None )),16384 ) 81 82def test_claude_settings_keep_a_chosen_cap (self ): 83with tempfile .TemporaryDirectory ()as tmp : 84path = Path (tmp )/ "settings.json" 85path .write_text (json .dumps ({"env" : {"CLAUDE_CODE_MAX_OUTPUT_TOKENS" :"32000" ,"X" :"1" }}),encoding = "utf-8" ) 86spec = render ("claude-code" ,131072 )["files" ][0 ] 87spec ["path" ]= str (path ) 88harness .write_file (spec ,backup = False ) 89env = json .loads (path .read_text ())["env" ] 90self .assertEqual ((env ["CLAUDE_CODE_MAX_OUTPUT_TOKENS" ],env ["X" ]), ("32000" ,"1" )) 91 92 93if __name__ == "__main__" : 94unittest .main ()