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
+56 -14
View File
@@ -6,6 +6,7 @@ import math
import re
from pathlib import Path
from k1link.cache_budget import cache_publication
from k1link.simulation.ai_polygon.worlds import write_json
CLASSES = {
@@ -148,43 +149,84 @@ class UrbanMapStore:
raise ValueError("Invalid world identity")
return self.root / key
def store_immutable(self, world, document):
def _prepare(self, world, document):
validate_map(document, world)
raw = json.dumps(document, sort_keys=True, ensure_ascii=False, allow_nan=False).encode()
byte_limit = (32 if document.get("semantic_stage") == "raw" else 12) * 1024**2
if len(raw) > byte_limit:
raise ValueError("Infrastructure map exceeds size limit")
sha = hashlib.sha256(raw).hexdigest()
directory = self.directory(world)
directory.mkdir(parents=True, exist_ok=True)
path = directory / (sha + ".json")
return document, sha, len(raw)
def _existing(self, world, sha):
path = self.directory(world) / (sha + ".json")
if path.is_symlink():
raise ValueError("Invalid infrastructure link")
if path.exists():
self.get_version(world, sha)
else:
write_json(path, document)
return {"sha256": sha, "features": len(document["features"])}
return True
return False
def _write_immutable(self, world, document, sha):
directory = self.directory(world)
directory.mkdir(parents=True, exist_ok=True)
write_json(directory / (sha + ".json"), document)
def _publish(self, world, prepared, pointer=None):
# One admission for the complete pair and its temporary pointer. No
# eviction: old versions may still be the base of a manual edit.
missing = {}
same_pointer = False
target = self.directory(world) / "latest.json"
def reservation():
nonlocal missing, same_pointer
missing = {sha: (document, size) for document, sha, size in prepared
if not self._existing(world, sha)}
if target.is_symlink():
raise ValueError("Invalid infrastructure pointer")
try:
same_pointer = pointer is None or json.loads(target.read_text()) == pointer
except (FileNotFoundError, ValueError):
same_pointer = False
pointer_bytes = len(json.dumps(pointer).encode()) if not same_pointer else 0
return sum(size for _, size in missing.values()) + pointer_bytes
if reservation() == 0:
return # Exact retry remains available even at/above the budget.
with cache_publication(self.root, reservation, kind="urban-maps", max_files=10000):
# Recomputed inside the lock: concurrent identical retries reserve no
# duplicate bytes and do not rewrite an already published pointer.
for sha, (document, _) in missing.items():
self._write_immutable(world, document, sha)
if pointer is not None and not same_pointer:
write_json(target, pointer)
def store_immutable(self, world, document):
prepared = self._prepare(world, document)
self._publish(world, [prepared])
return {"sha256": prepared[1], "features": len(document["features"])}
def save(self, world, document):
if document.get("semantic_stage") == "raw":
raise ValueError("Raw observations cannot replace the algorithmic map")
result = self.store_immutable(world, document)
write_json(self.directory(world) / "latest.json", {"sha256": result["sha256"]})
prepared = self._prepare(world, document)
result = {"sha256": prepared[1], "features": len(document["features"])}
self._publish(world, [prepared], {"sha256": prepared[1]})
return result
def publish_pair(self, world, raw, algorithmic):
"""Publish a complete pair with one atomic pointer replacement."""
"""Admit the full pair, then publish through one atomic pointer."""
for document, stage in ((raw, "raw"), (algorithmic, "algorithmic")):
validate_map(document, world)
if document.get("semantic_stage") != stage:
raise ValueError("Incorrect semantic representation role")
if raw["source"] != algorithmic["source"]:
raise ValueError("Semantic representations must share their source")
raw_result = self.store_immutable(world, raw)
result = self.store_immutable(world, algorithmic)
pointer = {"sha256": result["sha256"], "raw_sha256": raw_result["sha256"]}
write_json(self.directory(world) / "latest.json", pointer)
raw_prepared = self._prepare(world, raw)
prepared = self._prepare(world, algorithmic)
pointer = {"sha256": prepared[1], "raw_sha256": raw_prepared[1]}
self._publish(world, [raw_prepared, prepared], pointer)
return pointer
def versions(self, world):