okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d8a49433e
main
1"""Config "env_to_user": opencode's <env> block (working directory, date) leaves the system message for the first user 2message, so the system turn is the same in every session and the prefix cache keeps it. Off (the default), nothing moves.""" 3import sys 4import unittest 5from pathlib import Path 6 7sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 8 9import serve .frontend as fe # noqa: E402 10 11SYS = ("You are opencode.\n" 12"Here is some useful information about the environment you are running in:\n" 13"<env>\n" 14" Working directory: C:/a\n" 15" Today's date: Mon Oct 12 2026\n" 16"</env>\n" 17"Instructions that never change." ) 18 19 20class EnvBlockToUser (unittest .TestCase ): 21def setUp (self ): 22self .addCleanup (setattr ,fe ,"ENV_TO_USER" ,fe .ENV_TO_USER ) 23 24def convert (self ,on ,system = SYS ,user = "hi" ): 25fe .ENV_TO_USER = on 26msgs ,_ ,_ = fe .openai_to_messages ({"messages" : [{"role" :"system" ,"content" :system }, 27 {"role" :"user" ,"content" :user }]}) 28return msgs 29 30def test_moved_to_the_first_user_message (self ): 31msgs = self .convert (True ) 32self .assertEqual (msgs [0 ]["content" ],"You are opencode.\nInstructions that never change." ) 33self .assertTrue (msgs [1 ]["content" ].startswith ( 34"Here is some useful information about the environment you are running in:\n<env>\n Working directory: C:/a" )) 35self .assertTrue (msgs [1 ]["content" ].endswith ("</env>\n\nhi" )) 36 37def test_two_folders_share_the_system_turn (self ): 38a = self .convert (True )[0 ]["content" ] 39b = self .convert (True ,system = SYS .replace ("C:/a" ,"D:/b" ))[0 ]["content" ] 40self .assertEqual (a ,b ) 41 42def test_content_parts_get_a_leading_text_part (self ): 43fe .ENV_TO_USER = True 44out = fe ._env_to_user ([{"role" :"system" ,"content" :SYS }, 45 {"role" :"user" ,"content" : [{"type" :"image" ,"image" :"x.png" }, {"type" :"text" ,"text" :"hi" }]}]) 46self .assertEqual (out [1 ]["content" ][0 ]["type" ],"text" ) 47self .assertIn ("<env>" ,out [1 ]["content" ][0 ]["text" ]) 48self .assertEqual (out [1 ]["content" ][1 :], [{"type" :"image" ,"image" :"x.png" }, {"type" :"text" ,"text" :"hi" }]) 49 50def test_text_parts_are_read_as_one_text (self ): 51msgs = self .convert (True ,user = [{"type" :"text" ,"text" :"hi" }]) 52self .assertIn ("<env>" ,msgs [1 ]["content" ]if isinstance (msgs [1 ]["content" ],str )else msgs [1 ]["content" ][0 ]["text" ]) 53 54def test_anthropic_requests_too (self ): 55fe .ENV_TO_USER = True 56msgs ,_ ,_ = fe .anthropic_to_messages ({"system" :SYS ,"messages" : [{"role" :"user" ,"content" :"hi" }]}) 57self .assertNotIn ("<env>" ,msgs [0 ]["content" ]) 58self .assertIn ("<env>" ,msgs [1 ]["content" ]) 59 60def test_off_and_no_block_change_nothing (self ): 61self .assertEqual (self .convert (False )[0 ]["content" ],SYS ) 62self .assertEqual (self .convert (True ,system = "Plain system prompt." )[0 ]["content" ],"Plain system prompt." ) 63 64 65if __name__ == "__main__" : 66unittest .main ()