|
| 1 | +"""GraphCodec — pluggable serialization for graph snapshots (Phase 8.6). |
| 2 | +
|
| 3 | +The codec interface decouples snapshot format from graph internals. |
| 4 | +Default is JSON (current behavior). DAG-CBOR and compressed variants |
| 5 | +ship as optional codecs. |
| 6 | +
|
| 7 | +Tiered representation: |
| 8 | + HOT — Python objects (live propagation, no codec involved) |
| 9 | + WARM — DAG-CBOR in-memory buffer (lazy hydration, delta checkpoints) |
| 10 | + COLD — Arrow/Parquet (bulk storage, ML pipelines, archival) |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import contextlib |
| 16 | +import json |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Any, Protocol, runtime_checkable |
| 19 | + |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +# Core codec interface |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +@runtime_checkable |
| 26 | +class GraphCodec(Protocol): |
| 27 | + """Encode/decode graph snapshots to/from binary. |
| 28 | +
|
| 29 | + Implementations must be deterministic: ``encode(x)`` always produces |
| 30 | + the same bytes for the same input. |
| 31 | + """ |
| 32 | + |
| 33 | + @property |
| 34 | + def content_type(self) -> str: |
| 35 | + """MIME-like content type identifier (e.g. ``application/dag-cbor+zstd``).""" |
| 36 | + ... |
| 37 | + |
| 38 | + @property |
| 39 | + def name(self) -> str: |
| 40 | + """Human-readable name for diagnostics.""" |
| 41 | + ... |
| 42 | + |
| 43 | + def encode(self, snapshot: dict[str, Any]) -> bytes: |
| 44 | + """Encode a snapshot to binary.""" |
| 45 | + ... |
| 46 | + |
| 47 | + def decode(self, buffer: bytes) -> dict[str, Any]: |
| 48 | + """Decode binary back to a snapshot.""" |
| 49 | + ... |
| 50 | + |
| 51 | + |
| 52 | +@runtime_checkable |
| 53 | +class LazyGraphCodec(GraphCodec, Protocol): |
| 54 | + """Extended codec that supports lazy (on-demand) node decoding.""" |
| 55 | + |
| 56 | + def decode_lazy(self, buffer: bytes) -> dict[str, Any]: |
| 57 | + """Decode envelope and topology; defer node value decoding to access time.""" |
| 58 | + ... |
| 59 | + |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Delta checkpoint types (requires V0 — Phase 6.0) |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | + |
| 65 | + |
| 66 | +@dataclass(frozen=True, slots=True) |
| 67 | +class DeltaCheckpoint: |
| 68 | + """A delta checkpoint: only the nodes that changed since last checkpoint.""" |
| 69 | + |
| 70 | + seq: int |
| 71 | + name: str |
| 72 | + base_seq: int |
| 73 | + nodes: dict[str, dict[str, Any]] |
| 74 | + removed: tuple[str, ...] |
| 75 | + edges_added: tuple[dict[str, str], ...] |
| 76 | + edges_removed: tuple[dict[str, str], ...] |
| 77 | + timestamp_ns: int |
| 78 | + |
| 79 | + |
| 80 | +@dataclass(frozen=True, slots=True) |
| 81 | +class WALEntry: |
| 82 | + """WAL entry: either a full snapshot or a delta.""" |
| 83 | + |
| 84 | + type: str # "full" | "delta" |
| 85 | + snapshot: dict[str, Any] | None = None |
| 86 | + delta: DeltaCheckpoint | None = None |
| 87 | + seq: int = 0 |
| 88 | + |
| 89 | + |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | +# Eviction policy |
| 92 | +# --------------------------------------------------------------------------- |
| 93 | + |
| 94 | + |
| 95 | +@dataclass(frozen=True, slots=True) |
| 96 | +class EvictionPolicy: |
| 97 | + """Policy for evicting dormant subgraphs to reduce memory.""" |
| 98 | + |
| 99 | + idle_timeout_ms: int |
| 100 | + codec: GraphCodec | None = None |
| 101 | + |
| 102 | + |
| 103 | +@dataclass(frozen=True, slots=True) |
| 104 | +class EvictedSubgraphInfo: |
| 105 | + """Metadata about an evicted subgraph, exposed via describe().""" |
| 106 | + |
| 107 | + evicted: bool |
| 108 | + last_active_ns: int |
| 109 | + serialized_bytes: int |
| 110 | + codec_name: str |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- |
| 114 | +# JSON codec (default) |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | + |
| 117 | + |
| 118 | +class JsonCodec: |
| 119 | + """Default JSON codec. Wraps ``json.dumps``/``json.loads`` with |
| 120 | + deterministic key ordering. |
| 121 | + """ |
| 122 | + |
| 123 | + @property |
| 124 | + def content_type(self) -> str: |
| 125 | + return "application/json" |
| 126 | + |
| 127 | + @property |
| 128 | + def name(self) -> str: |
| 129 | + return "json" |
| 130 | + |
| 131 | + def encode(self, snapshot: dict[str, Any]) -> bytes: |
| 132 | + return json.dumps(snapshot, sort_keys=True, separators=(",", ":")).encode("utf-8") |
| 133 | + |
| 134 | + def decode(self, buffer: bytes) -> dict[str, Any]: |
| 135 | + result: dict[str, Any] = json.loads(buffer.decode("utf-8")) |
| 136 | + return result |
| 137 | + |
| 138 | + |
| 139 | +JSON_CODEC = JsonCodec() |
| 140 | + |
| 141 | + |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +# DAG-CBOR codec (stub — requires cbor2) |
| 144 | +# --------------------------------------------------------------------------- |
| 145 | + |
| 146 | + |
| 147 | +def create_dag_cbor_codec(cbor_module: Any) -> GraphCodec: |
| 148 | + """Create a DAG-CBOR codec. |
| 149 | +
|
| 150 | + Requires ``cbor2`` as a dependency. ~40-50% smaller than JSON, |
| 151 | + deterministic encoding. |
| 152 | +
|
| 153 | + Args: |
| 154 | + cbor_module: A module with ``encode(value) -> bytes`` and |
| 155 | + ``decode(bytes) -> Any`` callables. |
| 156 | + """ |
| 157 | + |
| 158 | + class _DagCborCodec: |
| 159 | + @property |
| 160 | + def content_type(self) -> str: |
| 161 | + return "application/dag-cbor" |
| 162 | + |
| 163 | + @property |
| 164 | + def name(self) -> str: |
| 165 | + return "dag-cbor" |
| 166 | + |
| 167 | + def encode(self, snapshot: dict[str, Any]) -> bytes: |
| 168 | + result: bytes = cbor_module.encode(snapshot) |
| 169 | + return result |
| 170 | + |
| 171 | + def decode(self, buffer: bytes) -> dict[str, Any]: |
| 172 | + result: dict[str, Any] = cbor_module.decode(buffer) |
| 173 | + return result |
| 174 | + |
| 175 | + return _DagCborCodec() |
| 176 | + |
| 177 | + |
| 178 | +def create_dag_cbor_zstd_codec( |
| 179 | + cbor_module: Any, |
| 180 | + zstd_module: Any, |
| 181 | +) -> GraphCodec: |
| 182 | + """Create a DAG-CBOR + zstd codec. ~80-90% smaller than JSON. |
| 183 | +
|
| 184 | + Args: |
| 185 | + cbor_module: Module with ``encode``/``decode``. |
| 186 | + zstd_module: Module with ``compress``/``decompress``. |
| 187 | + """ |
| 188 | + |
| 189 | + class _DagCborZstdCodec: |
| 190 | + @property |
| 191 | + def content_type(self) -> str: |
| 192 | + return "application/dag-cbor+zstd" |
| 193 | + |
| 194 | + @property |
| 195 | + def name(self) -> str: |
| 196 | + return "dag-cbor-zstd" |
| 197 | + |
| 198 | + def encode(self, snapshot: dict[str, Any]) -> bytes: |
| 199 | + result: bytes = zstd_module.compress(cbor_module.encode(snapshot)) |
| 200 | + return result |
| 201 | + |
| 202 | + def decode(self, buffer: bytes) -> dict[str, Any]: |
| 203 | + result: dict[str, Any] = cbor_module.decode( |
| 204 | + zstd_module.decompress(buffer) |
| 205 | + ) |
| 206 | + return result |
| 207 | + |
| 208 | + return _DagCborZstdCodec() |
| 209 | + |
| 210 | + |
| 211 | +# --------------------------------------------------------------------------- |
| 212 | +# Codec negotiation |
| 213 | +# --------------------------------------------------------------------------- |
| 214 | + |
| 215 | + |
| 216 | +def negotiate_codec( |
| 217 | + local_preference: list[GraphCodec], |
| 218 | + remote_content_types: list[str], |
| 219 | +) -> GraphCodec | None: |
| 220 | + """Negotiate a common codec between two peers. |
| 221 | +
|
| 222 | + Each peer advertises its supported codecs (ordered by preference). |
| 223 | + Returns the first codec supported by both, or ``None``. |
| 224 | + """ |
| 225 | + remote_set = set(remote_content_types) |
| 226 | + for codec in local_preference: |
| 227 | + if codec.content_type in remote_set: |
| 228 | + return codec |
| 229 | + return None |
| 230 | + |
| 231 | + |
| 232 | +# --------------------------------------------------------------------------- |
| 233 | +# WAL helpers |
| 234 | +# --------------------------------------------------------------------------- |
| 235 | + |
| 236 | + |
| 237 | +def replay_wal(entries: list[WALEntry]) -> dict[str, Any]: |
| 238 | + """Reconstruct a snapshot from a WAL (full snapshot + sequence of deltas). |
| 239 | +
|
| 240 | + Applies deltas in order on top of the base snapshot. |
| 241 | +
|
| 242 | + Args: |
| 243 | + entries: Ordered WAL entries (must start with a full snapshot). |
| 244 | +
|
| 245 | + Returns: |
| 246 | + Reconstructed snapshot dict. |
| 247 | +
|
| 248 | + Raises: |
| 249 | + ValueError: If the WAL is empty or doesn't start with a full snapshot. |
| 250 | + """ |
| 251 | + if not entries: |
| 252 | + raise ValueError("WAL is empty — need at least one full snapshot") |
| 253 | + |
| 254 | + first = entries[0] |
| 255 | + if first.type != "full" or first.snapshot is None: |
| 256 | + raise ValueError("WAL must start with a full snapshot") |
| 257 | + |
| 258 | + # Deep clone so we can mutate. |
| 259 | + import copy |
| 260 | + result = copy.deepcopy(first.snapshot) |
| 261 | + |
| 262 | + for entry in entries[1:]: |
| 263 | + if entry.type == "full" and entry.snapshot is not None: |
| 264 | + result = copy.deepcopy(entry.snapshot) |
| 265 | + continue |
| 266 | + |
| 267 | + if entry.type != "delta" or entry.delta is None: |
| 268 | + continue |
| 269 | + |
| 270 | + delta = entry.delta |
| 271 | + |
| 272 | + # Apply node changes. |
| 273 | + nodes = result.setdefault("nodes", {}) |
| 274 | + for name, patch in delta.nodes.items(): |
| 275 | + if name in nodes: |
| 276 | + nodes[name]["value"] = patch.get("value") |
| 277 | + if "meta" in patch: |
| 278 | + nodes[name]["meta"] = patch["meta"] |
| 279 | + else: |
| 280 | + nodes[name] = patch |
| 281 | + |
| 282 | + # Remove nodes. |
| 283 | + for name in delta.removed: |
| 284 | + nodes.pop(name, None) |
| 285 | + |
| 286 | + # Apply edge changes. |
| 287 | + edges = result.setdefault("edges", []) |
| 288 | + for edge in delta.edges_added: |
| 289 | + edges.append(edge) |
| 290 | + for edge in delta.edges_removed: |
| 291 | + with contextlib.suppress(ValueError): |
| 292 | + edges.remove(edge) |
| 293 | + |
| 294 | + return result |
0 commit comments