write the learned verdict to disk, not just to memory

record_resident_fits called save_models(), which writes the config manager's
own view of models.json -- a view that never saw this change. Verified live
2026-10-29: after a run that learned the model does not fit, the flag was set
in memory (the next load in the same process correctly skipped the resident
attempt, 110.6s instead of 865.3s) while models.json still read

    measured_resident_fits = None

so every restart would have paid the ~16 minute discovery again.

persist_model_field is the right call and is what record_vram_delta already
uses for measured_vram_gb: it re-reads models.json from disk, sets one field
and writes back, so a stale in-memory copy cannot revert a UI edit made
meanwhile. It also targets the entry by config_id, so two configs sharing one
checkpoint path do not overwrite each other's measurement.
Co-Authored-By: 's avatarClaude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014T3rZozi1Ehk96NG1WesgC
parent ecf9bbec
......@@ -3863,10 +3863,20 @@ class MultiModelManager:
self.config[canon] = working
from codai.admin.routes import config_manager
if config_manager is not None:
# persist_model_field, NOT save_models(): the latter writes the
# config manager's own view and never saw this change, so the
# value lived in memory only and every restart paid the
# discovery again. It also targets the entry by config_id, so
# two configs sharing one checkpoint path do not overwrite each
# other's measurement -- the same reason record_vram_delta uses
# it for measured_vram_gb.
try:
config_manager.save_models()
except Exception:
pass
config_manager.persist_model_field(
canon, "measured_resident_fits", bool(fits),
config_id=cfg.get("config_id"))
except Exception as _pe:
print(f" Warning: could not persist "
f"measured_resident_fits: {_pe}")
print(f" Learned: '{model_key}' "
f"{'fits' if fits else 'does NOT fit'} fully on the GPU "
f"(measured_resident_fits={bool(fits)})")
......
......@@ -106,3 +106,45 @@ def test_the_loader_consults_and_records():
"both outcomes must be recorded: writing only the failure means a "
"model that fits is re-proven every time, and writing only the success "
"never stops the expensive retry")
def test_the_verdict_reaches_disk_not_just_memory(monkeypatch):
"""It must survive a restart.
Written with save_models() the value never reached models.json at all --
that writes the config manager's own view, which never saw the change --
so the flag lived in memory only and every restart paid the ~16 minute
discovery again. Measured 2026-10-29: after a run that learned the model
does not fit, models.json still read measured_resident_fits = None.
persist_model_field also targets the entry by config_id, so two configs
sharing one checkpoint path cannot overwrite each other's measurement.
"""
import os as _os
root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
src = open(_os.path.join(root, "codai/models/manager.py"), encoding="utf-8").read()
after = src.split("def record_resident_fits", 1)[1]
# Stop at the next method, or the window spills into neighbouring code and
# the assertions below test the wrong function.
block = after.split("\n def ", 1)[0]
assert "persist_model_field(" in block, (
"the learned verdict is not written to models.json, so it is forgotten "
"on every restart")
assert "config_id=" in block, (
"without config_id, two configs sharing a checkpoint path overwrite "
"each other's measurement")
# Ignore comment lines: the code explains WHY save_models is wrong, so a
# naive substring search matches that explanation.
code = "\n".join(l for l in block.splitlines()
if not l.strip().startswith("#"))
assert "save_models()" not in code, (
"save_models() writes a stale whole-file view and can revert UI edits")
def test_it_is_recorded_through_the_manager_api():
"""The loader must not reach into models.json itself."""
import os as _os
root = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
src = open(_os.path.join(root, "codai/api/video.py"), encoding="utf-8").read()
block = src.split("def _record_resident_fits", 1)[1][:600]
assert "multi_model_manager.record_resident_fits" in block
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment