okami.mom/strata

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

Niko1221Merge f-0213-tuicfg (final c6dda74bf) into f-0213-int049ae1c67

main
4.7 KiB94 linesraw
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):
15    return harness.render(harness.find(name), harness.target(URL, None, "m", context))
16
17
18class OutputCap(unittest.TestCase):
19    def test_the_cap_grows_with_the_window_and_the_tested_window_keeps_4096(self):
20        caps = {c: harness.target(URL, None, "m", c).output_cap for c in (8192, 32768, 65536, 131072, 204800, 524288)}
21        self.assertEqual(caps, {8192: 4096, 32768: 4096, 65536: 8192, 131072: 16384, 204800: 25600, 524288: 65536})
22
23    def test_opencode_gets_room_for_a_compaction_summary_on_a_wide_server(self):
24        text = render("opencode", 131072)["files"][0]["text"]
25        cfg = json.loads(text)
26        limit = next(iter(cfg["provider"]["strata"]["models"].values()))["limit"]
27        self.assertEqual(limit, {"context": 131072, "input": 126976, "output": 16384})
28        trigger = limit["input"] - cfg["compaction"]["buffer"]
29        self.assertLess(trigger + limit["output"], limit["context"], "compaction fires with room left for its own summary and the reply")
30        small = json.loads(render("opencode", 32768)["files"][0]["text"])
31        self.assertEqual(next(iter(small["provider"]["strata"]["models"].values()))["limit"], {"context": 32768, "input": 28672, "output": 4096})
32
33    def test_every_config_that_names_a_cap_uses_it(self):
34        r = render("claude-code", 131072)
35        self.assertEqual(r["env"]["CLAUDE_CODE_MAX_OUTPUT_TOKENS"], "16384")
36        self.assertTrue(any("CLAUDE_CODE_MAX_OUTPUT_TOKENS=16384" in n for n in r["limits"]))
37        self.assertNotIn("maxTokens: 4096", render("omp", 131072)["files"][0]["text"])
38        self.assertIn("maxTokens: 16384", render("omp", 131072)["files"][0]["text"])
39        self.assertIn('"maxTokens": 16384', render("pi", 131072)["files"][0]["text"])
40        self.assertIn('"maxTokens": 16384', render("openclaw", 131072)["files"][0]["text"])
41        self.assertIn("CLAUDE_CODE_MAX_OUTPUT_TOKENS=4096", " ".join(render("claude-code", 32768)["limits"]))
42
43    def 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."""
45        cfg = json.loads(render("opencode", 131072)["files"][0]["text"])
46        p = cfg["provider"]["strata"]
47        self.assertEqual((p["npm"], sorted(p["options"])), ("@ai-sdk/openai-compatible", ["apiKey", "baseURL"]))
48        model = next(iter(p["models"].values()))
49        self.assertTrue(model["tool_call"] and model["reasoning"])
50
51
52class RewriteKeepsTheirCap(unittest.TestCase):
53    def write(self, old, ctx=131072):
54        with tempfile.TemporaryDirectory() as tmp:
55            path = Path(tmp) / "opencode.json"
56            if old is not None:
57                path.write_text(json.dumps(old), encoding="utf-8")
58            spec = render("opencode", ctx)["files"][0]
59            spec["path"] = str(path)
60            harness.write_file(spec, backup=False)
61            return json.loads(path.read_text(encoding="utf-8"))
62
63    @staticmethod
64    def out(cfg):
65        return next(iter(cfg["provider"]["strata"]["models"].values()))["limit"]["output"]
66
67    def old_file(self, output):
68        return {"theme": "mine", "provider": {"strata": {"models": {"m": {"limit": {"context": 131072, "input": 126976, "output": output}}}}}}
69
70    def test_the_4096_an_older_strata_wrote_is_replaced(self):
71        cfg = self.write(self.old_file(4096))
72        self.assertEqual(self.out(cfg), 16384)
73        self.assertEqual(cfg["theme"], "mine")
74
75    def test_a_cap_the_person_set_stays(self):
76        for value in (8192, 32768):
77            self.assertEqual(self.out(self.write(self.old_file(value))), value)
78
79    def test_a_new_file_gets_the_generated_cap(self):
80        self.assertEqual(self.out(self.write(None)), 16384)
81
82    def test_claude_settings_keep_a_chosen_cap(self):
83        with tempfile.TemporaryDirectory() as tmp:
84            path = Path(tmp) / "settings.json"
85            path.write_text(json.dumps({"env": {"CLAUDE_CODE_MAX_OUTPUT_TOKENS": "32000", "X": "1"}}), encoding="utf-8")
86            spec = render("claude-code", 131072)["files"][0]
87            spec["path"] = str(path)
88            harness.write_file(spec, backup=False)
89            env = json.loads(path.read_text())["env"]
90        self.assertEqual((env["CLAUDE_CODE_MAX_OUTPUT_TOKENS"], env["X"]), ("32000", "1"))
91
92
93if __name__ == "__main__":
94    unittest.main()