93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
"""Persistent sync state (the manifest).
|
|
|
|
The manifest is what makes update/delete sync possible: it remembers, per source,
|
|
which files were ingested, their stable Onyx ``document_id`` and a content hash.
|
|
On each cycle the engine diffs the share against this manifest to decide what to
|
|
upsert (new/changed) and what to delete (gone from disk).
|
|
|
|
It also caches the auto-provisioned ``cc_pair_id`` per source so provisioning is
|
|
idempotent across restarts.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import tempfile
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
@dataclass
|
|
class FileRecord:
|
|
document_id: str
|
|
content_hash: str
|
|
logical_path: str
|
|
size: int
|
|
mtime: float
|
|
# Ingestion payload format this file was last sent with. Bumping
|
|
# DOC_FORMAT_VERSION in sync.py forces a one-time re-upsert of every file
|
|
# when the document shape changes (e.g. the semantic_identifier), even
|
|
# though the file bytes are unchanged. Defaults to 1 for manifests written
|
|
# before this field existed.
|
|
doc_format_version: int = 1
|
|
|
|
|
|
@dataclass
|
|
class SourceState:
|
|
cc_pair_id: int | None = None
|
|
# relative-path -> FileRecord
|
|
files: dict[str, FileRecord] = field(default_factory=dict)
|
|
# Content hash of the last table-of-contents (index) document written for
|
|
# this source. Lets the engine re-upsert the index only when the file set
|
|
# changed. None until the first index is written.
|
|
index_hash: str | None = None
|
|
|
|
|
|
class State:
|
|
def __init__(self, path: str) -> None:
|
|
self.path = path
|
|
self.api_key: str | None = None
|
|
self.sources: dict[str, SourceState] = {}
|
|
|
|
# -- load/save -----------------------------------------------------------
|
|
@classmethod
|
|
def load(cls, path: str) -> "State":
|
|
state = cls(path)
|
|
if not os.path.exists(path):
|
|
return state
|
|
with open(path, "r", encoding="utf-8") as fh:
|
|
raw = json.load(fh)
|
|
state.api_key = raw.get("api_key")
|
|
for name, s in (raw.get("sources") or {}).items():
|
|
ss = SourceState(cc_pair_id=s.get("cc_pair_id"), index_hash=s.get("index_hash"))
|
|
for rel, rec in (s.get("files") or {}).items():
|
|
ss.files[rel] = FileRecord(**rec)
|
|
state.sources[name] = ss
|
|
return state
|
|
|
|
def save(self) -> None:
|
|
os.makedirs(os.path.dirname(self.path) or ".", exist_ok=True)
|
|
data = {
|
|
"api_key": self.api_key,
|
|
"sources": {
|
|
name: {
|
|
"cc_pair_id": ss.cc_pair_id,
|
|
"index_hash": ss.index_hash,
|
|
"files": {rel: vars(rec) for rel, rec in ss.files.items()},
|
|
}
|
|
for name, ss in self.sources.items()
|
|
},
|
|
}
|
|
# Atomic write so a crash mid-write can't corrupt the manifest.
|
|
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(self.path) or ".", suffix=".tmp")
|
|
try:
|
|
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
|
json.dump(data, fh, indent=2)
|
|
os.replace(tmp, self.path)
|
|
finally:
|
|
if os.path.exists(tmp):
|
|
os.remove(tmp)
|
|
|
|
# -- accessors -----------------------------------------------------------
|
|
def source(self, name: str) -> SourceState:
|
|
return self.sources.setdefault(name, SourceState())
|