Make the store movable and the CI gate actually runnable

CI was a workflow that could not run its own tests: pytest is not a
dependency of anything, so a fresh clone fails with "No module named
pytest". Test tooling now lives in the backend's dev extra, so CI and a
new contributor install the same tools, and ruff is pinned there instead
of drifting as a bare version in a workflow step.

backup, restore and doctor move the store. Backup uses SQLite's own
online API so a running server is safe, and the manifest records the
schema version, row counts, sha256 and size read back from the copy.
Restore verifies the digest, integrity_check, required tables and schema
age before it touches the live file, and keeps what it replaces as
.before-restore.

Doctor separates broken from degraded. On the 906 MB baseline store
(165,973 atoms) it reports no problems and one true warning: 21,362
picture atoms with no CLIP snapshot configured, so those atoms are
findable only by the text already on them. The first version called that
store healthy because it only complained about paths that were set and
missing, never about a sense the store needs and never had.

AGPL-3.0-only, because PyMuPDF is dual-licensed AGPL-or-commercial and
the repository is public. A permissive license needs the Artifex
commercial license bought first; the README says so rather than hiding it.

Gates: 253 tests pass; ruff clean repo-wide (17 errors at HEAD); mypy
unchanged from HEAD on touched files; pyright 0 errors on the new files;
backup 4.0s and restore 2.8s on real data with counts verified equal.
This commit is contained in:
ZacharyZhang-NY committed 2026-10-08 02:32:52 -04:00
1 parent 922d01202f
commit 07825913fe
10 files changed
+1234 -11

No files matched your search

+7
View File
@@ -7,6 +7,7 @@ name = "ely-eye"
version = "0.1.0"
description = "Local multimodal context operating system for Qwen3.5-9B."
readme = "README.md"
license = "AGPL-3.0-only"
requires-python = ">=3.12"
dependencies = [
"accelerate>=1.12.0",
@@ -41,6 +42,12 @@ dependencies = [
"zstandard>=0.25.0"
]
[project.optional-dependencies]
dev = [
"pytest>=9.1.0",
"ruff==0.16.10"
]
[project.scripts]
ely-eye = "ely_eye.cli:app"
+2
View File
@@ -18,6 +18,7 @@ from .benchmarks import (
from .cartridge import CartridgeService
from .cartridge_assets import CartridgeAssetService
from .cli_common import json_dump
from .cli_maintenance import register as register_maintenance_commands
from .cli_senses import register as register_sense_commands
from .cli_senses import senses_report
from .cli_transactive import register as register_transactive_commands
@@ -40,6 +41,7 @@ app = typer.Typer(no_args_is_help=True)
register_transactive_commands(app, lambda: get_settings())
register_sense_commands(app, lambda: get_settings())
register_maintenance_commands(app, lambda: get_settings())
@app.command()
+33
View File
@@ -0,0 +1,33 @@
"""The commands that keep a local store keepable: backup, restore, doctor."""
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import typer
from .config import Settings
from .maintenance import backup, doctor, restore
def register(app: typer.Typer, settings: Callable[[], Settings]) -> None:
"""Add the maintenance commands; the settings thunk lets one patch in a test redirect every path."""
@app.command("backup")
def backup_command(destination: Path, force: bool = False) -> None:
"""Copy the whole store into a folder while a running server keeps answering."""
typer.echo(backup(settings(), destination, force).model_dump_json(indent=2))
@app.command("restore")
def restore_command(source: Path, force: bool = False) -> None:
"""Put a backed-up folder back; the store it replaces is kept beside the new one."""
typer.echo(restore(settings(), source, force).model_dump_json(indent=2))
@app.command("doctor")
def doctor_command() -> None:
"""Report what is broken and what is only degraded; a broken store exits non-zero."""
report = doctor(settings())
typer.echo(report.model_dump_json(indent=2))
if not report.ok:
raise typer.Exit(code=1)
+259
View File
@@ -0,0 +1,259 @@
"""Move the store and ask whether it is well: backup, restore, doctor."""
from __future__ import annotations
import hashlib
import shutil
import sqlite3
from datetime import datetime
from pathlib import Path
from pydantic import BaseModel
from . import __version__
from .cartridge_store import mounted_ids
from .config import Settings
from .db import Database
from .embeddings import EmbeddingService
from .migrations import SCHEMA_VERSION
from .schemas import utc_now
from .visual_index import VisualIndex
BACKUP_FILE = "ely_eye.sqlite"
MANIFEST_FILE = "backup_manifest.json"
_REQUIRED_TABLES = ("atoms", "sources", "cartridges")
PICTURE_MODALITIES = ("image", "ui_screenshot")
class BackupManifest(BaseModel):
"""What a backup holds, so a folder of files can be checked before it is trusted."""
created_at: datetime
ely_eye_version: str
schema_version: int
atoms: int
sources: int
cartridges: int
sha256: str
size_bytes: int
path: str
class DoctorReport(BaseModel):
"""Broken versus degraded: problems stop the store, warnings only shrink what it can do."""
ok: bool
store: str
schema_version: int | None = None
atoms: int = 0
sources: int = 0
cartridges: int = 0
mounted: list[str] = []
problems: list[str] = []
warnings: list[str] = []
def _atoms_word(count: int) -> str:
return "atom" if count == 1 else "atoms"
def _modality_counts(database: Database) -> dict[str, int]:
with database.connect() as connection:
rows = connection.execute("SELECT modality, COUNT(*) AS total FROM atoms GROUP BY modality").fetchall()
return {str(row["modality"]): int(row["total"]) for row in rows}
def _counts(connection: sqlite3.Connection) -> dict[str, int]:
return {
"atoms": int(connection.execute("SELECT COUNT(*) FROM atoms").fetchone()[0]),
"sources": int(connection.execute("SELECT COUNT(*) FROM sources").fetchone()[0]),
"cartridges": int(connection.execute("SELECT COUNT(*) FROM cartridges").fetchone()[0]),
}
def _schema_version(connection: sqlite3.Connection) -> int:
return int(connection.execute("PRAGMA user_version").fetchone()[0])
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for block in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def backup(settings: Settings, destination: Path, force: bool = False) -> BackupManifest:
"""Copy the live store with SQLite's own backup API, so a running server still yields a whole file."""
origin = settings.db_path
if not origin.exists():
raise ValueError(f"no memory store at {origin}; nothing to back up")
destination.mkdir(parents=True, exist_ok=True)
target = destination / BACKUP_FILE
if target.exists() and not force:
raise ValueError(f"{target} already holds a backup; pass --force to replace it")
with sqlite3.connect(origin) as source, sqlite3.connect(target) as copy:
source.backup(copy)
counted = _counts(copy)
version = _schema_version(copy)
manifest = BackupManifest(
created_at=utc_now(),
ely_eye_version=__version__,
schema_version=version,
atoms=counted["atoms"],
sources=counted["sources"],
cartridges=counted["cartridges"],
sha256=_sha256(target),
size_bytes=target.stat().st_size,
path=str(target),
)
(destination / MANIFEST_FILE).write_text(manifest.model_dump_json(indent=2), encoding="utf-8")
return manifest
def _read_sidecar(source: Path) -> tuple[BackupManifest, int]:
manifest_path = source / MANIFEST_FILE
backup_file = source / BACKUP_FILE
if not manifest_path.is_file() or not backup_file.is_file():
raise ValueError(f"{source} is not a backup: it needs {BACKUP_FILE} and {MANIFEST_FILE} inside it")
manifest = BackupManifest.model_validate_json(manifest_path.read_text(encoding="utf-8"))
digest = _sha256(backup_file)
if digest != manifest.sha256:
raise ValueError(
f"{backup_file} does not match its manifest: the file hashes {digest[:12]}, the manifest says {manifest.sha256[:12]}"
)
with sqlite3.connect(backup_file) as connection:
if str(connection.execute("PRAGMA integrity_check").fetchone()[0]) != "ok":
raise ValueError(f"{backup_file} fails integrity_check; do not restore it")
tables = {
str(row[0])
for row in connection.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall()
}
missing = [table for table in _REQUIRED_TABLES if table not in tables]
if missing:
raise ValueError(f"{backup_file} is missing the tables this store needs: {', '.join(missing)}")
version = _schema_version(connection)
return manifest, version
def restore(settings: Settings, source: Path, force: bool = False) -> BackupManifest:
"""Put a backup back, and keep the store it replaces out of reach instead of under it."""
manifest, version = _read_sidecar(source)
if version > SCHEMA_VERSION:
raise ValueError(
f"{source / BACKUP_FILE} was written by a newer build: schema {version}, this build understands {SCHEMA_VERSION}"
)
live = settings.db_path
if live.exists():
if not force:
raise ValueError(f"{live} already holds a store; pass --force to replace it")
kept = live.with_suffix(".before-restore")
try:
shutil.move(str(live), kept)
for suffix in ("-wal", "-shm"):
sibling = Path(f"{live}{suffix}")
if sibling.exists():
shutil.move(str(sibling), Path(f"{kept}{suffix}"))
except OSError as error:
raise ValueError(f"cannot move {live} aside to {kept}: {error}") from error
live.parent.mkdir(parents=True, exist_ok=True)
try:
shutil.copyfile(source / BACKUP_FILE, live)
except OSError as error:
raise ValueError(f"cannot write the store to {live}: {error}") from error
Database(settings).init()
return manifest.model_copy(update={"path": str(live)})
def _check_cartridges(database: Database, mounted: list[str], problems: list[str]) -> None:
roots = {str(row["cartridge_id"]): str(row["root_path"]) for row in database.list_cartridges()}
for cartridge_id in mounted:
root = roots.get(cartridge_id)
if root is None:
problems.append(f"{cartridge_id} is mounted but no cartridge row names it")
elif not Path(root).exists():
problems.append(f"{cartridge_id} is mounted but its files at {root} are gone")
def _check_senses(settings: Settings, database: Database, visual: VisualIndex, warnings: list[str]) -> None:
counts = _modality_counts(database)
pictures = sum(counts.get(name, 0) for name in PICTURE_MODALITIES)
if settings.clip_model_dir is not None and visual.model_dir() is None:
warnings.append(f"the CLIP snapshot at {settings.clip_model_dir} is missing, so wordless pictures cannot be found")
elif pictures and not visual.available():
warnings.append(
f"the store holds {pictures} picture {_atoms_word(pictures)} but no CLIP snapshot is set "
"(ELY_EYE_CLIP_MODEL_DIR), so they are findable only by the text already on them"
)
elif visual.visual_atoms() and not visual.is_current():
warnings.append("the visual index is behind the store; run `ely-eye index-visual`")
audio = counts.get("audio", 0)
if settings.asr_model_dir is None:
if audio:
warnings.append(
f"the store holds {audio} audio {_atoms_word(audio)} but no speech snapshot is set "
"(ELY_EYE_ASR_MODEL_DIR), so new audio will not be transcribed"
)
elif not settings.asr_model_dir.exists():
warnings.append(f"the speech snapshot at {settings.asr_model_dir} is missing, so audio cannot be transcribed")
if settings.vlm_model_dir is not None and not settings.vlm_model_dir.exists():
warnings.append(f"the captioning model at {settings.vlm_model_dir} is missing, so images stay uncaptioned")
def _check_indexes(settings: Settings, database: Database, warnings: list[str]) -> None:
if not settings.dense_embeddings_enabled:
return
index = EmbeddingService(settings, database).load_index()
stored = database.atom_count()
if index is None:
warnings.append("dense embeddings are on but no index exists; run `ely-eye build-dense-index`")
elif len(index.atom_ids) != stored:
warnings.append(
f"the dense index covers {len(index.atom_ids)} atoms while the store holds {stored}; run `ely-eye build-dense-index`"
)
def doctor(settings: Settings) -> DoctorReport:
"""Say what is broken and what is only degraded, and name the command that fixes each."""
problems: list[str] = []
warnings: list[str] = []
live = settings.db_path
if not live.exists():
return DoctorReport(
ok=False,
store=str(live),
schema_version=None,
problems=[f"no memory store at {live}; run `ely-eye ingest` or `ely-eye remember` first"],
)
database: Database
try:
database = Database(settings)
except SystemExit as error:
return DoctorReport(
ok=False,
store=str(live),
problems=[f"the store cannot be opened by this build: {error}"],
)
with database.connect() as connection:
if str(connection.execute("PRAGMA integrity_check").fetchone()[0]) != "ok":
problems.append(f"{live} fails integrity_check; restore a backup before writing to it")
version = _schema_version(connection)
if version != SCHEMA_VERSION:
problems.append(f"the schema settled at version {version} while this build writes {SCHEMA_VERSION}")
mounted = mounted_ids(database)
_check_cartridges(database, mounted, problems)
_check_senses(settings, database, VisualIndex(settings, database), warnings)
_check_indexes(settings, database, warnings)
if settings.ui_dir is not None and not settings.ui_dir.exists():
problems.append(f"ui_dir {settings.ui_dir} is set but holds no built interface, so the API refuses to start")
return DoctorReport(
ok=not problems,
store=str(live),
schema_version=version,
atoms=database.atom_count(),
sources=database.source_count(),
cartridges=database.cartridge_count(),
mounted=mounted,
problems=problems,
warnings=warnings,
)
+225
View File
@@ -0,0 +1,225 @@
"""Backup, restore and doctor: a store nobody can move is a store nobody trusts."""
from __future__ import annotations
import hashlib
import json
import sqlite3
from pathlib import Path
import pytest
from typer.testing import CliRunner
from ely_eye import cli as cli_module
from ely_eye import maintenance
from ely_eye.cartridge_store import set_mounts
from ely_eye.cli import app
from ely_eye.config import Settings
from ely_eye.db import Database
from ely_eye.memory import MemoryService
from ely_eye.migrations import SCHEMA_VERSION
from ely_eye.schemas import EvidenceAtom, MemoryOrigin, Modality, SourceRecord, TrustScores, utc_now
KEYS = "the keys are on the shelf by the door"
PASSPORT = "the passport is in the blue box"
def _settings(tmp_path: Path) -> Settings:
return Settings(home=tmp_path / "home", default_holder="wife")
def _store(settings: Settings, statements: tuple[str, ...] = (KEYS, PASSPORT)) -> Database:
database = Database(settings)
database.init()
service = MemoryService(settings, database)
for statement in statements:
service.remember(statement, holder_id="wife", origin=MemoryOrigin.self_)
return database
def _digest(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def test_a_backup_carries_the_store_and_says_what_it_holds(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
manifest = maintenance.backup(settings, tmp_path / "backup")
assert (tmp_path / "backup" / maintenance.BACKUP_FILE).is_file()
assert (tmp_path / "backup" / maintenance.MANIFEST_FILE).is_file()
assert manifest.atoms == 2
assert manifest.sources == 2
assert manifest.cartridges == 0
assert manifest.schema_version == SCHEMA_VERSION
written = tmp_path / "backup" / maintenance.BACKUP_FILE
assert manifest.sha256 == _digest(written)
assert manifest.size_bytes == written.stat().st_size
def test_a_backup_refuses_to_overwrite_itself_without_permission(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
maintenance.backup(settings, tmp_path / "backup")
with pytest.raises(ValueError, match="already holds a backup"):
maintenance.backup(settings, tmp_path / "backup")
assert maintenance.backup(settings, tmp_path / "backup", force=True).atoms == 2
def test_backup_names_the_store_it_cannot_find(tmp_path: Path) -> None:
settings = _settings(tmp_path)
with pytest.raises(ValueError, match="nothing to back up"):
maintenance.backup(settings, tmp_path / "backup")
def test_a_restore_brings_the_memories_back(tmp_path: Path) -> None:
source = _settings(tmp_path)
_store(source)
manifest = maintenance.backup(source, tmp_path / "backup")
destination = Settings(home=tmp_path / "elsewhere", default_holder="wife")
restored = maintenance.restore(destination, tmp_path / "backup")
assert restored.path == str(destination.db_path)
assert manifest.atoms == 2
database = Database(destination)
assert database.atom_count() == 2
assert {atom.text for atom in database.list_atoms()} == {KEYS, PASSPORT}
def test_a_restore_keeps_the_store_it_replaces(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
maintenance.backup(settings, tmp_path / "backup")
_store(settings, statements=("only one memory here",))
with pytest.raises(ValueError, match="pass --force"):
maintenance.restore(settings, tmp_path / "backup")
maintenance.restore(settings, tmp_path / "backup", force=True)
kept = settings.db_path.with_suffix(".before-restore")
assert kept.is_file()
with sqlite3.connect(kept) as connection:
kept_atoms = int(connection.execute("SELECT COUNT(*) FROM atoms").fetchone()[0])
assert kept_atoms == 3
assert Database(settings).atom_count() == 2
def test_a_tampered_backup_is_refused(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
maintenance.backup(settings, tmp_path / "backup")
database_file = tmp_path / "backup" / maintenance.BACKUP_FILE
database_file.write_bytes(database_file.read_bytes() + b"\x00")
with pytest.raises(ValueError, match="does not match its manifest"):
maintenance.restore(Settings(home=tmp_path / "elsewhere"), tmp_path / "backup")
def test_a_backup_from_a_newer_build_is_refused(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
maintenance.backup(settings, tmp_path / "backup")
database_file = tmp_path / "backup" / maintenance.BACKUP_FILE
with sqlite3.connect(database_file) as connection:
connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION + 7}")
manifest_path = tmp_path / "backup" / maintenance.MANIFEST_FILE
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
manifest["schema_version"] = SCHEMA_VERSION + 7
manifest["sha256"] = _digest(database_file)
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
with pytest.raises(ValueError, match="newer build"):
maintenance.restore(Settings(home=tmp_path / "elsewhere"), tmp_path / "backup")
def test_a_folder_without_the_backup_pair_is_refused(tmp_path: Path) -> None:
(tmp_path / "backup").mkdir()
with pytest.raises(ValueError, match="is not a backup"):
maintenance.restore(_settings(tmp_path), tmp_path / "backup")
def test_doctor_reports_a_store_that_does_not_exist_yet(tmp_path: Path) -> None:
report = maintenance.doctor(_settings(tmp_path))
assert report.ok is False
assert len(report.problems) == 1
assert "no memory store" in report.problems[0]
assert report.atoms == 0
def test_doctor_passes_a_healthy_store(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
report = maintenance.doctor(settings)
assert report.ok is True
assert report.problems == []
assert report.atoms == 2
assert report.sources == 2
assert report.schema_version == SCHEMA_VERSION
def test_doctor_names_the_senses_a_store_holds_but_cannot_use(tmp_path: Path) -> None:
settings = _settings(tmp_path)
database = Database(settings)
database.init()
trust = TrustScores(parser=0.0, ocr=0.0, model=0.0)
database.upsert_sources(
[
SourceRecord(source_id="img", path="file:///img.png", kind="image", mime="image/png", sha256="a", size_bytes=1),
SourceRecord(source_id="aud", path="file:///a.mp3", kind="audio", mime="audio/mpeg", sha256="b", size_bytes=1),
]
)
database.upsert_atoms(
[
EvidenceAtom(atom_id="img:1", modality=Modality.image, source_id="img", source="file:///img.png", trust=trust),
EvidenceAtom(atom_id="aud:1", modality=Modality.audio, source_id="aud", source="file:///a.mp3", trust=trust),
]
)
report = maintenance.doctor(settings)
assert report.ok is True
assert len(report.warnings) == 2
assert "1 picture atom but no CLIP snapshot" in report.warnings[0]
assert "1 audio atom but no speech snapshot" in report.warnings[1]
def test_doctor_names_a_mount_whose_files_are_gone(tmp_path: Path) -> None:
settings = _settings(tmp_path)
database = _store(settings)
database.write_cartridge(
"cart-a", "Cart A", tmp_path / "gone", "{}", "dna", 10, utc_now()
)
set_mounts(database, ["cart-a"], utc_now())
report = maintenance.doctor(settings)
assert report.ok is False
assert report.mounted == ["cart-a"]
assert len(report.problems) == 1
assert "is mounted but its files at" in report.problems[0]
def test_doctor_refuses_a_store_written_by_a_newer_build(tmp_path: Path) -> None:
settings = _settings(tmp_path)
_store(settings)
with sqlite3.connect(settings.db_path) as connection:
connection.execute(f"PRAGMA user_version = {SCHEMA_VERSION + 7}")
report = maintenance.doctor(settings)
assert report.ok is False
assert len(report.problems) == 1
assert "this build understands" in report.problems[0]
def test_the_cli_backs_up_and_a_restore_bring_the_store_home(tmp_path: Path, monkeypatch) -> None:
runner = CliRunner()
settings = _settings(tmp_path)
monkeypatch.setattr(cli_module, "get_settings", lambda: settings)
empty = runner.invoke(app, ["doctor"])
assert empty.exit_code == 1
assert "no memory store" in empty.output
_store(settings)
healthy = runner.invoke(app, ["doctor"])
assert healthy.exit_code == 0
assert json.loads(healthy.output)["atoms"] == 2
backed_up = runner.invoke(app, ["backup", str(tmp_path / "backup")])
assert backed_up.exit_code == 0
assert json.loads(backed_up.output)["sha256"]
elsewhere = Settings(home=tmp_path / "elsewhere", default_holder="wife")
monkeypatch.setattr(cli_module, "get_settings", lambda: elsewhere)
moved = runner.invoke(app, ["restore", str(tmp_path / "backup")])
assert moved.exit_code == 0
assert Database(elsewhere).atom_count() == 2