Bound semantic map publication and define experiment storage closeout

This commit is contained in:
DCCONSTRUCTIONS
2026-11-07 17:31:06 +03:00
parent 09c13a5c68
commit 43148a8769
6 changed files with 215 additions and 23 deletions
+5 -5
View File
@@ -100,18 +100,18 @@ def test_pair_failure_preserves_pointer_and_ordinary_save_clears_raw(tmp_path, m
world, model, maps, _, _ = make_setup(tmp_path)
raw, auto = pair(model)
previous = maps.versions(world)
original = maps.store_immutable
original = maps._write_immutable
def fail_second(world, document):
def fail_second(world, document, sha):
if document.get("semantic_stage") == "algorithmic":
raise OSError("interrupted")
return original(world, document)
return original(world, document, sha)
monkeypatch.setattr(maps, "store_immutable", fail_second)
monkeypatch.setattr(maps, "_write_immutable", fail_second)
with pytest.raises(OSError):
maps.publish_pair(world, raw, auto)
assert maps.versions(world) == previous
monkeypatch.setattr(maps, "store_immutable", original)
monkeypatch.setattr(maps, "_write_immutable", original)
maps.publish_pair(world, raw, auto)
maps.save(world, model)
assert "raw_sha256" not in maps.versions(world)
+67
View File
@@ -146,3 +146,70 @@ def test_vector_lines_roundtrip_and_admission(tmp_path):
change(bad)
with pytest.raises(ValueError):
validate_map(bad, world)
def test_map_budget_is_global_and_preserves_existing_versions(tmp_path, monkeypatch):
from k1link import cache_budget
world = {"sha256": "a" * 64}
other = {"sha256": "b" * 64}
store = UrbanMapStore(tmp_path)
first = store.save(world, document(world))
before = {p.relative_to(store.root).as_posix(): p.read_bytes()
for p in store.root.rglob('*.json')}
monkeypatch.setitem(cache_budget.LIMITS, "urban-maps", 1)
assert store.save(world, document(world)) == first # Exact retry at capacity.
assert store.get(world) == document(world)
with pytest.raises(cache_budget.CacheCapacityError):
store.save(other, document(other))
after = {p.relative_to(store.root).as_posix(): p.read_bytes()
for p in store.root.rglob('*.json')}
assert after == before
def test_pair_budget_checks_both_maps_before_first_write(tmp_path, monkeypatch):
from k1link import cache_budget
world = {"sha256": "a" * 64}
store = UrbanMapStore(tmp_path)
store.save(world, document(world))
raw, auto = document(world), document(world)
raw['semantic_stage'], auto['semantic_stage'] = 'raw', 'algorithmic'
before = {p.relative_to(store.root).as_posix(): p.read_bytes()
for p in store.root.rglob('*.json')}
# There is room for raw alone, not the complete atomic publication.
used = cache_budget.usage(store.root)[0]
monkeypatch.setitem(cache_budget.LIMITS, "urban-maps", used + len(json.dumps(raw).encode()) + 1)
with pytest.raises(cache_budget.CacheCapacityError):
store.publish_pair(world, raw, auto)
assert {p.relative_to(store.root).as_posix(): p.read_bytes()
for p in store.root.rglob('*.json')} == before
def test_concurrent_identical_publications_recheck_reserved_bytes(tmp_path, monkeypatch):
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from threading import Barrier
from k1link import cache_budget
from k1link.simulation.ai_polygon import urban_maps
world = {"sha256": "a" * 64}
store = UrbanMapStore(tmp_path)
data, sha, size = store._prepare(world, document(world))
monkeypatch.setitem(cache_budget.LIMITS, "urban-maps", size + len(json.dumps({'sha256': sha})))
barrier = Barrier(2)
publish = urban_maps.cache_publication
@contextmanager
def together(*args, **kwargs):
barrier.wait(timeout=2) # Both callers observed missing files before locking.
with publish(*args, **kwargs):
yield
monkeypatch.setattr(urban_maps, "cache_publication", together)
with ThreadPoolExecutor(max_workers=2) as pool:
results = list(pool.map(lambda _: store.save(world, data), range(2)))
assert results[0] == results[1]
assert store.get(world) == data
assert len(list(store.root.rglob('*.json'))) == 2