"""Dev-only lever overrides for same-seed A/B renders (overhaul Phase 1, CP-07). Not a user feature and no widget: the module constants below are research levers, and flipping one used to mean an edit plus a ComfyUI restart per arm. A local JSON file, `/default/htc_levers.json` (outside git), is read at the start of every run; every lever is first put back to its shipped value, then the file's entries are applied, so deleting the file restores the shipped behaviour on the next run. {"h3_ref_chain.WT_PIN_MODE": "tail5", "latents.CARRY_STRENGTH": 0.5} Each applied override is logged as `[HandTieClips] lever override: NAME = value`. """ from __future__ import annotations import json import os TAG = "HandTieClips" FILE_NAME = "htc_levers.json" # module short name -> {constant: kind} ALLOWED = { "h3_ref_chain": { "DIALOGUE_CONT_NO_AUDIO_CARRY": "bool", "WT_PIN_MODE": ("full", "tail5"), "WT_ANCHOR_LATE_PROG": "float", "WT_RETAKE_SEQUENTIAL": "bool", "SWAP_VIDEO_FADE": "pair_or_none", "REF_AUDIO_ROPE_GAP": "float", "VOICE_FRAME_ALIGN": "bool", "VOICE_TRIM_S": "float", "AUTO_TIMBRE_WORDS": "bool", "VIT_CACHE": "bool", "MC_KEYSTONE_PROG": "float", "MC_FREQ_FORCING": "bool", "MC_FF_W_HI": "float", "MC_FF_UNTIL": "float", "MC_TRFLOW_SIGMA": "float", "TONE_ANCHOR_CARRY": "bool", "HT_V2": "bool", "HT_V2_TAIL_PROG": "float", "SWAP_CHUNKED": "bool", "SWAP_CHUNK_FRAMES": "float", "SWAP_CHUNK_OVERLAP": "float", "SWAP_CHUNK_TONE": "bool", "SWAP_CHUNK_TAIL5": "bool", "SWAP_BG_MASK": "bool", "SWAP_BG_BACKEND": ("auto_rembg", "auto_sam"), }, "refmod": { "REFMOD_VROW_CURVE": "bool", }, "cond_store": { "DISK_COND_CACHE": "bool", }, "latents": { "CARRY_STRENGTH": "float", "CARRY_MATCH_STD": "bool", }, } _SHIPPED: dict[str, object] = {} def path(): # No environment switch here (the registry scanner rejects env reads): move # the lever file aside before running the offline checks beside an A/B. try: import folder_paths # noqa: PLC0415 base = folder_paths.get_user_directory() except Exception: # noqa: BLE001 return None return os.path.join(base, "default", FILE_NAME) def _coerce(kind, value): if kind == "bool": if not isinstance(value, bool): raise ValueError("expects true/false") return value if kind == "float": return float(value) if kind == "pair_or_none": if value is None: return None a, b = value return (float(a), float(b)) if isinstance(kind, tuple): if value not in kind: raise ValueError(f"expects one of {list(kind)}") return value raise ValueError(f"unknown kind {kind!r}") def apply(modules, file_path=None): """Reset every allowed lever on `modules` (`{short name: module}`) to its shipped value, then apply the override file. -> `[(name, value)]` applied.""" for short, names in ALLOWED.items(): mod = modules.get(short) if mod is None: continue for name in names: key = f"{short}.{name}" if key not in _SHIPPED and hasattr(mod, name): _SHIPPED[key] = getattr(mod, name) if key in _SHIPPED: setattr(mod, name, _SHIPPED[key]) fp = file_path or path() if not fp or not os.path.isfile(fp): return [] try: with open(fp, encoding="utf-8") as fh: data = json.load(fh) except Exception as exc: # noqa: BLE001 print(f"[{TAG}] lever override: {fp} unreadable ({exc!r}); shipped values", flush=True) return [] out = [] for key, value in (data or {}).items(): short, _, name = str(key).partition(".") kind = ALLOWED.get(short, {}).get(name) mod = modules.get(short) if kind is None or mod is None: print(f"[{TAG}] lever override: {key} is not an A/B lever; ignored", flush=True) continue try: v = _coerce(kind, value) except (TypeError, ValueError) as exc: print(f"[{TAG}] lever override: {key} = {value!r} rejected ({exc}); shipped value", flush=True) continue setattr(mod, name, v) out.append((key, v)) print(f"[{TAG}] lever override: {key} = {v!r} (shipped {_SHIPPED.get(key)!r})", flush=True) return out