Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,12 @@ def move_into(src: Path, dst: Path) -> None:
pass


def model_configs(folder: Path) -> list:
"""The model configs in a Strata folder. The server keeps a config's Chat settings next to it as
`strata-<model>.shared-settings.json`: it matches the pattern but is no config (no exe, no args)."""
return [p for p in folder.glob("strata-*.json") if not p.name.endswith(".shared-settings.json")]


def repoint_config(cfg_file: Path, old: Path, new: Path) -> None:
"""A config whose model files moved from `old` to `new` points at them there (each path only if its file is
now there and no longer at the old place)."""
Expand Down Expand Up @@ -1557,7 +1563,7 @@ def data_folder(requested: str | None) -> tuple:
(folder / d).rmdir() # empty now
except OSError:
pass
for c in folder.glob("strata-*.json"):
for c in model_configs(folder):
repoint_config(c, folder, dest)
if has_data(folder):
elsewhere.append(folder) # in use, or a copy the data folder already has
Expand All @@ -1573,7 +1579,7 @@ def previous_config(elsewhere_first: list, settings: dict):
"""The most recently used model config of another Strata folder on this PC, for a folder that has none yet."""
cands = []
for folder in [*elsewhere_first, *other_installs(settings)]:
cands += list(folder.glob("strata-*.json"))
cands += model_configs(folder)
cands = [c for c in dict.fromkeys(cands) if c.is_file()]
return max(cands, key=lambda p: p.stat().st_mtime) if cands else None

Expand Down Expand Up @@ -1608,7 +1614,7 @@ def find_in(roots: list, rel: str):

# ------------------------------------------------------------------------------------------------ start
def installed_configs():
return sorted(ROOT.glob("strata-*.json"), key=lambda p: p.stat().st_mtime, reverse=True)
return sorted(model_configs(ROOT), key=lambda p: p.stat().st_mtime, reverse=True)


def source_version() -> str:
Expand Down
63 changes: 63 additions & 0 deletions tools/test_setup_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Tests for the model configs setup.py lists: the server keeps a config's Chat settings next to it as
`strata-<model>.shared-settings.json`, which matches the configs' own `strata-*.json` but is not one (no exe, no
args), so the start menu and the "set up like the last one" lookup must leave it out.

python -m unittest tools.test_setup_configs
"""
from __future__ import annotations

import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tools"))
import setup # noqa: E402

CONFIG = {"exe": "engine/strata.exe", "args": ["--max-context", "131072"]}
SHARED = {"reasoning_effort": "high", "temperature": 0.6}


def write(folder: Path, name: str, data: dict) -> Path:
path = folder / name
path.write_text(json.dumps(data), encoding="utf-8")
return path


class ModelConfigs(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.dir = Path(tmp.name)
self.config = write(self.dir, "strata-iq3_s.json", CONFIG)
self.shared = write(self.dir, "strata-iq3_s.shared-settings.json", SHARED)
# previous_config also looks into the Strata folders next to this checkout: keep the real ones out
patch = mock.patch.object(setup, "other_installs", return_value=[])
patch.start()
self.addCleanup(patch.stop)

def test_the_start_menu_lists_the_config_but_not_its_shared_settings(self):
with mock.patch.object(setup, "ROOT", self.dir):
self.assertEqual(setup.installed_configs(), [self.config])

def test_the_last_config_of_another_install_is_never_its_shared_settings(self):
# the shared settings are written after the config (the first time Chat settings are saved), so they are
# the newest strata-*.json there
later = self.config.stat().st_mtime + 60
os.utime(self.shared, (later, later))
self.assertEqual(setup.previous_config([self.dir], {}), self.config)

def test_a_folder_with_only_shared_settings_has_no_config(self):
self.config.unlink()
with mock.patch.object(setup, "ROOT", self.dir):
self.assertEqual(setup.installed_configs(), [])
self.assertIsNone(setup.previous_config([self.dir], {}))


if __name__ == "__main__":
unittest.main()