fix(agent): persist the LLM confidence through metadata writes

The post-processor stored the LLM's confidence as `_llm_confidence`, but
that value could never be read back: `BaseModelMetadata.from_dict()`
deliberately excludes underscore-prefixed keys from `_unknown_fields` and
`to_dict()` strips private fields, so it was erased by the next metadata
write and was invisible to `read_metadata()`.  The enrichment evaluation
harness reads this field to score runs, so confidence was always scored
as blank.

Store it as `llm_confidence`, which round-trips as an ordinary unknown
field — the same mechanism `llm_enriched_at` already relies on.  Nothing
else consumed the old name, and the harness still accepts it so sidecars
written by earlier versions keep evaluating.

Covered by a metadata load/save round-trip regression test plus
assertions that the post-processor writes the persisted key and no longer
writes the private one.
This commit is contained in:
Will Miao
2026-09-14 20:42:14 +08:00
parent 4064ea7d3a
commit 51de85a6ca
4 changed files with 92 additions and 3 deletions
+42
View File
@@ -212,3 +212,45 @@ async def test_self_healed_sidecar_is_parseable(tmp_path) -> None:
assert metadata.file_name == "MyModel"
assert metadata.model_name == "My Model"
assert metadata.sha256 == "abc123"
@pytest.mark.asyncio
async def test_provenance_fields_survive_a_load_save_round_trip(tmp_path) -> None:
"""Non-underscore extras must persist; underscore keys are ephemeral.
Regression test for `llm_confidence`: it was written as `_llm_confidence`,
which `BaseModelMetadata.from_dict()` drops (and `to_dict()` strips), so the
value was erased by the next metadata write and was invisible to
`read_metadata()`. The enrichment evaluation harness depends on it.
"""
model_path = tmp_path / "Model.safetensors"
model_path.write_bytes(b"fake model data")
metadata_path = tmp_path / "Model.metadata.json"
payload = {
"file_path": str(model_path),
"file_name": "Model",
"model_name": "Model",
"sha256": "deadbeef",
"base_model": "Krea 2",
"preview_url": "",
"metadata_source": "agent:enrich_hf_metadata",
"llm_enriched_at": "2026-01-01T00:00:00+00:00",
"llm_confidence": "medium",
"_llm_confidence": "medium",
}
assert await MetadataManager.save_metadata(str(model_path), payload) is True
# A read must surface the supported key...
loaded = await MetadataManager.load_metadata_payload(str(model_path))
assert loaded["llm_confidence"] == "medium"
assert loaded["metadata_source"] == "agent:enrich_hf_metadata"
# ...but the underscore alias is intentionally not persisted.
assert "_llm_confidence" not in loaded
# Re-saving what we read must not lose the confidence.
assert await MetadataManager.save_metadata(str(model_path), loaded) is True
saved = json.loads(metadata_path.read_text(encoding="utf-8"))
assert saved["llm_confidence"] == "medium"
assert saved["llm_enriched_at"] == "2026-01-01T00:00:00+00:00"