[AZ-305] c6 PostgresFilesystemStore: TileStore + TileMetadataStore impl

Adds the production PostgresFilesystemStore implementing both protocols
in a single class. Filesystem-backed JPEG I/O (atomic sidecar write,
read-only mmap) + Postgres-backed metadata (spatial bbox, LRU, voting,
upload bookkeeping). Wires composition via `from_config` classmethod.

Key behaviors:
- AC-3 strict reading: INSERT runs first inside an open transaction;
  duplicate-key collisions raise `TileMetadataError` BEFORE any byte is
  written, leaving the original file + sidecar byte-identical. Atomic
  sidecar write happens inside the same transaction; commit closes it.
  Comp-delete remains as a safety net for the rare commit-after-write
  failure path.
- AC-2 content-hash gate runs before any I/O.
- Construction performs an orphan-file reconciliation scan and emits an
  INFO `c6.store.construct` log with steady-state stats.

Adds `c6.write` and `c6.write_failed` FDR record kinds (schema v1.1.0,
forward-compatible) and a thin operator CLI at
`c6_tile_cache.tools dump` for inspection.

Dependencies: adds `psycopg-pool>=3.2,<4.0` for the connection pool used
on the F3 read-hot path.

Tests: 25 new tests for c6_tile_cache cover AC-1..AC-15 plus
MmapTilePixelHandle + helper round-trips. Full Tier-2 unit suite passes
(1215 passed, 8 skipped, 1 pre-existing unrelated failure
`test_ac8_read_host_tuple_on_jetson` — missing `pynvml` on macOS,
Jetson-only).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-12 18:01:50 +03:00
parent bf33b94260
commit d1c1cd9ab4
14 changed files with 2382 additions and 18 deletions
@@ -52,7 +52,7 @@ def _is_build_flag_on(flag_name: str) -> bool:
return raw.strip().lower() in {"on", "1", "true", "yes"}
def _c6_config(config: "Config") -> "C6TileCacheConfig":
def _c6_config(config: Config) -> C6TileCacheConfig:
"""Pull the registered C6 config block.
``c6_tile_cache.__init__`` registers it on import; if the package
@@ -62,18 +62,21 @@ def _c6_config(config: "Config") -> "C6TileCacheConfig":
return config.components["c6_tile_cache"]
def build_tile_store(config: "Config") -> "TileStore":
def build_tile_store(config: Config) -> TileStore:
"""Construct the :class:`TileStore` impl selected by config.
Today only ``"postgres_filesystem"`` is wired; the runtime label
is validated at config-load time so unknown labels never reach
here. Concrete impl is produced by AZ-305.
here. Concrete impl produced by AZ-305 — the constructor is
invoked via ``PostgresFilesystemStore.from_config(config)`` which
wires the ``ConnectionPool`` / ``FdrClient`` / logger / static
helper dependencies from the config block.
"""
block = _c6_config(config)
runtime = block.store_runtime
if runtime == "postgres_filesystem":
try:
from gps_denied_onboard.components.c6_tile_cache.postgres_filesystem_store import ( # noqa: PLC0415
from gps_denied_onboard.components.c6_tile_cache.postgres_filesystem_store import (
PostgresFilesystemStore,
)
except ModuleNotFoundError as exc:
@@ -83,13 +86,13 @@ def build_tile_store(config: "Config") -> "TileStore":
"'c6_tile_cache.postgres_filesystem_store' has not been "
"built into this binary yet (AZ-305 pending)."
) from exc
return PostgresFilesystemStore(config)
return PostgresFilesystemStore.from_config(config)
raise RuntimeNotAvailableError(
f"TileStore runtime {runtime!r} is not buildable in this binary."
)
def build_tile_metadata_store(config: "Config") -> "TileMetadataStore":
def build_tile_metadata_store(config: Config) -> TileMetadataStore:
"""Construct the :class:`TileMetadataStore` impl selected by config.
Today the same ``PostgresFilesystemStore`` class implements both
@@ -102,7 +105,7 @@ def build_tile_metadata_store(config: "Config") -> "TileMetadataStore":
runtime = block.metadata_runtime
if runtime == "postgres_filesystem":
try:
from gps_denied_onboard.components.c6_tile_cache.postgres_filesystem_store import ( # noqa: PLC0415
from gps_denied_onboard.components.c6_tile_cache.postgres_filesystem_store import (
PostgresFilesystemStore,
)
except ModuleNotFoundError as exc:
@@ -112,13 +115,13 @@ def build_tile_metadata_store(config: "Config") -> "TileMetadataStore":
"'c6_tile_cache.postgres_filesystem_store' has not been "
"built into this binary yet (AZ-305 pending)."
) from exc
return PostgresFilesystemStore(config)
return PostgresFilesystemStore.from_config(config)
raise RuntimeNotAvailableError(
f"TileMetadataStore runtime {runtime!r} is not buildable in this binary."
)
def build_descriptor_index(config: "Config") -> "DescriptorIndex":
def build_descriptor_index(config: Config) -> DescriptorIndex:
"""Construct the :class:`DescriptorIndex` impl selected by config.
Gated by ``BUILD_FAISS_INDEX``: if the flag is OFF, the concrete
@@ -134,7 +137,7 @@ def build_descriptor_index(config: "Config") -> "DescriptorIndex":
"BUILD_FAISS_INDEX=ON in this binary; the flag is OFF."
)
try:
from gps_denied_onboard.components.c6_tile_cache.faiss_descriptor_index import ( # noqa: PLC0415
from gps_denied_onboard.components.c6_tile_cache.faiss_descriptor_index import (
FaissDescriptorIndex,
)
except ModuleNotFoundError as exc: