okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

zsaribogaserve: "env_to_user" (opt-in) moves opencode's <env> block into the first user message, so a new folder or day reuses the cached system prompt (11.9 -> 1.1 s first request)d8a49433e

main
3.0 KiB66 linesraw
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):
21    def setUp(self):
22        self.addCleanup(setattr, fe, "ENV_TO_USER", fe.ENV_TO_USER)
23
24    def convert(self, on, system=SYS, user="hi"):
25        fe.ENV_TO_USER = on
26        msgs, _, _ = fe.openai_to_messages({"messages": [{"role": "system", "content": system},
27                                                         {"role": "user", "content": user}]})
28        return msgs
29
30    def test_moved_to_the_first_user_message(self):
31        msgs = self.convert(True)
32        self.assertEqual(msgs[0]["content"], "You are opencode.\nInstructions that never change.")
33        self.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"))
35        self.assertTrue(msgs[1]["content"].endswith("</env>\n\nhi"))
36
37    def test_two_folders_share_the_system_turn(self):
38        a = self.convert(True)[0]["content"]
39        b = self.convert(True, system=SYS.replace("C:/a", "D:/b"))[0]["content"]
40        self.assertEqual(a, b)
41
42    def test_content_parts_get_a_leading_text_part(self):
43        fe.ENV_TO_USER = True
44        out = fe._env_to_user([{"role": "system", "content": SYS},
45                               {"role": "user", "content": [{"type": "image", "image": "x.png"}, {"type": "text", "text": "hi"}]}])
46        self.assertEqual(out[1]["content"][0]["type"], "text")
47        self.assertIn("<env>", out[1]["content"][0]["text"])
48        self.assertEqual(out[1]["content"][1:], [{"type": "image", "image": "x.png"}, {"type": "text", "text": "hi"}])
49
50    def test_text_parts_are_read_as_one_text(self):
51        msgs = self.convert(True, user=[{"type": "text", "text": "hi"}])
52        self.assertIn("<env>", msgs[1]["content"] if isinstance(msgs[1]["content"], str) else msgs[1]["content"][0]["text"])
53
54    def test_anthropic_requests_too(self):
55        fe.ENV_TO_USER = True
56        msgs, _, _ = fe.anthropic_to_messages({"system": SYS, "messages": [{"role": "user", "content": "hi"}]})
57        self.assertNotIn("<env>", msgs[0]["content"])
58        self.assertIn("<env>", msgs[1]["content"])
59
60    def test_off_and_no_block_change_nothing(self):
61        self.assertEqual(self.convert(False)[0]["content"], SYS)
62        self.assertEqual(self.convert(True, system="Plain system prompt.")[0]["content"], "Plain system prompt.")
63
64
65if __name__ == "__main__":
66    unittest.main()