From 15047016b934e0545a0cbd791e2cce9752dd7049 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Wed, 18 Mar 2026 22:55:38 +0100 Subject: [PATCH] Migrate legacy LoRA format at JSON load time Run _migrate_lora_keys on every load_json call so all sequences get split name/strength keys immediately, not just when the LoRA expansion is rendered. Next save writes the clean format to disk. Co-Authored-By: Claude Opus 4.6 --- utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils.py b/utils.py index 3905898..d8d1083 100644 --- a/utils.py +++ b/utils.py @@ -144,6 +144,32 @@ def save_snippets(snippets): json.dump(snippets, f, indent=4) os.replace(tmp, SNIPPETS_FILE) +def _migrate_lora_keys(data: dict) -> None: + """Split legacy values into separate name/strength keys in-place.""" + for item in data.get(KEY_BATCH_DATA, []): + if not isinstance(item, dict): + continue + for idx in range(1, 4): + for tier in ('high', 'low'): + name_key = f'lora {idx} {tier}' + str_key = f'lora {idx} {tier} strength' + raw = str(item.get(name_key, '')) + if raw.startswith('', '') + if ':' in inner: + parts = inner.rsplit(':', 1) + item[name_key] = parts[0] + try: + item[str_key] = float(parts[1]) + except ValueError: + item.setdefault(str_key, 1.0) + else: + item[name_key] = inner + item.setdefault(str_key, 1.0) + elif name_key in item and str_key not in item: + item[str_key] = 1.0 + + def load_json(path: str | Path) -> tuple[dict[str, Any], float]: path = Path(path) if not path.exists(): @@ -151,6 +177,7 @@ def load_json(path: str | Path) -> tuple[dict[str, Any], float]: try: with open(path, 'r') as f: data = json.load(f) + _migrate_lora_keys(data) return data, path.stat().st_mtime except Exception as e: logger.error(f"Error loading JSON: {e}")