Official Codebase of 3DCodeVerse
Python
brief: the disk cache key ignored spec.references, so --image reused a brief made without the image
gaoypengecc3b7766a3e
diff --git a/harness/codeverse/tracks/brief.py b/harness/codeverse/tracks/brief.pyindex d8724a19..b8f3836f 100644--- a/harness/codeverse/tracks/brief.py+++ b/harness/codeverse/tracks/brief.py@@ -51,12 +51,44 @@ def brief_cache_dir() -> Path:return Path(root) / "briefs"+def _reference_digest(spec: Spec) -> list[dict[str, str]]:+ """Identify the reference images by path AND content hash.++ ``--reference`` synthesises a NEW image per run under the same spec fields, so+ the path alone does not separate two runs; the bytes do. An unreadable file+ still contributes its path, so it can never collapse onto "no references"."""+ out: list[dict[str, str]] = []+ for r in spec.references:+ try:+ digest = _sha256_file(Path(r.path))+ except OSError:+ digest = "(unreadable)"+ out.append({"path": r.path, "role": r.role, "note": r.note, "sha256": digest})+ return out+++def _sha256_file(path: Path) -> str:+ h = hashlib.sha256()+ with path.open("rb") as fh:+ for chunk in iter(lambda: fh.read(1 << 20), b""):+ h.update(chunk)+ return h.hexdigest()++def brief_cache_key(spec: Spec, model_id: str) -> str:+ """Every input to the call must be in the key.++ ``expand_brief`` attaches ``spec.references`` to the request and tells the model+ to read the dimensions and features off them, so a key that omits them lets an+ ``--image`` run drink a brief generated WITHOUT the image (and vice versa) —+ silently, from the machine-wide ~/.cache/codeverse/briefs, with cached=True in+ events.jsonl."""c = spec.constraintspayload = json.dumps({"prompt": spec.prompt.strip(), "track": spec.track.value, "language": spec.language.value,"must_have": list(c.must_have), "must_not": list(c.must_not), "style": c.style,"dimensions_m": c.dimensions_m or {}, "model": model_id,+ "references": _reference_digest(spec),"template": prompt_hash(load_text(BRIEF_TEMPLATE)),}, sort_keys=True)return hashlib.sha256(payload.encode()).hexdigest()[:20]diff --git a/harness/tests/orchestrator_tracks/test_planner_depth.py b/harness/tests/orchestrator_tracks/test_planner_depth.pyindex 206434ee..3d833f55 100644--- a/harness/tests/orchestrator_tracks/test_planner_depth.py+++ b/harness/tests/orchestrator_tracks/test_planner_depth.py@@ -10,6 +10,7 @@ import jsonimport pytestfrom pydantic import ValidationError+from codeverse.contracts.chat import ImagePartfrom codeverse.contracts.common import Backends, Language, Trackfrom codeverse.contracts.plan import (SUBPART_REL_SLACK,@@ -22,7 +23,7 @@ from codeverse.contracts.plan import (SubAssembly,SubPartPlan,)-from codeverse.contracts.spec import Constraints, Spec+from codeverse.contracts.spec import Constraints, ReferenceImage, Specfrom codeverse.tracks import brief as BRfrom codeverse.tracks import plan_budget as Bfrom codeverse.tracks.planner import (@@ -272,6 +273,53 @@ def test_brief_is_cached_by_prompt_hash_and_the_second_call_is_free(tmp_path):assert calls["n"] == 2+def test_reference_images_are_part_of_the_brief_cache_key(tmp_path):+ """RS-4: expand_brief attaches spec.references and tells the model to read the+ dimensions off them, so a key that omits them let an --image run silently+ reuse a brief generated WITHOUT the image (and vice versa)."""+ img = tmp_path / "ref.png"+ img.write_bytes(b"\x89PNG\r\n\x1a\n-first-")+ plain = _spec()+ withref = plain.model_copy(update={"references": [ReferenceImage(path=str(img))]})++ assert BR.brief_cache_key(plain, "m") != BR.brief_cache_key(withref, "m")++ # --reference synthesises a NEW image per run under identical spec fields:+ # the path alone does not separate the two runs, the bytes must.+ before = BR.brief_cache_key(withref, "m")+ img.write_bytes(b"\x89PNG\r\n\x1a\n-second-")+ assert BR.brief_cache_key(withref, "m") != before++ # ... and the role/note that go into the image label matter too+ other = plain.model_copy(update={"references": [ReferenceImage(path=str(img), role="style")]})+ assert BR.brief_cache_key(other, "m") != BR.brief_cache_key(withref, "m")+++def test_an_image_run_does_not_drink_the_no_image_brief(tmp_path):+ """The end-to-end shape of RS-4: two expand_brief calls, one model call each."""+ cache = tmp_path / "cache"+ img = tmp_path / "ref.png"+ img.write_bytes(b"\x89PNG\r\n\x1a\n-bytes-")+ calls: list[int] = []++ def responder(req):+ calls.append(sum(1 for part in req.messages[0].parts if isinstance(part, ImagePart)))+ return json.loads(_brief().model_dump_json())++ model = FakeChatModel(responder)+ plain = _spec()+ withref = plain.model_copy(update={"references": [ReferenceImage(path=str(img))]})+ BR.expand_brief(plain, "fake:planner", model=model, cache_dir=cache)+ BR.expand_brief(withref, "fake:planner", model=model, cache_dir=cache)++ assert calls == [0, 1] # the reference run made its own, grounded call+++def test_an_unreadable_reference_never_collapses_onto_no_references(tmp_path):+ missing = _spec().model_copy(update={"references": [ReferenceImage(path=str(tmp_path / "gone.png"))]})+ assert BR.brief_cache_key(missing, "m") != BR.brief_cache_key(_spec(), "m")++def test_brief_failure_is_never_fatal(tmp_path):def boom(req):raise RuntimeError("503 storm")