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
+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