When SQLite's backup() cannot write the snapshot into the live database (a damaged destination header, for example), both restore paths fall back to copying the snapshot beside the file, unlinking the database and its -wal/-shm/-journal sidecars, and moving the copy into place. That swap is only safe when no other process holds the file: a live holder keeps writing the unlinked inode and the restore silently splits the database. The guard in front of the swap asked _foreign_db_holder_pids(), a /proc scan that returns None when it cannot run (every non-Linux host, or an unreadable /proc). Both callers tested `if holders:`, so None passed as "no holders" and the swap ran on macOS under a live gateway, dashboard or CLI. The helper's own docstring says None means unknown. The store admin path (state.db, shared-state.db) now uses foreign_state_db_holders(), and the non-store path (projects.db, cron/executions.db, ...) asks the file's store admin for the same scan. It covers Linux, macOS (psutil) and Windows (Restart Manager), and reports a scan it could not finish as a (-1, reason) holder, so unknown refuses like a live holder does. With a clean scan the fallback still runs on every host. The now-unused copy of the Linux-only scan in the store admin is removed. The missing-target check in `hermes import` keeps the old scan: refusing on unknown there would block importing into a fresh home on macOS and Windows.
768 lines
38 KiB
Python
768 lines
38 KiB
Python
"""BackupAdmin operations of the SQLite store admin: WAL-safe snapshots and restore, file verification, the session
|
|
row counts, the runtime-ledger probe and offline transcript recovery."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import shutil
|
|
import sqlite3
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
from contextlib import closing, contextmanager, suppress
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
from typing import Any, Callable, Optional
|
|
|
|
from persistence.sqlite._state.holders import foreign_state_db_holders, read_only_db_uri
|
|
from hermes_cli.sqlite_safe_read import connect_tracked, read_header_bytes_preopen
|
|
|
|
from .._backend import translated
|
|
|
|
from . import _recovery
|
|
from ._base import AdminFile
|
|
|
|
logger = logging.getLogger("hermes_cli.backup_restore")
|
|
_backup_sqlite_logger = logging.getLogger("hermes_cli.backup_sqlite")
|
|
|
|
_SQLITE_HEADER = b"SQLite format 3\0"
|
|
|
|
# Above this size ``PRAGMA integrity_check`` (walks every b-tree page: minutes of pegged CPU on a
|
|
# 30 GB state.db, reading as a hung ``hermes update``) is replaced by the O(1) header+schema probe.
|
|
# Sessions databases in the tens of GB are normal for heavy users, so the size-unbounded check is
|
|
# never an acceptable default on the update path.
|
|
DEFAULT_INTEGRITY_CHECK_MAX_BYTES = 2 << 30 # 2 GiB
|
|
|
|
_LEDGER_PROBE = ("SELECT EXISTS(SELECT 1 FROM session_admissions) "
|
|
"OR EXISTS(SELECT 1 FROM worker_executions)")
|
|
|
|
|
|
class _SQLiteBackupTimeout(RuntimeError):
|
|
"""Raised when a SQLite snapshot remains busy past its deadline."""
|
|
|
|
|
|
def _close_quietly(conn: Optional[sqlite3.Connection]) -> None:
|
|
if conn is not None:
|
|
with suppress(Exception):
|
|
conn.close()
|
|
|
|
|
|
class _SQLiteBackupRestarted(RuntimeError):
|
|
"""Raised when a foreign write restarted a stepped snapshot from page one."""
|
|
|
|
|
|
# A rollback-journal reader blocks every writer's commit while it holds the read
|
|
# lock, so that copy releases it after each step; a waiting writer's PENDING lock
|
|
# refuses the next step, so it waits for one step at most. Activity writes give
|
|
# up after 0.5s (the store's activity-write patience); every hold stays under it.
|
|
_ROLLBACK_STEP_SECONDS = 0.1
|
|
_ROLLBACK_FIRST_STEP_PAGES = 256
|
|
_ROLLBACK_BUSY_SLEEP_SECONDS = 0.01
|
|
# Each foreign write restarts a stepped copy, so a continuous writer gets a bounded
|
|
# number of attempts, then one whole-copy hold if it fits the budget, else a refusal.
|
|
_ROLLBACK_MAX_RESTARTS = 20
|
|
_ROLLBACK_LOCK_HOLD_SECONDS = 0.25
|
|
|
|
|
|
def _uses_wal(src: Path) -> bool:
|
|
# Header byte 18 (file format write version) is 2 for WAL; reading it takes no lock.
|
|
with open(src, "rb") as handle:
|
|
header = handle.read(19)
|
|
return len(header) == 19 and header[18] == 2
|
|
|
|
|
|
class _CopyRate:
|
|
def __init__(self) -> None:
|
|
self.seconds = 0.0
|
|
self.pages = 0
|
|
self.total = 0
|
|
|
|
def pages_per_second(self) -> float:
|
|
return self.pages / max(self.seconds, 1e-6)
|
|
|
|
|
|
def _stepped_attempt(conn: sqlite3.Connection, backup_conn: sqlite3.Connection, pages: int,
|
|
check_busy, rate: _CopyRate) -> bool:
|
|
"""One stepped backup; False when a foreign write restarted it."""
|
|
last_remaining: Optional[int] = None
|
|
last_tick, last_busy = time.monotonic(), False
|
|
|
|
def _progress(status: int, remaining: int, total: int) -> None:
|
|
nonlocal last_remaining, last_tick, last_busy
|
|
check_busy(status, remaining, total)
|
|
now = time.monotonic()
|
|
# Copy time only: drop the sleep sqlite3 takes after a busy step.
|
|
step_seconds = max(0.0, now - last_tick - (_ROLLBACK_BUSY_SLEEP_SECONDS if last_busy else 0.0))
|
|
last_tick, last_busy = now, status in (sqlite3.SQLITE_BUSY, sqlite3.SQLITE_LOCKED)
|
|
if status not in (sqlite3.SQLITE_OK, sqlite3.SQLITE_DONE):
|
|
return
|
|
restarted = last_remaining is not None and remaining >= last_remaining
|
|
rate.seconds += step_seconds
|
|
rate.pages += total - remaining if last_remaining is None or restarted else last_remaining - remaining
|
|
rate.total = total
|
|
if restarted:
|
|
raise _SQLiteBackupRestarted()
|
|
last_remaining = remaining
|
|
|
|
try:
|
|
conn.backup(backup_conn, pages=pages, progress=_progress, sleep=_ROLLBACK_BUSY_SLEEP_SECONDS)
|
|
except _SQLiteBackupRestarted:
|
|
return False
|
|
return True
|
|
|
|
|
|
def _stepped_rollback_journal_copy(conn: sqlite3.Connection, backup_conn: sqlite3.Connection,
|
|
check_busy) -> None:
|
|
rate = _CopyRate()
|
|
pages = _ROLLBACK_FIRST_STEP_PAGES
|
|
for _attempt in range(_ROLLBACK_MAX_RESTARTS + 1):
|
|
if _stepped_attempt(conn, backup_conn, pages, check_busy, rate):
|
|
return
|
|
pages = max(_ROLLBACK_FIRST_STEP_PAGES, int(rate.pages_per_second() * _ROLLBACK_STEP_SECONDS))
|
|
# Floored: a run where no step completed has no measured rate, and the estimate must not divide by 0.
|
|
expected_hold = rate.total / max(rate.pages_per_second(), 1.0)
|
|
if expected_hold > _ROLLBACK_LOCK_HOLD_SECONDS:
|
|
raise RuntimeError(
|
|
"state.db uses a rollback journal and is written continuously; a consistent snapshot "
|
|
f"would block those writes for about {expected_hold:.1f}s, so it was not taken. "
|
|
"Retry when no agent turn is running.")
|
|
conn.backup(backup_conn, pages=-1, progress=check_busy, sleep=_ROLLBACK_BUSY_SLEEP_SECONDS)
|
|
|
|
|
|
def _wal_copy_until_deadline(conn: sqlite3.Connection, backup_conn: sqlite3.Connection, check_busy) -> None:
|
|
"""One-step backup, restarted while the source reports an extended busy code. sqlite3's backup loop
|
|
retries only plain SQLITE_BUSY/LOCKED; SQLITE_BUSY_RECOVERY (another connection rebuilding the
|
|
WAL index) ends it as an error, though the source is readable a moment later."""
|
|
while True:
|
|
try:
|
|
conn.backup(backup_conn, pages=-1, progress=check_busy, sleep=0.1)
|
|
return
|
|
except sqlite3.OperationalError as exc:
|
|
code = getattr(exc, "sqlite_errorcode", 0)
|
|
if code & 0xFF not in (sqlite3.SQLITE_BUSY, sqlite3.SQLITE_LOCKED):
|
|
raise
|
|
check_busy(code, 0, 0) # raises once the source has been busy past the deadline
|
|
time.sleep(0.1)
|
|
|
|
|
|
class _OffThreadCopy:
|
|
"""One long sqlite3 call run off the main thread. Python runs signal handlers only on the main
|
|
thread and only between bytecodes, so a one-step backup (a single C ``sqlite3_backup_step``,
|
|
GIL released) would hold a SIGTERM until the whole copy finished; the main thread waits here in
|
|
short joins instead, so a deadline signal interrupts the snapshot promptly."""
|
|
|
|
def __init__(self, copy) -> None:
|
|
self._copy, self._error = copy, None
|
|
# health: allow HX012 -- the copy thread runs one sqlite3 backup call and reads no profile state
|
|
self.thread = threading.Thread(target=self._run, name="sqlite-snapshot", daemon=True)
|
|
|
|
def _run(self) -> None:
|
|
try:
|
|
self._copy()
|
|
except BaseException as exc: # health: allow BLE001 -- handed to the waiting main thread, which re-raises it
|
|
self._error = exc
|
|
|
|
def run(self) -> None:
|
|
self.thread.start()
|
|
while self.thread.is_alive():
|
|
self.thread.join(0.05)
|
|
if self._error is not None:
|
|
raise self._error
|
|
|
|
def running(self) -> bool:
|
|
return self.thread.is_alive()
|
|
|
|
|
|
def _safe_copy_db(src: Path, dst: Path, *, timeout_seconds: float = 10.0) -> bool:
|
|
"""Copy a SQLite database with the backup() API (WAL-safe consistent snapshot).
|
|
|
|
Fails closed when no consistent snapshot can be made: copying only the main file loses WAL data.
|
|
"""
|
|
conn = backup_conn = None
|
|
copier: Optional[_OffThreadCopy] = None
|
|
try:
|
|
# sqlite3.connect() creates a missing destination with the process
|
|
# umask, which is commonly 0022 (0644). Snapshot databases contain
|
|
# session and tool state, so create the inode owner-only before SQLite
|
|
# writes its first byte. O_NOFOLLOW also refuses a planted symlink on
|
|
# platforms that support it. Tighten an existing internal staging
|
|
# file as well (NamedTemporaryFile callers already create it 0600).
|
|
if os.name != "nt":
|
|
open_flags = os.O_WRONLY | os.O_CREAT
|
|
if hasattr(os, "O_NOFOLLOW"):
|
|
open_flags |= os.O_NOFOLLOW
|
|
secure_fd = os.open(dst, open_flags, 0o600)
|
|
try:
|
|
os.fchmod(secure_fd, 0o600)
|
|
finally:
|
|
os.close(secure_fd)
|
|
# timeout=0.0 disables sqlite3's implicit busy wait so the progress callback owns the
|
|
# full locked-source deadline instead of adding the default timeout before each callback.
|
|
conn = sqlite3.connect(f"{src.resolve().as_uri()}?mode=ro", uri=True, timeout=0.0,
|
|
check_same_thread=False)
|
|
backup_conn = sqlite3.connect(str(dst), check_same_thread=False)
|
|
busy_deadline = time.monotonic() + max(0.0, timeout_seconds)
|
|
|
|
def _check_backup_progress(status: int, _remaining: int, _total: int) -> None:
|
|
nonlocal busy_deadline
|
|
now = time.monotonic()
|
|
# Masked: extended codes such as SQLITE_BUSY_RECOVERY are busy too.
|
|
if status & 0xFF in (sqlite3.SQLITE_BUSY, sqlite3.SQLITE_LOCKED):
|
|
if now >= busy_deadline:
|
|
raise _SQLiteBackupTimeout(f"database remained locked for {timeout_seconds:g} seconds")
|
|
else:
|
|
busy_deadline = now + max(0.0, timeout_seconds)
|
|
|
|
if _uses_wal(src):
|
|
# One step: SQLite restarts a multi-step backup whenever another connection writes the
|
|
# source, and a WAL reader never blocks a writer however long the copy takes.
|
|
# Run off the main thread so a SIGTERM is serviced during the copy (_OffThreadCopy).
|
|
copier = _OffThreadCopy(lambda: _wal_copy_until_deadline(conn, backup_conn, _check_backup_progress))
|
|
copier.run()
|
|
else:
|
|
_stepped_rollback_journal_copy(conn, backup_conn, _check_backup_progress)
|
|
return True
|
|
except Exception as exc: # health: allow BLE001 -- any failure fails the copy closed and removes the partial file
|
|
_backup_sqlite_logger.warning("SQLite safe copy failed for %s: %s", src, exc)
|
|
# Windows won't remove the partial destination while SQLite still has it open.
|
|
_close_quietly(backup_conn)
|
|
backup_conn = None
|
|
with suppress(OSError):
|
|
dst.unlink(missing_ok=True)
|
|
return False
|
|
finally:
|
|
# Interrupted mid-copy (SIGTERM -> SystemExit): the worker is still inside the C copy with
|
|
# both connections, and close() would wait on their mutex until the copy ends. The process
|
|
# is exiting; leave them to it, and the caller unlinks the staging file.
|
|
if copier is None or not copier.running():
|
|
_close_quietly(backup_conn)
|
|
_close_quietly(conn)
|
|
|
|
|
|
# Longer than the Desktop's 30-minute pre-flight cap: a staging file this old
|
|
# has no live writer. A hard-killed pre-flight (Windows TerminateProcess)
|
|
# cannot unlink its own staging copy, and each one is a full-size database.
|
|
_ABANDONED_STAGING_SECONDS = 2 * 3600
|
|
|
|
|
|
def _reclaim_abandoned_staging(home: Path, prefix: str) -> None:
|
|
horizon = time.time() - _ABANDONED_STAGING_SECONDS
|
|
for staged in home.glob(f"{prefix}*.partial*"): # + the copy's -journal
|
|
with suppress(OSError):
|
|
if staged.stat().st_mtime < horizon:
|
|
staged.unlink()
|
|
|
|
|
|
def _query_ro_sqlite(path: Path, fn):
|
|
"""Run ``fn(conn)`` on a read-only connection to *path*; return ``(value, None)`` or ``(None, exc)``."""
|
|
conn = None
|
|
try:
|
|
conn = connect_tracked(read_only_db_uri(path), uri=True, timeout=1.0, tracking_path=path)
|
|
return fn(conn), None
|
|
except Exception as exc: # health: allow BLE001 -- the error is returned to the caller
|
|
return None, exc
|
|
finally:
|
|
_close_quietly(conn)
|
|
|
|
|
|
def _probe_verdict(exc: BaseException) -> Optional[bool]:
|
|
"""Only SQLite reporting bad bytes (NOTADB/CORRUPT) accuses the file; failing to open or read it
|
|
(CANTOPEN, IOERR, BUSY, a Windows sharing violation) proves nothing, so the verdict is ``None``."""
|
|
code = getattr(exc, "sqlite_errorcode", None)
|
|
return False if code is not None and code & 0xFF in (sqlite3.SQLITE_NOTADB, sqlite3.SQLITE_CORRUPT) else None
|
|
|
|
|
|
def _wal_may_hold_pages(path: Path) -> bool:
|
|
"""Whether *path* has a ``-wal`` sidecar SQLite may still read committed pages from.
|
|
|
|
A crashed writer can leave every committed page, page 1 included, in the WAL while the main
|
|
file is short or its header is damaged; a read-only open still serves those rows (#140053).
|
|
A sidecar that cannot be stat'd is not proof it is empty, so SQLite's probe decides.
|
|
"""
|
|
# SQLite keeps a symlinked database's -wal beside the file the link resolves to.
|
|
target = Path(os.path.realpath(path))
|
|
try:
|
|
return target.with_name(target.name + "-wal").stat().st_size > 0
|
|
except FileNotFoundError:
|
|
return False
|
|
except OSError:
|
|
return True
|
|
|
|
|
|
def verify_sqlite_integrity(
|
|
path: Path, *, check_header: bool = True, run_pragma: bool = True,
|
|
max_bytes: int = DEFAULT_INTEGRITY_CHECK_MAX_BYTES) -> dict:
|
|
"""Verify a SQLite database: existence + minimum size, header magic, then a read-only
|
|
``PRAGMA integrity_check`` (or a cheap structural probe above ``max_bytes``).
|
|
|
|
``valid`` is tri-state: ``True``, ``False`` (the bytes are bad), or ``None`` when the file could
|
|
not be read right now (locked or held). Callers must not restore over a ``None`` (#147938)."""
|
|
def _done(message: str, valid: Optional[bool] = False, size: Optional[int] = None) -> dict:
|
|
return {"valid": valid, "message": message, "size": size}
|
|
try:
|
|
st = path.stat()
|
|
except FileNotFoundError:
|
|
return _done(f"not found: {path}")
|
|
except OSError as exc:
|
|
return _done(f"cannot stat: {exc}")
|
|
size = st.st_size
|
|
# A short main file or a bad header magic is decided by SQLite itself when a WAL may still
|
|
# hold the pages and the caller asked for the probe; without either the raw verdict stands.
|
|
wal_may_serve = run_pragma and _wal_may_hold_pages(path)
|
|
note = ""
|
|
raw_verdict = "" # what the main file alone says, when only the WAL can still serve it
|
|
if size < 100: # SQLite minimum viable size (header + 1 page)
|
|
raw_verdict = f"too small ({size} bytes) to be a valid SQLite database"
|
|
note = f"main file only {size} bytes, read through its WAL; "
|
|
elif check_header:
|
|
# Refused when a live connection exists: close() would cancel this process's POSIX locks
|
|
# (see sqlite_safe_read). An OSError (a Windows sharing violation) or a short read is
|
|
# refused too. Neither is evidence of corruption, so SQLite's own probe decides below.
|
|
head = read_header_bytes_preopen(path, length=len(_SQLITE_HEADER))
|
|
if head is None or len(head) < len(_SQLITE_HEADER):
|
|
note = "cannot read header; "
|
|
elif head != _SQLITE_HEADER:
|
|
raw_verdict = f"missing SQLite header magic (got {head[:16].hex()!r})"
|
|
note = f"main header damaged (got {head[:16].hex()!r}), read through its WAL; "
|
|
if raw_verdict:
|
|
if not wal_may_serve:
|
|
return _done(raw_verdict, size=size)
|
|
# A stale or torn -wal (shorter than its header, or frames that never committed) opens
|
|
# as an EMPTY database whose integrity check passes; only pages SQLite can see prove the
|
|
# WAL still serves the database, or that empty schema would be kept over the snapshot.
|
|
geometry, exc = _query_ro_sqlite(path, lambda c: tuple(
|
|
c.execute(f"PRAGMA {pragma}").fetchone()[0] for pragma in ("page_count", "page_size")))
|
|
if exc is not None:
|
|
return _done(f"{note}page probe failed: {exc}", _probe_verdict(exc), size=size)
|
|
if not geometry[0]:
|
|
return _done(f"{raw_verdict}; its -wal holds no page SQLite can read", size=size)
|
|
# The main file's few bytes say nothing about the data walk: the WAL may serve gigabytes.
|
|
scanned = max(size, geometry[0] * geometry[1])
|
|
else:
|
|
scanned = size
|
|
if max_bytes > 0 and scanned > max_bytes:
|
|
# O(1) probe: the header check caught the zeroed signature; reading sqlite_master + page
|
|
# geometry catches malformed-schema and truncated-header-page classes without a data walk.
|
|
_, exc = _query_ro_sqlite(path, lambda c: (
|
|
c.execute("PRAGMA schema_version").fetchone(),
|
|
c.execute("SELECT count(*) FROM sqlite_master").fetchone()))
|
|
if exc is not None:
|
|
kind = "failed" if isinstance(exc, sqlite3.DatabaseError) else "error"
|
|
return _done(f"{note}schema probe {kind}: {exc}", _probe_verdict(exc), size=size)
|
|
return _done(
|
|
f"{note}size {scanned:,} bytes exceeds max_bytes {max_bytes:,}; "
|
|
"skipped PRAGMA integrity_check (header + schema probe passed)",
|
|
valid=True, size=size)
|
|
if run_pragma:
|
|
rows, exc = _query_ro_sqlite(
|
|
path, lambda c: [str(r[0]) for r in c.execute("PRAGMA integrity_check")])
|
|
if exc is not None:
|
|
kind = "cannot open database" if isinstance(exc, sqlite3.DatabaseError) else "integrity check error"
|
|
return _done(f"{note}{kind}: {exc}", _probe_verdict(exc), size=size)
|
|
if rows == ["ok"]:
|
|
return _done(f"{note}integrity check passed", valid=True, size=size)
|
|
return _done(f"integrity check failed: {'; '.join(rows[:5])}", size=size)
|
|
if note:
|
|
return _done(note.rstrip("; "), None, size=size)
|
|
return _done("header check passed", valid=True, size=size)
|
|
|
|
|
|
def _restore_epoch(path: Path) -> int:
|
|
if not path.exists():
|
|
return 0
|
|
from hermes_cli.sqlite_safe_read import connect_tracked
|
|
|
|
with closing(connect_tracked(read_only_db_uri(path), uri=True, tracking_path=path,
|
|
connect_fn=sqlite3.connect)) as conn:
|
|
if not conn.execute("SELECT 1 FROM sqlite_master WHERE name='runtime_epoch'").fetchone():
|
|
return 0
|
|
row = conn.execute("SELECT epoch FROM runtime_epoch WHERE singleton=1").fetchone()
|
|
return int(row[0]) if row else 0
|
|
|
|
|
|
def _restore_destination_epoch(dst: Path) -> int:
|
|
try:
|
|
return _restore_epoch(dst)
|
|
except (OSError, sqlite3.Error, ValueError) as exc:
|
|
raise OSError(
|
|
f"Cannot read the canonical runtime epoch at {dst}; refusing in-place restore. "
|
|
"Use `hermes sessions recover --source <snapshot> --output <separate-path>` "
|
|
"to salvage transcripts into a separate output."
|
|
) from exc
|
|
|
|
|
|
@contextmanager
|
|
def _restore_epoch_source(src: Path, dst: Path):
|
|
"""Stage an epoch floor into the image BEFORE publishing it atomically.
|
|
|
|
Updating the destination after backup() would leave a crash window that
|
|
reuses an older worker epoch. Never modify the user's source snapshot.
|
|
Full restores retain all admissions/worker receipts; normal startup advances
|
|
this floor and reconciles started work to unknown, not replayable queued work.
|
|
An unreadable destination cannot prove its epoch floor: use transcript salvage
|
|
into a separate output rather than silently restoring with a recycled epoch.
|
|
"""
|
|
if dst.name != 'state.db' and dst.resolve().name != 'state.db':
|
|
yield src
|
|
return
|
|
floor = _restore_destination_epoch(dst)
|
|
if floor <= _restore_epoch(src):
|
|
yield src
|
|
return
|
|
with tempfile.TemporaryDirectory(prefix='.restore-epoch-', dir=dst.parent) as work:
|
|
prepared = Path(work) / 'state.db'
|
|
if not _safe_copy_db(src, prepared):
|
|
raise OSError('Unable to stage a restore with a safe runtime epoch')
|
|
with closing(sqlite3.connect(prepared)) as conn: # health: allow HX015 -- private staging copy in a temp dir
|
|
conn.execute("""CREATE TABLE IF NOT EXISTS runtime_epoch (
|
|
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
|
epoch INTEGER NOT NULL CHECK (epoch > 0), instance_id TEXT NOT NULL)""")
|
|
conn.execute("""INSERT INTO runtime_epoch VALUES(1,?, 'offline-restore')
|
|
ON CONFLICT(singleton) DO UPDATE SET epoch=excluded.epoch,
|
|
instance_id=excluded.instance_id""", (floor,))
|
|
conn.commit()
|
|
prepared.chmod(src.stat().st_mode)
|
|
yield prepared
|
|
|
|
|
|
def _restore_db_pages(src: Path, dst: Path, before_publish: Optional[Callable[[], None]] = None) -> bool:
|
|
"""Restore a SQLite database from snapshot *src* into live *dst*.
|
|
|
|
Uses SQLite's backup() API to write snapshot pages into the live
|
|
database file, preserving the file's inode and WAL state so that
|
|
any other process still holding the DB open (gateway, dashboard,
|
|
another CLI session) sees the restored data on the next read,
|
|
instead of continuing to serve stale cached pages from a replaced
|
|
inode. Replacing the file (``unlink() + move()``) under a live
|
|
connection leaves that connection serving its cached pre-restore
|
|
pages while new connections see the snapshot, a partial and
|
|
inconsistent state.
|
|
|
|
Falls back to the unlink+move approach on failure ONLY when no other
|
|
process or in-process connection holds the file: replacing the inode
|
|
under a live holder is the split-brain, so that branch fails
|
|
closed (returns ``False``) and the caller reports the file as skipped.
|
|
Callers go through ``restore_from``, which validates *src* and holds
|
|
the maintenance reservation and epoch floor around this publication.
|
|
|
|
``before_publish`` runs once before the first byte of *dst* changes, while no other
|
|
connection can commit to it: a copy of what is about to be replaced taken there holds every
|
|
acknowledged commit. If it raises, nothing is published and the restore returns ``False``.
|
|
"""
|
|
from hermes_cli.sqlite_safe_read import connect_tracked
|
|
|
|
dst_conn: Optional[sqlite3.Connection] = None
|
|
try:
|
|
dst_conn = connect_tracked(str(dst), tracking_path=dst)
|
|
if before_publish is None:
|
|
try:
|
|
# Force a WAL checkpoint so the backup starts from a clean
|
|
# state rather than writing on top of a deep WAL.
|
|
dst_conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
|
|
except sqlite3.Error as exc:
|
|
# Optional: backup() still publishes over a deep WAL, just less tidily.
|
|
logger.debug("Pre-restore WAL checkpoint of %s skipped: %s", dst, exc)
|
|
src_conn = sqlite3.connect(read_only_db_uri(src), uri=True) # health: allow HX015 -- the backup being restored, not a live store
|
|
try:
|
|
if before_publish is None:
|
|
src_conn.backup(dst_conn)
|
|
else:
|
|
_backup_with_writers_held_off(src_conn, dst_conn, before_publish)
|
|
finally:
|
|
src_conn.close()
|
|
dst_conn.close()
|
|
# Restore original file permissions from the snapshot
|
|
try:
|
|
mode = src.stat().st_mode
|
|
dst.chmod(mode)
|
|
except OSError as exc:
|
|
# The data is restored; only the snapshot's file mode could not be carried over.
|
|
logger.warning("Restored %s but could not apply the snapshot's file mode: %s", dst, exc)
|
|
return True
|
|
except _PrePublishFailed as exc:
|
|
_close_quietly(dst_conn)
|
|
logger.error("Restore of %s stopped before publication: %s", dst, exc)
|
|
return False
|
|
except (sqlite3.Error, OSError, ValueError) as exc: # ValueError: an unrepresentable path
|
|
logger.warning("SQLite safe restore failed for %s -> %s: %s", src, dst, exc)
|
|
# Release our own handle on *dst* before the fallback: on Windows an
|
|
# open connection blocks unlink() with WinError 32, which would make
|
|
# the fallback below dead code exactly when it is needed.
|
|
if dst_conn is not None:
|
|
try:
|
|
dst_conn.close()
|
|
except sqlite3.Error as close_exc:
|
|
logger.debug("Closing %s before the fallback restore failed: %s", dst, close_exc)
|
|
return _replace_db_file(src, dst, before_publish)
|
|
|
|
|
|
class _PrePublishFailed(Exception):
|
|
"""Nothing was published and the fallback must not run: ``before_publish`` raised (the error is
|
|
``__cause__``), or the snapshot cannot be published with writers held off."""
|
|
|
|
|
|
def _backup_with_writers_held_off(src_conn: sqlite3.Connection, dst_conn: sqlite3.Connection,
|
|
before_publish: Callable[[], None]) -> None:
|
|
"""``src_conn.backup(dst_conn)`` with ``before_publish`` run under the destination's write lock.
|
|
|
|
SQLite takes the destination's write transaction on the first backup step and holds it until
|
|
the last one commits, so a one-page first step leaves a window where *dst* is still untouched
|
|
on disk and no other connection can commit. A source of one page would finish (and commit) in
|
|
that first step, and a one-page database holds no table, so it is refused.
|
|
"""
|
|
if src_conn.execute("PRAGMA page_count").fetchone()[0] < 2:
|
|
raise _PrePublishFailed("the snapshot holds no table; refusing to publish it")
|
|
ran = False
|
|
|
|
def _progress(status: int, _remaining: int, _total: int) -> None:
|
|
nonlocal ran
|
|
if ran or status != sqlite3.SQLITE_OK:
|
|
return
|
|
ran = True
|
|
try:
|
|
before_publish()
|
|
except Exception as exc: # health: allow BLE001 -- any failure stops the publication
|
|
raise _PrePublishFailed(str(exc)) from exc
|
|
|
|
src_conn.backup(dst_conn, pages=1, progress=_progress)
|
|
|
|
|
|
def _replace_db_file(src: Path, dst: Path, before_publish: Optional[Callable[[], None]]) -> bool:
|
|
"""The unlink+move fallback of ``_restore_db_pages``; ``False`` (logged) when refused or failed."""
|
|
from hermes_cli.sqlite_safe_read import (
|
|
LiveConnectionError,
|
|
offline_file_access,
|
|
)
|
|
|
|
# SQLite keeps a symlinked database's sidecars beside the file the link resolves to: replace
|
|
# that file and its sidecars, and leave the link in place.
|
|
target = Path(os.path.realpath(dst))
|
|
tmp = target.parent / f".{target.name}.snap_restore"
|
|
try:
|
|
# A scan that cannot finish reports a (-1, reason) holder, so an
|
|
# unknown answer refuses here the same as a live holder does.
|
|
holders = foreign_state_db_holders(target)
|
|
if holders:
|
|
# Replacing the inode under a live holder is the split-brain
|
|
# corruption class: the holder keeps writing through a
|
|
# deleted-inode fd, and removing its sidecars
|
|
# detaches the WAL index it is checkpointing through. The
|
|
# backup-API path above is the live-safe route; if it failed,
|
|
# fail closed rather than corrupt.
|
|
logger.error(
|
|
"Refusing unlink+move restore of %s: other processes hold "
|
|
"the database or its WAL open, or the holder scan could not "
|
|
"rule them out (%s). Stop them and retry.",
|
|
dst, holders,
|
|
)
|
|
return False
|
|
# The foreign-pid scan above deliberately excludes THIS process,
|
|
# but an in-process store handle (the agent's own handle during
|
|
# /snapshot restore, a second store handle, a read pool)
|
|
# is exactly as much of a live holder: unlinking the DB and its
|
|
# sidecars under it leaves this process on deleted-inode fds,
|
|
# the same split brain, produced first-party
|
|
# (``/proc/self/fd`` shows ``state.db-wal (deleted)`` right
|
|
# after this fallback runs under a tracked connection).
|
|
# ``offline_file_access`` fails CLOSED when any tracked
|
|
# connection to *dst* is live and holds the connection-lifecycle
|
|
# lock across the whole swap so no new connection can appear
|
|
# mid-replace.
|
|
with offline_file_access(target, what="unlink+move restore of"):
|
|
# Copied through SQLite, never byte for byte: a snapshot whose pages sit in its -wal
|
|
# has a main file that alone opens as an empty or unreadable database.
|
|
tmp.unlink(missing_ok=True)
|
|
if not _safe_copy_db(src, tmp):
|
|
logger.error("Refusing unlink+move restore of %s: no consistent copy of %s", dst, src)
|
|
return False
|
|
staged = verify_sqlite_integrity(tmp)
|
|
if staged["valid"] is not True:
|
|
logger.error("Refusing unlink+move restore of %s: the staged copy did not verify (%s)",
|
|
dst, staged["message"])
|
|
return False
|
|
tmp.chmod(src.stat().st_mode)
|
|
if before_publish is not None:
|
|
try:
|
|
before_publish()
|
|
except Exception as exc: # health: allow BLE001 -- any failure stops the publication
|
|
logger.error("Restore of %s stopped before publication: %s", dst, exc)
|
|
return False
|
|
target.unlink(missing_ok=True)
|
|
# Drop the destination's sidecars before installing the
|
|
# snapshot. The staged copy is a checkpointed ``sqlite3.backup()``
|
|
# image that owns no WAL, so any
|
|
# ``-wal``/``-shm`` still sitting here describes the database we
|
|
# just unlinked: an ungracefully killed gateway leaves them
|
|
# behind, which is exactly when a restore gets run. SQLite
|
|
# replays that foreign WAL over the restored file on the next
|
|
# open and the database comes up "malformed" (or silently
|
|
# resurrects post-snapshot rows). The backup excludes the same
|
|
# sidecars for the same reason.
|
|
for _sidecar_suffix in ("-wal", "-shm", "-journal"):
|
|
target.with_name(target.name + _sidecar_suffix).unlink(missing_ok=True)
|
|
shutil.move(str(tmp), str(target))
|
|
return True
|
|
except LiveConnectionError as exc2:
|
|
logger.error(
|
|
"Refusing unlink+move restore of %s: %s Close the in-process "
|
|
"database handles (or restart Hermes) and retry.",
|
|
dst, exc2,
|
|
)
|
|
return False
|
|
except (OSError, ValueError) as exc2: # ValueError: a path the OS cannot represent
|
|
logger.error("Fallback restore also failed for %s -> %s: %s", src, dst, exc2)
|
|
return False
|
|
finally:
|
|
with suppress(OSError, ValueError):
|
|
tmp.unlink(missing_ok=True)
|
|
|
|
|
|
class BackupAdmin(AdminFile):
|
|
def backup_to(self, dst: Path, *, timeout_seconds: float = 10.0) -> bool:
|
|
"""WAL-safe snapshot of the file into ``dst``; False, with ``dst`` removed, when none can be made."""
|
|
return _safe_copy_db(self.db_path, dst, timeout_seconds=timeout_seconds)
|
|
|
|
def preflight_backup(self) -> dict:
|
|
"""Publish an emergency snapshot beside the file; do not prune recovery files on failure."""
|
|
source = self.db_path
|
|
home = source.parent
|
|
if not source.exists():
|
|
return {"path": None, "message": "state.db not found (fresh install?)"}
|
|
prefix = "state.db.pre-update-emergency-"
|
|
_reclaim_abandoned_staging(home, prefix)
|
|
stamp = datetime.now(UTC).strftime("%Y-%m-%dT%H-%M-%S-%fZ")
|
|
destination = home / f"{prefix}{stamp}-{os.getpid()}.bak"
|
|
# Named before it exists, so a SIGTERM at any point after creation reaches the
|
|
# cleanup below; _safe_copy_db creates it owner-only and refuses a symlink.
|
|
staged = home / f"{prefix}{stamp}-{os.getpid()}.partial"
|
|
try:
|
|
if not _safe_copy_db(source, staged):
|
|
raise RuntimeError("SQLite safe copy failed; previous emergency snapshots were retained")
|
|
connection = sqlite3.connect(str(staged)) # health: allow HX015 -- staged copy written by this function
|
|
try:
|
|
result = connection.execute("PRAGMA quick_check").fetchall()
|
|
if result != [("ok",)]:
|
|
raise RuntimeError(f"SQLite snapshot integrity check failed: {result}")
|
|
finally:
|
|
connection.close()
|
|
size = staged.stat().st_size
|
|
os.replace(staged, destination)
|
|
finally:
|
|
staged.unlink(missing_ok=True)
|
|
# A copy stopped mid-write leaves the staging copy's rollback journal behind too.
|
|
Path(f"{staged}-journal").unlink(missing_ok=True)
|
|
for old in sorted(home.glob(f"{prefix}*.bak"), reverse=True)[2:]:
|
|
try:
|
|
old.unlink()
|
|
except OSError as exc:
|
|
_backup_sqlite_logger.warning("Could not prune emergency snapshot %s: %s", old, exc)
|
|
return {"path": str(destination), "bytes": size}
|
|
|
|
@staticmethod
|
|
def verify_file(path: Path, *, max_bytes: int = DEFAULT_INTEGRITY_CHECK_MAX_BYTES) -> Any:
|
|
"""``{valid, message, size}`` for the SQLite file at ``path``: size, header magic, then integrity_check, or a
|
|
cheap structural probe for a file over ``max_bytes``."""
|
|
return verify_sqlite_integrity(path, max_bytes=max_bytes)
|
|
|
|
def restore_from(self, snapshot: Path, *, before_publish: Optional[Callable[[], None]] = None) -> bool:
|
|
"""Restore only while holding the destination authority's maintenance reservation.
|
|
|
|
Returns ``False``, without touching the file, when *snapshot* fails the SQLite
|
|
integrity check, when the runtime owns the profile, or when a canonical
|
|
``state.db`` epoch floor cannot be proven. ``before_publish`` runs once right before the
|
|
file first changes, while no other connection can commit to it (see ``_restore_db_pages``).
|
|
"""
|
|
self._require_writable("restore_from")
|
|
from gateway.runtime_ownership import OwnershipConflict, exclusive_maintenance
|
|
|
|
src, dst = snapshot, self.db_path
|
|
# backup() copies pages without validating their contents; its fallback
|
|
# copies bytes even when SQLite rejected the source. Neither may touch the
|
|
# destination until the snapshot passes the existing bounded integrity policy.
|
|
source_check = verify_sqlite_integrity(src)
|
|
if not source_check["valid"]:
|
|
logger.error("Refusing SQLite restore from %s: %s", src, source_check["message"])
|
|
return False
|
|
try:
|
|
with exclusive_maintenance([dst.absolute().parent, dst.resolve().parent]):
|
|
with _restore_epoch_source(src, dst) as prepared:
|
|
return _restore_db_pages(prepared, dst, before_publish)
|
|
except (OwnershipConflict, OSError, sqlite3.Error) as exc:
|
|
logger.error("%s", exc)
|
|
return False
|
|
|
|
def restore_epoch_floor(self) -> int:
|
|
"""The file's ``runtime_epoch`` on a ``mode=ro`` connection, 0 when the file or the row is absent; an unreadable
|
|
epoch raises ``OSError`` pointing at transcript salvage."""
|
|
return _restore_destination_epoch(self.db_path)
|
|
|
|
def session_row_counts(self) -> Optional[tuple[int, int]]:
|
|
"""Return ``(sessions, messages)`` stored in the file.
|
|
|
|
Read-only and best effort. ``None`` means "unknown": a missing file, a
|
|
database that is not a Hermes session store, or one that cannot be read.
|
|
Callers must never read ``None`` as "zero rows": acting on an unreadable
|
|
database would mask the very loss this count exists to surface.
|
|
"""
|
|
path = self.db_path
|
|
if not path.is_file():
|
|
return None
|
|
try:
|
|
from hermes_cli.sqlite_safe_read import connect_tracked
|
|
|
|
conn = connect_tracked(read_only_db_uri(path), uri=True, tracking_path=path, connect_fn=sqlite3.connect)
|
|
except sqlite3.Error:
|
|
return None
|
|
try:
|
|
sessions = conn.execute("SELECT COUNT(*) FROM sessions").fetchone()[0]
|
|
messages = conn.execute("SELECT COUNT(*) FROM messages").fetchone()[0]
|
|
return int(sessions), int(messages)
|
|
except (sqlite3.Error, TypeError, ValueError):
|
|
return None
|
|
finally:
|
|
conn.close()
|
|
|
|
@staticmethod
|
|
def has_runtime_ledger_rows(path: Path) -> bool:
|
|
"""Whether the file at ``path`` holds any runtime ledger row; False when it is missing or unreadable."""
|
|
db_path = path
|
|
if not db_path.is_file():
|
|
return False
|
|
try:
|
|
from hermes_cli.sqlite_safe_read import connect_tracked
|
|
|
|
with closing(connect_tracked(read_only_db_uri(db_path), uri=True, timeout=5.0, tracking_path=db_path,
|
|
connect_fn=sqlite3.connect)) as conn:
|
|
return bool(conn.execute(_LEDGER_PROBE).fetchone()[0])
|
|
except sqlite3.Error:
|
|
return False # no ledger tables yet, or unreadable: nothing an older release would trip on
|
|
|
|
@staticmethod
|
|
def has_runtime_ledger_table(path: Path) -> bool:
|
|
"""Whether the file at ``path`` holds a ``session_admissions`` table, read on one ``mode=ro`` connection; an
|
|
unreadable file raises the store error."""
|
|
with translated(), closing(sqlite3.connect(read_only_db_uri(path), uri=True)) as conn: # health: allow HX015 -- quick-snapshot copy, never the live store
|
|
return conn.execute("SELECT 1 FROM sqlite_master WHERE name='session_admissions'").fetchone() is not None
|
|
|
|
@staticmethod
|
|
def recover_transcripts(
|
|
source: Path, output: Path, *, inspect_only: bool = False, work_dir: Optional[Path] = None,
|
|
chunk_size: int = 1_000, progress_cb: Optional[Callable[[dict[str, Any]], None]] = None,
|
|
allow_partial: bool = False,
|
|
) -> dict[str, Any]:
|
|
"""Salvage the canonical rows of ``source`` into the new file ``output``, never opening ``source`` itself:
|
|
the snapshot lives under ``work_dir`` (a temporary folder by default), rows are copied ``chunk_size`` at a
|
|
time with ``progress_cb`` called between chunks, and ``allow_partial`` keeps what can be salvaged (row-range
|
|
salvage of damaged tables, the page-level salvage when a canonical schema is unreadable) instead of refusing
|
|
a source that is not fully recoverable.
|
|
|
|
``inspect_only`` reports which canonical tables are readable instead, and writes nothing at ``output``.
|
|
"""
|
|
if inspect_only:
|
|
return _recovery.inspect_session_database(source, work_dir=work_dir)
|
|
return _recovery.recover_session_database(
|
|
source, output, work_dir=work_dir, chunk_size=chunk_size, progress_cb=progress_cb,
|
|
allow_partial=allow_partial,
|
|
)
|