refactor(state): the backend and the backup share one zeroed-file detector in sqlite_support

The removal gave the backend's dbfile module its own copy of hermes_cli.backup.is_zeroed_sqlite_file, because the backend does not import hermes_cli.backup, and the test that kept the copy equal to backup's went with the old store's parity tests. So the detector existed twice, plus the header probe is_zeroed_state_db falls back on, and a fix to the live-connection guard or the header test had to land in both copies. The function now lives once in sqlite_support, the neutral SQLite helper module both sides already import, moved from hermes_cli/backup.py unchanged; backup.py and the backend import it from there, and the backend's copy goes. is_zeroed_state_db keeps main's fallback probe for the case where the detector raises.

The new case checks that the backend, the backup and sqlite_support name the same function; it fails before this commit. Greptile raised the duplicate in review.
This commit is contained in:
yoniebans
2026-11-11 15:48:31 +01:00
parent 6ec576295d
commit 0edf4be2b3
4 changed files with 33 additions and 47 deletions

View File

@@ -20,8 +20,7 @@ from hermes_constants import (
)
from persistence.core.admin import AdminUnsupported
from persistence_assembly import open_store_admin, profile_scope, store_exists
from sqlite_support import RETIRED_GENERATION_DIR_SUFFIX
from hermes_cli.sqlite_safe_read import has_live_connection, read_header_bytes_preopen
from sqlite_support import RETIRED_GENERATION_DIR_SUFFIX, is_zeroed_sqlite_file
from agent.provider_media import GENERATED_SUBDIR
from hermes_cli.archive_safe import normalize_archive_parts
@@ -356,28 +355,6 @@ def _snapshot_db(src: Path, dst: Path) -> bool:
return not isinstance(admin, AdminUnsupported) and admin.backup_to(dst)
def is_zeroed_sqlite_file(path: Path, *, probe_bytes: int = 100, force: bool = False) -> bool:
"""True when *path* looks like the #68474 zeroed-state.db signature.
Only regular files qualify: probing a FIFO/device/socket could block indefinitely.
Signature: no ``SQLite format 3`` header and no data — either empty (size 0, the total-loss case,
#97568) or first *probe_bytes* all NUL. Used at SessionDB open and for snapshot diagnostics so a silent
all-zero file becomes a guided recovery instead of a generic failure.
"""
try:
if not path.is_file():
return False
except OSError:
return False
if not force and has_live_connection(path):
return False
head = read_header_bytes_preopen(path, length=max(16, probe_bytes), force=force)
# Empty or all-NUL header => zeroed; a real header (or unreadable) => not.
return head is not None and not head.startswith(b"SQLite format 3") and not any(head)
def _discard_failed_zip_members(zf: zipfile.ZipFile, filelist_len: int) -> None:
"""Drop the member(s) created by a failed write, both from the central directory and the file.

View File

@@ -28,6 +28,7 @@ from persistence.sqlite._state.common import (
FTS_REBUILD_DEFERRAL_KEY, path_is_under, stat_db_file_identity as _stat_db_file_identity
)
from persistence.sqlite._state.lockguard import own_descriptors
from sqlite_support import is_zeroed_sqlite_file
# Tests pin the "hermes_state" logger name.
logger = logging.getLogger("hermes_state")
@@ -741,29 +742,6 @@ def _preopen_header(path: Path, probe_bytes: int, force: bool) -> Optional[bytes
return None
def is_zeroed_sqlite_file(path: Path, *, probe_bytes: int = 100, force: bool = False) -> bool:
"""True when *path* looks like the #68474 zeroed-state.db signature.
Only regular files qualify: probing a FIFO/device/socket could block indefinitely.
Signature: no ``SQLite format 3`` header and no data — either empty (size 0, the total-loss case,
#97568) or first *probe_bytes* all NUL. Used at SessionDB open and for snapshot diagnostics so a silent
all-zero file becomes a guided recovery instead of a generic failure.
"""
try:
if not path.is_file():
return False
except OSError:
return False
from hermes_cli.sqlite_safe_read import has_live_connection, read_header_bytes_preopen
if not force and has_live_connection(path):
return False
head = read_header_bytes_preopen(path, length=max(16, probe_bytes), force=force)
# Empty or all-NUL header => zeroed; a real header (or unreadable) => not.
return head is not None and not head.startswith(b"SQLite format 3") and not any(head)
def is_zeroed_state_db(path: Path, *, probe_bytes: int = 100, force: bool = False) -> bool:
"""Detect the zeroed state.db signature (0-byte or NUL header). Prefers
:func:`is_zeroed_sqlite_file`; the header probe below answers when it raises."""

View File

@@ -1048,3 +1048,26 @@ def schema_not_built(exc: BaseException) -> bool:
RETIRED_GENERATION_DIR_SUFFIX = ".retired-wal-"
def is_zeroed_sqlite_file(path: Path, *, probe_bytes: int = 100, force: bool = False) -> bool:
"""True when *path* looks like the #68474 zeroed-state.db signature.
Only regular files qualify: probing a FIFO/device/socket could block indefinitely.
Signature: no ``SQLite format 3`` header and no data — either empty (size 0, the total-loss case,
#97568) or first *probe_bytes* all NUL. Used at SessionDB open and for snapshot diagnostics so a silent
all-zero file becomes a guided recovery instead of a generic failure.
"""
try:
if not path.is_file():
return False
except OSError:
return False
from hermes_cli.sqlite_safe_read import has_live_connection, read_header_bytes_preopen
if not force and has_live_connection(path):
return False
head = read_header_bytes_preopen(path, length=max(16, probe_bytes), force=force)
# Empty or all-NUL header => zeroed; a real header (or unreadable) => not.
return head is not None and not head.startswith(b"SQLite format 3") and not any(head)

View File

@@ -23,6 +23,14 @@ def test_is_zeroed_state_db_and_quarantine(tmp_path):
assert q.read_bytes() == bytes(1024)
def test_the_backend_and_the_backup_share_one_zeroed_file_detector():
import hermes_cli.backup
import sqlite_support
assert dbfile_module.is_zeroed_sqlite_file is hermes_cli.backup.is_zeroed_sqlite_file
assert hermes_cli.backup.is_zeroed_sqlite_file is sqlite_support.is_zeroed_sqlite_file
@pytest.mark.skipif(not hasattr(__import__("os"), "mkfifo"), reason="POSIX only")
def test_is_zeroed_never_probes_special_files(tmp_path):
"""A FIFO at the state.db path must be rejected without any blocking read.