okami.mom/strata

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

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
4.9 KiB103 linesraw
1"""Lane NET item 5: --model-path and the data folder Strata guesses from it (the folder above a `models` folder). Strata keeps its own files (the
2prepared data, the picture reader) there only when it can write there and it is not a Hugging Face cache; otherwise in the data folder every
3command agrees on, with one line saying so, instead of stopping with "Strata cannot write to ...". The finish marks of the person's own files go
4to Strata's own marks/ folder, never beside the files."""
5
6from __future__ import annotations
7
8import os
9import tempfile
10import unittest
11from pathlib import Path
12from unittest import mock
13
14import setup
15from strata_cli.launch import settings as S
16from strata_cli.launch.catalog import Catalog
17from strata_cli.launch.runner import Runner
18from strata_control import discovery
19from tests.terminal.launch_support import HW_12, Home, catalog
20from tests.terminal.test_own_files import IQ3XXS, gguf_bytes
21
22
23class Base(unittest.TestCase):
24    def setUp(self):
25        self.home = Home().__enter__()
26        self.addCleanup(self.home.__exit__, None, None, None)
27        p = mock.patch.object(Catalog, "hardware", lambda self, fresh=False: dict(HW_12, disk_free_gb=500.0))
28        p.start()
29        self.addCleanup(p.stop)
30        t = tempfile.TemporaryDirectory(prefix="strata-netown-")
31        self.addCleanup(t.cleanup)
32        self.base = Path(t.name)
33
34    def own(self, folder: Path) -> Path:
35        folder.mkdir(parents=True, exist_ok=True)
36        for n in IQ3XXS:
37            (folder / n).write_bytes(gguf_bytes(["IQ3_XXS"]))
38        return folder
39
40
41class HuggingFaceCache(Base):
42    def test_cache_layouts(self):
43        snap = self.base / "hub" / "models--unsloth--Qwen3.8-Flash-Next-GGUF" / "snapshots" / "38bb39ee" / "UD-Q4_K_XL"
44        snap.mkdir(parents=True)
45        env = {"HOME": str(self.base / "h")}
46        self.assertTrue(discovery.in_hf_cache(snap, env))
47        self.assertTrue(discovery.in_hf_cache(self.base / "hub", env), "a folder that holds models--* folders is a cache")
48        self.assertTrue(discovery.in_hf_cache(self.base / "elsewhere" / "x", {"HF_HOME": str(self.base / "elsewhere")}))
49        self.assertFalse(discovery.in_hf_cache(self.base / "data", env))
50
51    def test_a_guessed_folder_inside_a_cache_is_not_used(self):
52        own = self.own(self.base / "hfhome" / "models" / "IQ3_XXS")
53        first = own / IQ3XXS[0]
54        self.assertEqual(discovery.prepared_beside(first)["data_dir"], self.base / "hfhome")
55        with mock.patch.dict(os.environ, {"HF_HOME": str(self.base / "hfhome")}):
56            self.assertIsNone(discovery.writable_beside(first))
57        self.assertEqual(discovery.writable_beside(first), self.base / "hfhome")
58
59
60@unittest.skipIf(os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0), "a read-only folder needs POSIX permissions and no root")
61class ReadOnlyFolder(Base):
62    def test_launch_uses_the_data_folder_and_says_so(self):
63        top = self.base / "lender"
64        own = self.own(top / "models" / "IQ3_XXS")
65        os.chmod(top, 0o555)
66        self.addCleanup(os.chmod, top, 0o755)
67        r = Runner(S.LaunchSettings(family="qwen", model="IQ3_XXS", vision="off", mtp="off", drafting="off", context=32768, model_path=str(own)),
68                   catalog=catalog(HW_12))
69        self.assertIsNone(r.existing_data_dir(), "not the folder Strata cannot write to: setup's own data folder")
70        self.assertEqual(r.beside_refused(), top)
71        self.assertNotIn("--data-dir", r.setup_args())
72        self.assertIn("--gguf-dir", r.setup_args())
73
74    def test_a_writable_folder_is_still_used(self):
75        top = self.base / "mine"
76        own = self.own(top / "models" / "IQ3_XXS")
77        r = Runner(S.LaunchSettings(family="qwen", model="IQ3_XXS", vision="off", mtp="off", drafting="off", context=32768, model_path=str(own)),
78                   catalog=catalog(HW_12))
79        self.assertEqual(r.existing_data_dir(), top)
80        self.assertIsNone(r.beside_refused())
81
82
83class FinishMarks(Base):
84    def test_own_files_are_marked_in_stratas_folder(self):
85        own = self.own(self.base / "mine" / "IQ3_XXS")
86        root = self.base / "strata"
87        root.mkdir()
88        shard = own / IQ3XXS[0]
89        with mock.patch.object(setup, "ROOT", root), mock.patch.object(setup, "_OWN_DIRS", []):
90            setup.own_files_in(own)
91            setup.mark(shard, "whole")
92            self.assertFalse(shard.with_name(shard.name + ".done").exists(), "nothing is written beside the person's files")
93            self.assertTrue(setup.done(shard))
94            self.assertEqual(len(list((root / "marks").glob("*.done"))), 1)
95            other = self.base / "data" / "x.gguf"
96            other.parent.mkdir()
97            other.write_bytes(b"x")
98            setup.mark(other)                                # a download of Strata's own: beside it, as before
99            self.assertTrue(other.with_name("x.gguf.done").exists())
100
101
102if __name__ == "__main__":
103    unittest.main()