mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-06 08:18:54 +00:00
serve: one unknown key in .shared-settings.json no longer clears the rest (#4933)
`clean_shared_defaults` is the strict reader behind `POST /settings`, where a
client can be told which key is wrong. The start-up path called the same reader
and caught its ValueError with `svc.shared = {}`: a single unknown key - a typo,
a key from a newer version - discarded every OTHER setting in the file,
`reasoning_effort` included, with nothing in the log.
`load_shared_settings` reads the file key by key at start-up: a key the server
understands keeps applying, a key it does not is named on one line and skipped.
A file that cannot be parsed, or is not a JSON object, is still ignored as a whole
and now says so; a missing file stays quiet, as before.
`POST /settings` is unchanged and still answers 400 naming the key.
This commit is contained in:
1 parent
c62d5e3a15
commit
15ae1ea329
2 files changed
+97
-7
No files matched your search
+35
-7
@@ -8470,6 +8470,37 @@ SHARED_KEYS = ("reasoning_effort", "reasoning_budget_tokens", "temperature", "to
|
||||
"experimental_speed_projection")
|
||||
|
||||
|
||||
def load_shared_settings(path, warn=print) -> dict:
|
||||
"""The Chat settings kept next to a config between starts, read so that one bad key cannot erase the others.
|
||||
|
||||
The strict reader below belongs to POST /settings, where a client is there to be told which key is wrong and can
|
||||
fix it. At start-up nobody is there: the old code called the same strict reader and caught its ValueError with
|
||||
`svc.shared = {}`, so a single unknown key - a typo, a key from a newer version, or a setting the server does not
|
||||
know - discarded every OTHER setting in the file, `reasoning_effort` included, without a word in the log (#4933).
|
||||
|
||||
So the two failures are told apart here: a file that cannot be read, or is not a JSON object, is still ignored as
|
||||
a whole (nothing in it can be trusted); a file that IS an object keeps every key the server understands, and each
|
||||
key it does not is named on one line and skipped."""
|
||||
try:
|
||||
doc = json.loads(Path(path).read_text(encoding="utf-8"))
|
||||
except FileNotFoundError:
|
||||
return {} # no file yet: the normal case on a first start, nothing to report
|
||||
except (OSError, ValueError) as e:
|
||||
warn(f"[strata] the Chat settings in {path} are ignored ({e}); other apps use their own settings")
|
||||
return {}
|
||||
if not isinstance(doc, dict):
|
||||
warn(f"[strata] the Chat settings in {path} are ignored (a JSON object was expected, found "
|
||||
f"{type(doc).__name__}); other apps use their own settings")
|
||||
return {}
|
||||
out = {}
|
||||
for key, value in doc.items():
|
||||
try:
|
||||
out.update(clean_shared_defaults({key: value}))
|
||||
except ValueError as e:
|
||||
warn(f"[strata] one Chat setting in {path} is skipped ({e}); the others still apply")
|
||||
return out
|
||||
|
||||
|
||||
def clean_shared_defaults(d) -> dict:
|
||||
"""The Chat settings other apps get (POST /settings): only known keys, each checked; ValueError names a bad one."""
|
||||
if d is None:
|
||||
@@ -9467,13 +9498,10 @@ def main(argv=None, *, lifecycle_event=None, interrupt_exit_code: int = 0) -> in
|
||||
svc.config_path = a.config # #564: the web page's Settings view
|
||||
if a.config: # the Chat settings shared with other apps, from last time
|
||||
svc.shared_path = str(Path(a.config).with_suffix("")) + ".shared-settings.json"
|
||||
try:
|
||||
svc.shared = clean_shared_defaults(json.loads(Path(svc.shared_path).read_text(encoding="utf-8")))
|
||||
if svc.shared:
|
||||
print("[strata] other apps use the Chat settings: " +
|
||||
", ".join(f"{k}={v}" for k, v in svc.shared.items()), flush=True)
|
||||
except (OSError, ValueError):
|
||||
svc.shared = {}
|
||||
svc.shared = load_shared_settings(svc.shared_path) # #4933: one unknown key no longer clears the rest
|
||||
if svc.shared:
|
||||
print("[strata] other apps use the Chat settings: " +
|
||||
", ".join(f"{k}={v}" for k, v in svc.shared.items()), flush=True)
|
||||
if hub is not None:
|
||||
import atexit
|
||||
svc.mcp = hub
|
||||
|
||||
@@ -3276,6 +3276,68 @@ class ConfigReasoningEffort(unittest.TestCase):
|
||||
self.assertEqual(cfg, {"args": ["--ctx", "4096"]})
|
||||
|
||||
|
||||
class LoadSharedSettings(unittest.TestCase):
|
||||
"""#4933: one unknown key in `.shared-settings.json` used to clear EVERY shared setting, silently.
|
||||
|
||||
The strict reader stays for POST /settings, where a client can be told which key is wrong. At start-up the file
|
||||
is read key by key instead: the keys the server understands keep applying and each one it does not is named and
|
||||
skipped. A file that cannot be parsed, or is not an object, is still ignored as a whole."""
|
||||
|
||||
def load(self, text):
|
||||
from serve.server import load_shared_settings
|
||||
d = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, d, True)
|
||||
path = os.path.join(d, "x.shared-settings.json")
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
said = []
|
||||
got = load_shared_settings(path, warn=said.append)
|
||||
return got, said
|
||||
|
||||
def test_two_known_keys_are_both_kept(self):
|
||||
got, said = self.load('{"reasoning_effort": "medium", "top_p": 0.9}')
|
||||
self.assertEqual(got, {"reasoning_effort": "medium", "top_p": 0.9})
|
||||
self.assertEqual(said, [])
|
||||
|
||||
def test_an_unknown_key_no_longer_clears_the_known_ones(self):
|
||||
# A made-up key, and not `preserve_thinking`: #4932 teaches the server that name, so on a tree carrying both
|
||||
# changes this example would be a KNOWN key and the test would pass without exercising anything.
|
||||
got, said = self.load('{"reasoning_effort": "medium", "preserve_thoughts": false}')
|
||||
self.assertEqual(got, {"reasoning_effort": "medium"})
|
||||
self.assertEqual(len(said), 1)
|
||||
self.assertIn("preserve_thoughts", said[0])
|
||||
self.assertIn("skipped", said[0])
|
||||
|
||||
def test_a_known_key_with_a_bad_value_is_named_and_skipped(self):
|
||||
got, said = self.load('{"reasoning_effort": "medium", "top_p": 5}')
|
||||
self.assertEqual(got, {"reasoning_effort": "medium"})
|
||||
self.assertIn("top_p", said[0])
|
||||
|
||||
def test_several_bad_keys_are_each_named(self):
|
||||
got, said = self.load('{"reasoning_effort": "medium", "nope": 1, "top_k": 900}')
|
||||
self.assertEqual(got, {"reasoning_effort": "medium"})
|
||||
self.assertEqual(len(said), 2)
|
||||
|
||||
def test_an_unreadable_file_is_ignored_whole_and_says_so(self):
|
||||
got, said = self.load('{not json')
|
||||
self.assertEqual(got, {})
|
||||
self.assertEqual(len(said), 1)
|
||||
self.assertIn("ignored", said[0])
|
||||
|
||||
def test_a_non_object_is_ignored_whole_and_says_so(self):
|
||||
got, said = self.load('["reasoning_effort"]')
|
||||
self.assertEqual(got, {})
|
||||
self.assertEqual(len(said), 1)
|
||||
self.assertIn("ignored", said[0])
|
||||
|
||||
def test_a_missing_file_is_quiet(self):
|
||||
from serve.server import load_shared_settings
|
||||
said = []
|
||||
self.assertEqual(load_shared_settings(os.path.join(tempfile.gettempdir(), "no-such-file-4933.json"),
|
||||
warn=said.append), {})
|
||||
self.assertEqual(said, []) # no file is the normal case: nothing to report
|
||||
|
||||
|
||||
class JoinedEngineArgs(unittest.TestCase):
|
||||
"""#2267: "--ple-io mmap" written as one element of the config's args reached the engine as one argument."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user