~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(backup): encrypt failure status and retain purge warnings (#7443)
LearningCircuitb520f7d08b0d
diff --git a/.pre-commit-hooks/custom-checks.py b/.pre-commit-hooks/custom-checks.pyindex f73133c0d2..5f0df58df5 100755--- a/.pre-commit-hooks/custom-checks.py+++ b/.pre-commit-hooks/custom-checks.py@@ -372,6 +372,21 @@ def _is_raw_sql_exempt(filename: str) -> bool:return False+# Exact driver-level statements allowed in one exact file. The connector sets+# a fixed SQLCipher PRAGMA before any ORM exists, so it cannot use the ORM;+# every other line of that file is still checked.+_RAW_SQL_LINE_EXEMPTIONS = {+ ("src", "local_deep_research", "database", "sqlcipher_compat.py"): {+ 'cursor.execute("PRAGMA cipher_log_level = NONE")',+ },+}+++def _raw_sql_line_exemptions(filename: str) -> set:+ parts = Path(filename.replace("\\", "/")).parts[-4:]+ return _RAW_SQL_LINE_EXEMPTIONS.get(tuple(parts), set())++def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:"""Check for raw SQL usage patterns."""errors = []@@ -383,6 +398,7 @@ def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:if _is_raw_sql_exempt(filename):return errors+ exempt_lines = _raw_sql_line_exemptions(filename)# More specific patterns for database execute calls to avoid false positivesdb_execute_patterns = [@@ -435,6 +451,7 @@ def check_raw_sql(content: str, filename: str) -> List[Tuple[int, str]]:or line_stripped.startswith('"""')or line_stripped.startswith("'''")or not line_stripped+ or line_stripped in exempt_lines):continuediff --git a/changelog.d/7199.bugfix.md b/changelog.d/7199.bugfix.mdnew file mode 100644index 0000000000..433d7830a4--- /dev/null+++ b/changelog.d/7199.bugfix.md@@ -0,0 +1 @@+Show the latest backup failure in settings. Safe failure metadata is written to the encrypted user database; once that write succeeds it survives server restarts and is shared across workers, and a result observed while the database is closed is kept in memory until it can be written. A backup directory the server cannot list is no longer shown as empty, in the panel or in the "no backups" warning, and a password change that cannot list the old backups is recorded as a failed attempt. When the user's database cannot be opened to read the backup setting, the panel shows it as unknown instead of disabled.diff --git a/changelog.d/7443.security.md b/changelog.d/7443.security.mdnew file mode 100644index 0000000000..b569f0eee9--- /dev/null+++ b/changelog.d/7443.security.md@@ -0,0 +1 @@+Backup attempt status is kept in the encrypted user database, with no plaintext auth-database fallback; while that database cannot be written it is held in bounded process memory and written at the next opportunity. Password-change cleanup that fails, cannot inspect old backups, is skipped (the rekey lock stayed held, or a later change took over), or cannot start (a rekey-lock or epoch fault, or an unusable backup directory) is recorded as incomplete and remains visible through later backups until another purge verifies removal of old database, salt and temporary files. Opening a SQLCipher connection now sets SQLCipher's process-wide native log level to `NONE` (`PRAGMA cipher_log_level`), so driver errors are no longer written directly to stderr outside the application's log redaction; this applies to every SQLCipher connection in the process.diff --git a/docs/security/database-backup.md b/docs/security/database-backup.mdindex a14cdc296b..d686e152f9 100644--- a/docs/security/database-backup.md+++ b/docs/security/database-backup.md@@ -49,6 +49,61 @@ matching application version and cipher configuration before relying on it.## Settings+The backup status panel shows the latest failed attempt and a fixed error+description, including failures before a migration or after a password change.+A successful new backup clears an ordinary backup failure; reusing a daily+backup does not erase it. An incomplete password-change purge remains visible+after ordinary successful backups. Only another purge that verifies all old+backup files were removed can clear that warning. Listing failures, denied+deletions and a directory becoming unreadable during cleanup are failures to+confirm the purge, even when a fresh current-password backup succeeds. A+password change whose purge is skipped (the rekey lock stayed held, a later+change took over, the change could not be fenced, or the backup service+resolved another database) or fails before it starts (a rekey-lock or epoch+fault, or the backup directory cannot be set up) is recorded the same way,+because nothing was removed. Only a purge run by a later password change can+clear the warning: the recovery purge at login after an interrupted change+does not report status, because it keeps backups it cannot classify.++Attempt timestamps, outcomes and fixed error codes are account activity. They+are stored only in the encrypted user database, never in the plaintext central+account database. Status survives restart and is shared across workers once it+has been written to that encrypted store. Status reporting writes through the+user's already-open database connection and never reopens a logged-out+database with a cached password. The purge after a password change is handled+separately: it runs under the rekey lock, which the open-connection path would+wait on, so it records its result over a connection opened with the new+password the user just entered, the same way the fresh backup itself is+exported. If encryption, the connection, or status storage is unavailable, the+serving worker keeps only a bounded memory observation, explicitly reports+unavailable persistence, and writes that observation to the encrypted store at+the next opportunity (the next backup attempt, the next status read, or the+daily backup check at login). It is lost if the worker restarts first.++Workers merge their observations. The newest attempt wins. The+password-change purge state is changed only by a purge observation, and a+newer one wins, except that on a timestamp tie or clock step-back+"incomplete" wins. A stored purge state is never resent, so an ordinary backup+never clears a warning and never revives one from a stale copy. A purge+observation that a worker could not write yet travels with that worker's next+write; if it is "incomplete" it can then override a newer clean purge from+another worker. That is deliberate: a spurious warning is cleared by the next+verified purge, while a spurious all-clear would hide old-password backups.+Passwords, backup paths and exception text are not part of the status record.++The panel distinguishes a backup directory the server cannot list from an+empty one, as far as the listing call reports the error, and an unreadable+backup setting from a disabled setting. Daily backup reuse and ordinary+retention still use `Path.glob`, which can suppress scan errors, but they+cannot clear an incomplete-purge warning. To remove backups made with the+previous password, restore directory access and change the password again,+which repeats the purge. The purge removes only regular files: a symlink or a+directory with a backup name is left in place and is not counted as a+remaining backup. An entry it cannot inspect (for example in a directory that+can be listed but not searched) is counted, and an unexpected error during+cleanup is recorded as an incomplete purge. Reporting failure never changes+the actual result of creating a fresh backup.+Configure backup behavior in Settings > Backup:| Setting | Default | Description |diff --git a/src/local_deep_research/database/backup/backup_executor.py b/src/local_deep_research/database/backup/backup_executor.pyindex 8c7afe0609..a43ac21ab7 100644--- a/src/local_deep_research/database/backup/backup_executor.py+++ b/src/local_deep_research/database/backup/backup_executor.py@@ -11,6 +11,11 @@ from typing import Optionalfrom loguru import loggerfrom .backup_service import BackupResult, BackupService+from .backup_status import (+ BackupError,+ error_from_exception,+ record_backup_result,+)class BackupExecutor:@@ -81,13 +86,19 @@ class BackupExecutor:self._pending_backups.add(username)# Submit backup to thread pool- future = self._executor.submit(- self._run_backup,- username,- password,- max_backups,- max_age_days,- )+ try:+ future = self._executor.submit(+ self._run_backup,+ username,+ password,+ max_backups,+ max_age_days,+ )+ except Exception:+ with self._pending_lock:+ self._pending_backups.discard(username)+ record_backup_result(username, False, BackupError.SCHEDULING)+ raise# Add callback to remove from pending set when donefuture.add_done_callback(lambda f: self._backup_completed(username, f))@@ -133,6 +144,7 @@ class BackupExecutor:except Exception as e:logger.warning("Background backup error")+ record_backup_result(username, False, error_from_exception(e))return BackupResult(success=False, error=str(e))def _backup_completed(self, username: str, future) -> None:diff --git a/src/local_deep_research/database/backup/backup_service.py b/src/local_deep_research/database/backup/backup_service.pyindex 45d3806007..56ae49c776 100644--- a/src/local_deep_research/database/backup/backup_service.py+++ b/src/local_deep_research/database/backup/backup_service.py@@ -4,6 +4,7 @@ Uses sqlcipher_export() for safe atomic backups that preserve encryptionand work correctly with WAL mode."""+import fnmatchimport hmacimport osimport re@@ -12,13 +13,22 @@ import sqlite3import tempfileimport threadingimport time+from contextlib import contextmanagerfrom dataclasses import dataclassfrom datetime import UTC, datetime, timedeltafrom pathlib import Path+from stat import S_ISLNK, S_ISREGfrom typing import Optionalfrom loguru import logger++from .backup_status import (+ BackupError,+ error_from_exception,+ persist_pending_backup_status,+ record_backup_result,+)from ...utilities.resource_utils import safe_closefrom ...config.paths import (@@ -235,6 +245,42 @@ def is_safe_glob_result(path: Path, base_dir: Path) -> bool:return False+def list_backup_files(+ backup_dir: Path, *, include_auxiliary: bool = False+) -> list[Path]:+ """List the ``ldr_backup_*.db`` entries of ``backup_dir``, or raise.++ ``Path.glob`` swallows the ``PermissionError`` and other ``OSError`` that+ scanning a directory raises, so an unreadable directory looks exactly like+ an empty one. ``Path.iterdir`` raises instead, which lets the settings+ panel tell "could not be read" from "no backups yet". Like ``glob`` this+ returns every matching name, symlinks included, so callers still apply+ ``is_safe_glob_result``.++ Raises:+ OSError: ``backup_dir`` is missing, is not a directory or cannot be+ read.+ """+ patterns = ["ldr_backup_*.db"]+ if include_auxiliary:+ patterns += [+ "ldr_backup_*.db.salt",+ "ldr_backup_*.db.tmp",+ "ldr_backup_*.db.tmp.salt",+ ]+ return [+ entry+ for entry in backup_dir.iterdir()+ if any(fnmatch.fnmatch(entry.name, pattern) for pattern in patterns)+ or (+ include_auxiliary+ and _QUARANTINED_BACKUP_NAME_RE.fullmatch(+ entry.name.removesuffix(".salt")+ )+ )+ ]++def pop_user_lock(username: str) -> None:"""Compatibility no-op: per-user backup locks keep stable identity.@@ -249,6 +295,43 @@ def pop_user_lock(username: str) -> None:del username+@contextmanager+def password_status_session(db_path: Path, password: str):+ """Yield a session on an existing user database keyed with ``password``.++ Only for recording a password-change purge result with the new password+ the user just entered (see ``BackupService._password_status_session``).+ Raises ``FileNotFoundError`` for a missing database, and whatever opening+ raises for a wrong key. The existence check is best effort, not atomic+ with the open: a file removed in between is created empty by SQLite,+ keyed with ``password`` and passes the key check, so the status row+ would land in a new encrypted database at that path. Nothing in the app+ deletes a user database, so only an outside actor can open that window.+ (``create_sqlcipher_connection`` needs a plain path for the salt lookup,+ so a ``mode=rw`` URI is not used.)+ """+ from sqlalchemy import create_engine+ from sqlalchemy.orm import Session+ from sqlalchemy.pool import NullPool++ from ..sqlcipher_compat import get_sqlcipher_module++ if not db_path.is_file():+ raise FileNotFoundError("User database not found")+ engine = create_engine(+ "sqlite://",+ creator=lambda: create_sqlcipher_connection(str(db_path), password),+ module=get_sqlcipher_module().dbapi2,+ poolclass=NullPool,+ hide_parameters=True,+ )+ try:+ with Session(engine) as session:+ yield session+ finally:+ engine.dispose()++@dataclassclass BackupResult:"""Result of a backup operation."""@@ -257,6 +340,27 @@ class BackupResult:backup_path: Optional[Path] = Noneerror: Optional[str] = Nonesize_bytes: int = 0+ error_code: Optional[BackupError] = None+ # Set by purge_and_refresh: whether old-password files may remain.+ purge_incomplete: Optional[bool] = None+++@contextmanager+def _report_backup_errors(username: str):+ """Record an unexpected exception raised outside the export, then re-raise it.++ The export reports its own outcome. This covers the work around it, such+ as checking today's backups or purging old ones, so a failure there does+ not leave an older successful backup looking current. It is not a check+ of the backup directory: ``Path.glob`` swallows directory-scan errors, so+ an unreadable directory is not raised here. The settings panel detects+ that with ``list_backup_files``.+ """+ try:+ yield+ except Exception as exc:+ record_backup_result(username, False, error_from_exception(exc))+ raiseclass BackupService:@@ -275,6 +379,8 @@ class BackupService:password: str,max_backups: int = 1,max_age_days: int = 7,+ *,+ record_failure: bool = True,):"""Initialize backup service.@@ -286,6 +392,10 @@ class BackupService:max_age_days: Delete backups older than this many days. Zero,a negative value, or a value too large to turn into a datemeans no age limit.+ record_failure: If False, a failure to resolve the paths is+ only raised, not recorded. The password-change refresh+ records it itself: it holds the rekey sidecar, which the+ default (open-engine) status write would wait on."""self.username = usernameself.password = password@@ -293,9 +403,14 @@ class BackupService:self.max_age_days = max_age_days# Get paths- self.db_filename = get_user_database_filename(username)- self.db_path = get_encrypted_database_path() / self.db_filename- self.backup_dir = get_user_backup_directory(username)+ try:+ self.db_filename = get_user_database_filename(username)+ self.db_path = get_encrypted_database_path() / self.db_filename+ self.backup_dir = get_user_backup_directory(username)+ except Exception as exc:+ if record_failure:+ record_backup_result(username, False, error_from_exception(exc))+ raisedef create_backup(self, force: bool = False, prune: bool = True@@ -336,7 +451,10 @@ class BackupService:BackupResult with success status and backup path"""# Acquire per-user lock to prevent concurrent backup operations- with _get_user_lock(self.username):+ with (+ _get_user_lock(self.username),+ _report_backup_errors(self.username),+ ):# Skip if a backup already exists for today (unless forced)if not force:today = datetime.now(UTC).strftime("%Y%m%d")@@ -353,6 +471,11 @@ class BackupService:"Backup already exists for today "f"({existing_path.name}), skipping")+ # No new attempt is recorded here, so save any status+ # observed while the database was closed (such as an+ # incomplete password-change purge) before a restart+ # can lose it.+ persist_pending_backup_status(self.username)return BackupResult(success=True,backup_path=existing_path,@@ -379,12 +502,65 @@ class BackupService:)return result- def _create_backup_impl(self, prune: bool = True) -> BackupResult:- """Internal implementation of backup creation (must be called with lock held)."""+ def _create_backup_impl(+ self, prune: bool = True, purge_incomplete: bool | None = None+ ) -> BackupResult:+ """Internal implementation of backup creation (must be called with lock held).++ Ordinary backups preserve an incomplete password-change purge. Only+ a purge verified to have removed all old files clears that warning.++ A purge result (``purge_incomplete`` given) is written over a+ connection opened with this service's password, the same way the+ export itself opens the database, before the open engine is tried:+ the purge runs under the rekey sidecar, which the open-engine status+ path would wait on, and the engine may not be open at all.+ """+ direct = (+ self._password_status_session+ if purge_incomplete is not None+ else None+ )+ try:+ result = self._export_backup(prune=prune)+ except Exception as exc:+ record_backup_result(+ self.username,+ False,+ error_from_exception(exc),+ purge_incomplete=purge_incomplete,+ direct_session=direct,+ )+ raise+ record_backup_result(+ self.username,+ result.success,+ result.error_code,+ purge_incomplete=purge_incomplete,+ direct_session=direct,+ )+ return result++ @contextmanager+ def _password_status_session(self):+ """Yield a session on the user database keyed with this password.++ Used only to record a password-change purge result. That runs under+ the rekey sidecar (``refresh_backups_after_password_change``), so the+ open-engine status path, which takes the same sidecar, would stall+ until its wait bound. Refuses a database that is already missing; see+ ``password_status_session`` for the remaining race.+ """+ with password_status_session(self.db_path, self.password) as session:+ yield session++ def _export_backup(self, prune: bool = True) -> BackupResult:+ """Export and verify a new backup while the caller holds the user lock."""if not self.db_path.exists():return BackupResult(success=False,error=f"Database not found: {self.db_path}",+ error_code=BackupError.SOURCE_MISSING,)# Check available disk space@@ -396,6 +572,7 @@ class BackupService:return BackupResult(success=False,error=f"Insufficient disk space. Need {db_size * 2} bytes, have {free_space}",+ error_code=BackupError.INSUFFICIENT_SPACE,)except OSError as e:# Fail closed - don't proceed with backup if we can't verify disk space@@ -403,6 +580,7 @@ class BackupService:return BackupResult(success=False,error=f"Could not verify disk space: {e}",+ error_code=error_from_exception(e),)# Reserve a unique, owner-only temporary file. Forced backups can run@@ -529,6 +707,7 @@ class BackupService:return BackupResult(success=False,error="Backup verification failed - backup was corrupted",+ error_code=BackupError.VERIFICATION,)# Set restrictive permissions (owner read/write only)@@ -576,6 +755,7 @@ class BackupService:return BackupResult(success=False,error="Backup files changed during publication",+ error_code=BackupError.PUBLICATION,)logger.info(@@ -619,6 +799,7 @@ class BackupService:return BackupResult(success=False,error=str(e),+ error_code=error_from_exception(e),)def _verify_and_identify(@@ -1094,63 +1275,177 @@ class BackupService:] + self._quarantined_backup_paths()def purge_and_refresh(self) -> "BackupResult":- """Delete all existing backups and create a fresh one.+ """Remove old-key backup files and create a current-password backup.- Used after a password change to replace old-key backups with a- new backup encrypted under the current password. Old backups- encrypted with a previous password are a security risk (NIST- SP 800-57, OWASP A02) because they remain decryptable with the- old (potentially compromised) password.-- Returns:- BackupResult from the fresh backup creation+ The result describes creation of the fresh backup. Status separately+ retains any incomplete purge until a later verified purge succeeds.+ Listing or unlink errors never imply that old-key files were removed."""# Hold per-user lock for the entire purge+create operation to# prevent a concurrent backup from writing an old-key backup# between the purge and the fresh backup creation.- with _get_user_lock(self.username):- # Forget results obtained with the old password, so a backup the- # purge cannot delete is checked again with the new one.- _get_backup_verifications(self.username).clear()+ # Failures are recorded here and in _create_backup_impl, through the+ # password session; _report_backup_errors would record again through+ # the open engine, which waits on the sidecar this caller holds.+ try:+ user_lock = _get_user_lock(self.username)+ except Exception as exc:+ self.record_purge_incomplete(error_from_exception(exc))+ raise+ with user_lock:+ try:+ purge_incomplete = self._purge_old_backup_files()+ except Exception as exc:+ # Nothing proves the old-password files are gone.+ self.record_purge_incomplete(error_from_exception(exc))+ raise+ result = self._create_backup_impl(purge_incomplete=purge_incomplete)+ result.purge_incomplete = purge_incomplete+ return result- # Delete all existing backup files, including quarantined- # damaged copies (they are still under the old key).- # list_backups() reports only bare names from a filtered glob,- # but re-check each joined path before removing it, so an entry- # that is not a bare in-root name (a poisoned listing or a future- # caller) cannot redirect the unlink outside backup_dir.- # is_safe_glob_result also skips a symlink entry. The check is a- # snapshot, not atomic with the unlink.- for backup_path in self._managed_backup_paths():- if not is_safe_glob_result(backup_path, self.backup_dir):- logger.warning(- f"Refusing to delete backup {backup_path.name}: it "- "is a symlink or resolves outside the backup directory"- )- continue- try:- self._remove_backup_files(backup_path)- logger.debug(f"Purged old-key backup: {backup_path.name}")- except OSError:- logger.warning(- f"Could not delete backup {backup_path.name}"- )+ def record_purge_incomplete(+ self, error_code: BackupError = BackupError.PURGE_INCOMPLETE+ ) -> None:+ """Record that old-password backups may remain after a password change.- # Also clean up any stale .tmp files- for tmp_file in [- f- for f in self.backup_dir.glob("ldr_backup_*.db.tmp")- if is_safe_glob_result(f, self.backup_dir)- ]:- try:- self._remove_backup_files(tmp_file)- except OSError:- logger.warning(- f"Could not delete stale tmp file {tmp_file.name}"- )+ For a purge that failed or was skipped. Writes through this+ service's password first (see ``_password_status_session``).+ """+ record_backup_result(+ self.username,+ False,+ error_code,+ purge_incomplete=True,+ direct_session=self._password_status_session,+ )++ def _purge_old_backup_files(self) -> bool:+ """Delete old-password backup files; return whether any may remain.++ Must be called with the user's backup lock held.+ """+ # Forget results obtained with the old password, so a backup the+ # purge cannot delete is checked again with the new one.+ _get_backup_verifications(self.username).clear()++ # Use one raising enumeration for the actual deletion set;+ # glob/list_backups can silently turn a scan failure into [].+ purge_incomplete = False+ try:+ old_files = list_backup_files(+ self.backup_dir, include_auxiliary=True+ )+ except FileNotFoundError:+ old_files = []+ except OSError:+ old_files = []+ purge_incomplete = True+ logger.warning("Could not list old backups after a password change")++ for old_file in old_files:+ if not self._is_purgeable(old_file):+ continue+ # Re-check containment before deleting, so a listing entry that+ # is a symlink or lies outside backup_dir (a poisoned listing or+ # a future caller) cannot redirect the unlink. _is_purgeable+ # counts an entry it cannot inspect, so it can pass one that this+ # check cannot confirm; leave that entry in place. On Python+ # 3.12/3.13 Path.is_symlink raises on EACCES rather than+ # returning False, so an error here is a refusal too. The check+ # is a snapshot, not atomic with the unlink.+ try:+ contained = is_safe_glob_result(old_file, self.backup_dir)+ except OSError:+ contained = False+ if not contained:+ purge_incomplete = True+ logger.warning(+ "Refusing to delete an old backup that could not be "+ "confirmed inside the backup directory"+ )+ continue+ try:+ if old_file.name.endswith(".salt"):+ old_file.unlink(missing_ok=True)+ else:+ self._remove_backup_files(old_file)+ except OSError:+ purge_incomplete = True+ logger.warning(+ "Could not remove an old backup after a password change"+ )++ # An unlink failure, a changed directory, or an unreadable+ # post-purge listing must not become a clean success.+ try:+ remaining = [+ entry+ for entry in list_backup_files(+ self.backup_dir, include_auxiliary=True+ )+ if self._is_purgeable(entry)+ ]+ purge_incomplete = purge_incomplete or bool(remaining)+ except FileNotFoundError:+ pass+ except OSError:+ purge_incomplete = True++ return purge_incomplete- # Create fresh backup with current password (lock already held)- return self._create_backup_impl()+ def _is_purgeable(self, entry: Path) -> bool:+ """Whether ``entry`` counts as an old backup the purge must remove.++ Only regular files directly in the backup directory are backups the+ purge removes. A symlink or a directory that merely matches a backup+ name is never followed or deleted, so it is not counted as a+ remaining old-password backup either: counting it would keep the+ purge warning up forever with nothing a repeated purge could fix.++ Anything that cannot be inspected counts. This uses one ``os.lstat``+ rather than ``Path.is_symlink``/``is_file``/``exists`` or+ ``is_safe_glob_result``: on Python 3.14 those return False on any+ ``OSError``, so in a directory that can be listed but not searched+ every old backup would look absent and the purge would be recorded+ as verified clean.+ """+ try:+ info = os.lstat(entry)+ except FileNotFoundError:+ return False+ except OSError:+ return True+ if S_ISLNK(info.st_mode):+ logger.warning(+ "Ignoring a symlink with a backup name during a "+ "password-change purge"+ )+ return False+ if not S_ISREG(info.st_mode):+ logger.warning(+ "Ignoring a non-file entry with a backup name during a "+ "password-change purge"+ )+ return False+ from ...security.path_validator import resolve_allow_missing++ # Not dead: entries normally come from iterdir() of backup_dir, but+ # this keeps the purge from deleting (or counting) a regular file+ # elsewhere if a listing ever yields one (tests/security/+ # test_identity_paths_and_host_effects.py injects such paths).+ try:+ inside = entry.name not in ("", ".", "..") and (+ resolve_allow_missing(entry.parent)+ == resolve_allow_missing(self.backup_dir)+ )+ except OSError:+ return True+ if not inside:+ logger.warning(+ "Ignoring an entry outside the backup directory during a "+ "password-change purge"+ )+ return insidedef _open_backup_with_current_key(self, backup_path: Path):"""Return a raw connection to ``backup_path`` keyed with this password."""diff --git a/src/local_deep_research/database/backup/backup_status.py b/src/local_deep_research/database/backup/backup_status.pynew file mode 100644index 0000000000..d8a7edd425--- /dev/null+++ b/src/local_deep_research/database/backup/backup_status.py@@ -0,0 +1,503 @@+"""Last backup attempt, stored in the encrypted user database.++Status is written through the user's already-open encrypted connection. A+caller that is itself holding the user's new password (the password-change+purge) supplies a session on a connection it opens with that password, which+is tried first: that caller holds the rekey sidecar, and the open-engine path+would wait on that same sidecar. Status never reopens a database with a+cached password on its own, and never falls back to the plaintext auth+database.++When no encrypted connection is usable, the observation is kept only in a+bounded per-process cache and reported as unpersisted. It is written at the+next opportunity: the next recorded attempt, a status read, or a daily-reuse+backup run while the user's database is open.++Two independent facts are merged per column, so workers can write in any+order:++* the latest attempt (``completed_at``, ``success``, ``error_code``):+ the newest ``completed_at`` wins;+* the password-change purge state (``purge_incomplete``,+ ``purge_checked_at``): only a purge sets it, and the newest purge wins,+ except that on a timestamp tie or step-back "incomplete" wins.++Only a purge observation that has not been written yet travels with a write.+An ordinary backup carries such an observation from its own worker's cache;+a stored purge state is never resent. So an ordinary backup never clears a+pending warning. It can make an older "incomplete" from its own worker's+cache, never written until then, override a newer clean purge from another+worker: deliberately, since a spurious warning is cleared by the next+verified purge while a spurious all-clear would hide old-password backups.+"""++from collections import OrderedDict+from contextlib import contextmanager+from datetime import UTC, datetime+from enum import Enum+import errno+import threading+import sys++from loguru import logger+from sqlalchemy import (+ Boolean,+ Column,+ MetaData,+ String,+ Table,+ Integer,+ case,+ func,+ inspect,+ select,+ type_coerce,+)+from sqlalchemy.dialects.sqlite import insert+from sqlalchemy.schema import CreateTable+from sqlalchemy_utc import UtcDateTime+++class BackupError(str, Enum):+ SOURCE_MISSING = "source_missing"+ INSUFFICIENT_SPACE = "insufficient_space"+ STORAGE_UNAVAILABLE = "storage_unavailable"+ PERMISSION_DENIED = "permission_denied"+ CONFIGURATION = "configuration"+ VERIFICATION = "verification"+ PUBLICATION = "publication"+ SCHEDULING = "scheduling"+ PURGE_INCOMPLETE = "purge_incomplete"+ UNKNOWN = "unknown"+++_ERROR_MESSAGES = {+ BackupError.SOURCE_MISSING: "The source database could not be found.",+ BackupError.INSUFFICIENT_SPACE: "There is not enough free disk space for a backup.",+ BackupError.STORAGE_UNAVAILABLE: "Backup storage could not be accessed.",+ BackupError.PERMISSION_DENIED: "The server lacks permission to create the backup.",+ BackupError.CONFIGURATION: "The backup configuration could not be used.",+ BackupError.VERIFICATION: "The backup failed verification and was discarded.",+ BackupError.PUBLICATION: "The backup changed during publication and was discarded.",+ BackupError.SCHEDULING: "The backup could not be scheduled.",+ BackupError.PURGE_INCOMPLETE: "Cleanup of backups made before the password change is incomplete. Old-password backups may remain.",+ BackupError.UNKNOWN: "The backup could not be created.",+}++# Separate metadata permits best-effort status writes without depending on+# a user-schema migration succeeding first. This table is never in auth DB.+_attempts = Table(+ "user_backup_attempt_status",+ MetaData(),+ Column("username", String(80), primary_key=True),+ Column("completed_at", UtcDateTime, nullable=False),+ Column("success", Boolean, nullable=False),+ Column("error_code", String(32)),+ Column("purge_incomplete", Boolean, nullable=False, default=False),+ # When the purge state above was last determined by a purge; NULL if no+ # purge has run. Orders purge observations independently of attempts.+ Column("purge_checked_at", UtcDateTime),+)++# If storage is full/unavailable, still expose the latest local observation.+# Namespace by encrypted DB root so a data-root change cannot expose old state.+_recent: OrderedDict[tuple[str, str], dict] = OrderedDict()+_recent_lock = threading.Lock()+_RECENT_LIMIT = 1024+++def error_from_exception(exc: Exception) -> BackupError:+ """Classify a failure without retaining its possibly sensitive text."""+ if isinstance(exc, OSError):+ if exc.errno == errno.ENOSPC:+ return BackupError.INSUFFICIENT_SPACE+ if isinstance(exc, PermissionError):+ return BackupError.PERMISSION_DENIED+ return BackupError.STORAGE_UNAVAILABLE+ if isinstance(exc, ValueError):+ return BackupError.CONFIGURATION+ return BackupError.UNKNOWN+++def _purge_wins(new: dict, old: dict) -> bool:+ """Whether ``new``'s purge state replaces ``old``'s (as in _save_attempt).++ A strictly newer purge wins. On a timestamp tie, or an older timestamp+ from a clock step-back or a delayed worker, "incomplete" wins over+ "clean": a spurious warning is fixed by repeating the purge, while a+ spurious all-clear hides old-password backups.+ """+ new_at, old_at = new.get("purge_checked_at"), old.get("purge_checked_at")+ if new_at is None:+ return False+ if old_at is None or new_at > old_at:+ return True+ new_pending = bool(new.get("purge_incomplete"))+ if new_pending != bool(old.get("purge_incomplete")):+ return new_pending+ return new_at == old_at # The same observation: take the newer copy.+++def _remember(key: tuple[str, str], attempt: dict) -> None:+ """Merge ``attempt`` into the local observation, fact by fact."""+ with _recent_lock:+ previous = _recent.get(key)+ if previous is not None:+ latest = (+ attempt+ if attempt["completed_at"] >= previous["completed_at"]+ else previous+ )+ purge = attempt if _purge_wins(attempt, previous) else previous+ attempt = {+ "completed_at": latest["completed_at"],+ "success": latest["success"],+ "error_code": latest["error_code"],+ "persisted": latest.get("persisted", False),+ "purge_incomplete": bool(purge.get("purge_incomplete")),+ "purge_checked_at": purge.get("purge_checked_at"),+ "purge_persisted": purge.get("purge_persisted", True),+ }+ _recent[key] = attempt+ _recent.move_to_end(key)+ while len(_recent) > _RECENT_LIMIT:+ _recent.popitem(last=False)+++def _status_key(username: str) -> tuple[str, str]:+ from ...config.paths import get_data_directory++ # Namespace resolution must not create a directory just to remember an+ # observation in memory (including before the first encrypted DB opens).+ return str(get_data_directory() / "encrypted_databases"), username+++@contextmanager+def _encrypted_status_session(username: str):+ # A status observation cannot initialize a database manager. An available+ # encrypted user connection necessarily belongs to the already-loaded one.+ module = sys.modules.get(__name__.rsplit(".", 2)[0] + ".encrypted_db")+ db_manager = getattr(module, "db_manager", None)+ if db_manager is None:+ raise RuntimeError("Encrypted backup status is unavailable")+ if not db_manager.has_encryption or not db_manager.is_user_connected(+ username+ ):+ raise RuntimeError("Encrypted backup status is unavailable")+ session = db_manager.get_session(username) # noqa: raw-session+ if session is None:+ raise RuntimeError("Encrypted backup status is unavailable")+ with session:+ yield session+++def _save_attempt(session, username: str, attempt: dict) -> dict:+ """Merge ``attempt`` into the stored row and return the merged row.++ Each fact is taken from whichever side observed it last (see the module+ docstring), so a delayed or repeated write never loses newer state.+ """+ session.execute(CreateTable(_attempts, if_not_exists=True))+ # Send the purge state only while it is unwritten. A cached copy of a+ # stored state is stale once another worker has stored a newer one, and+ # the step-back rule below would let a stale "incomplete" win.+ purge_unwritten = not attempt.get("purge_persisted", True)+ statement = insert(_attempts).values(+ username=username,+ completed_at=attempt["completed_at"],+ success=attempt["success"],+ error_code=attempt["error_code"],+ purge_incomplete=purge_unwritten and bool(attempt["purge_incomplete"]),+ purge_checked_at=(+ attempt["purge_checked_at"] if purge_unwritten else None+ ),+ )+ new = statement.excluded+ newer_attempt = new.completed_at >= _attempts.c.completed_at+ # Mirrors _purge_wins: newer wins; on a tie or step-back, True wins.+ newer_purge = new.purge_checked_at.is_not(None) & (+ _attempts.c.purge_checked_at.is_(None)+ | (new.purge_checked_at > _attempts.c.purge_checked_at)+ | (new.purge_incomplete & ~_attempts.c.purge_incomplete)+ )+ # SQLite evaluates every SET expression against the original row.+ update = {+ name: case((newer_attempt, new[name]), else_=_attempts.c[name])+ for name in ("completed_at", "success", "error_code")+ }+ update.update(+ {+ name: case((newer_purge, new[name]), else_=_attempts.c[name])+ for name in ("purge_incomplete", "purge_checked_at")+ }+ )+ session.execute(+ statement.on_conflict_do_update(+ index_elements=["username"], set_=update+ )+ )+ try:+ row = dict(+ session.execute(+ select(_attempts).where(_attempts.c.username == username)+ )+ .mappings()+ .one()+ )+ except (ValueError, TypeError, OverflowError) as exc:+ row = _replace_unreadable_row(session, username, attempt, exc)+ session.commit()+ return {**row, "persisted": True, "purge_persisted": True}+++def _replace_unreadable_row(session, username, attempt, exc) -> dict:+ """Repair a stored row whose values cannot be parsed, after the upsert.++ One corrupt value (say a malformed ``completed_at``) would otherwise make+ every later write fail on the read-back, and its text comparison could+ keep the merge from ever replacing it. The upsert above has already+ merged this write into the row, so each column that still reads cleanly+ keeps its merged value. Only an unreadable column is replaced:++ * the attempt columns, together, by this write's attempt;+ * ``purge_incomplete`` by "incomplete", since nothing proves a clean+ purge (a newer purge this write carried was already written by the+ upsert, which leaves a readable flag);+ * ``purge_checked_at`` by NULL, so any later purge decides. The merge+ could not order against an unreadable timestamp, so a purge state this+ write carries then replaces both purge columns instead.+ """+ logger.warning(+ "Repairing an unreadable backup status row ({})", type(exc).__name__+ )+ owner = _attempts.c.username == username+ carried = not attempt.get("purge_persisted", True)++ def read(column):+ try:+ return True, session.execute(+ select(column).where(owner)+ ).scalar_one()+ except (ValueError, TypeError, OverflowError):+ return False, None++ row = {}+ attempt_columns = ("completed_at", "success", "error_code")+ values = [read(_attempts.c[name]) for name in attempt_columns]+ if all(ok for ok, _ in values):+ row.update(zip(attempt_columns, (value for _, value in values)))+ else:+ row.update(+ completed_at=attempt["completed_at"],+ success=bool(attempt["success"]),+ error_code=attempt["error_code"],+ )+ checked_ok, checked = read(_attempts.c.purge_checked_at)+ if carried and not checked_ok:+ row["purge_incomplete"] = bool(attempt["purge_incomplete"])+ row["purge_checked_at"] = attempt["purge_checked_at"]+ else:+ # Boolean results are not validated on read, so check the storage:+ # only an integer 0 or 1 is a readable flag.+ kind, flag = session.execute(+ select(+ func.typeof(_attempts.c.purge_incomplete),+ type_coerce(_attempts.c.purge_incomplete, Integer),+ ).where(owner)+ ).one()+ # A purge this write carries that is newer than the stored one was+ # already written as an integer by the upsert, so an unreadable flag+ # here is a stored state nothing has superseded: assume incomplete.+ row["purge_incomplete"] = (+ bool(flag) if kind == "integer" and flag in (0, 1) else True+ )+ row["purge_checked_at"] = checked if checked_ok else None+ session.execute(_attempts.update().where(owner).values(**row))+ return {"username": username, **row}+++def _is_pending(entry: dict | None) -> bool:+ """Whether a local observation holds anything not yet written."""+ return entry is not None and not (+ entry.get("persisted") and entry.get("purge_persisted", True)+ )+++def _persist(username: str, key, attempt: dict, direct_session=None) -> None:+ """Write ``attempt``, or raise if no encrypted session is usable.++ ``direct_session`` is tried first, then the open engine.+ """+ saved = None+ if direct_session is not None:+ try:+ with direct_session() as session:+ saved = _save_attempt(session, username, attempt)+ except Exception as exc:+ logger.debug(+ "Direct backup status session unusable ({})",+ type(exc).__name__,+ )+ if saved is None:+ with _encrypted_status_session(username) as session:+ saved = _save_attempt(session, username, attempt)+ _remember(key, saved)+++def record_backup_result(+ username: str,+ success: bool,+ error_code: BackupError | None = None,+ *,+ purge_incomplete: bool | None = None,+ direct_session=None,+) -> None:+ """Best-effort reporting must never change a backup's actual outcome.++ ``purge_incomplete`` is given only by a password-change purge: True when+ old-password files may remain, False when the purge verified none did.+ ``direct_session``, a zero-argument callable returning a session context+ manager on the user's encrypted database, is tried before the open+ engine (see the module docstring).+ """+ if not username:+ return+ try:+ key = _status_key(username)+ code = (+ None if success else BackupError(error_code or BackupError.UNKNOWN)+ )+ now = datetime.now(UTC)+ purge, purge_checked_at, purge_persisted = False, None, True+ if purge_incomplete is not None:+ purge, purge_checked_at, purge_persisted = (+ bool(purge_incomplete),+ now,+ False,+ )+ else:+ # Carry a purge state not yet written, with its own timestamp,+ # so this write persists it. A state already stored is not+ # resent: the database may have a newer one from another worker.+ with _recent_lock:+ previous = _recent.get(key, {})+ if not previous.get("purge_persisted", True):+ purge = bool(previous.get("purge_incomplete"))+ purge_checked_at = previous.get("purge_checked_at")+ purge_persisted = False+ attempt = {+ "completed_at": now,+ "success": bool(success),+ "error_code": code.value if code else None,+ "purge_incomplete": purge,+ "purge_checked_at": purge_checked_at,+ "persisted": False,+ "purge_persisted": purge_persisted,+ }+ _remember(key, attempt)+ _persist(username, key, attempt, direct_session)+ except Exception as exc:+ logger.warning(+ "Could not save encrypted backup status ({})", type(exc).__name__+ )+++def persist_pending_backup_status(username: str) -> None:+ """Write an unpersisted local observation if the user DB is now open.++ Called where a backup run ends without recording a new attempt (the+ daily-reuse early return), so an observation made while the database+ was closed, such as an incomplete password-change purge, does not stay+ process-local and vanish on restart.+ """+ if not username:+ return+ try:+ key = _status_key(username)+ with _recent_lock:+ local = _recent.get(key)+ if not _is_pending(local):+ return+ _persist(username, key, local)+ except Exception as exc:+ logger.debug(+ "Pending backup status not yet saved ({})", type(exc).__name__+ )+++def get_backup_status(username: str) -> dict:+ """Read this account's attempt; never substitute a plaintext store."""+ available = True+ attempt = None+ local = None+ try:+ key = _status_key(username)+ with _recent_lock:+ local = _recent.get(key)+ with _encrypted_status_session(username) as session:+ if _is_pending(local):+ # Persist an observation made before the encrypted DB+ # opened; the merged row comes back (and an unreadable one+ # is replaced).+ attempt = _save_attempt(session, username, local)+ _remember(key, attempt)+ elif inspect(session.connection()).has_table(_attempts.name):+ row = (+ session.execute(+ select(_attempts).where(+ _attempts.c.username == username+ )+ )+ .mappings()+ .first()+ )+ if row is not None:+ attempt = {**row, "persisted": True}+ except Exception as exc:+ available = False+ attempt = local+ logger.warning(+ "Could not read encrypted backup status ({})", type(exc).__name__+ )++ try:+ attempt = _report(attempt)+ except Exception as exc:+ # A malformed stored row must not break the settings panel.+ available, attempt = False, None+ logger.warning(+ "Could not format backup status ({})", type(exc).__name__+ )+ return {"status_available": available, "last_attempt": attempt}+++def _report(attempt: dict | None) -> dict | None:+ """Turn a stored or local observation into the public report."""+ if attempt is not None:+ pending = bool(attempt.get("purge_incomplete"))+ success = bool(attempt["success"]) and not pending+ try:+ code = (+ BackupError.PURGE_INCOMPLETE+ if pending and attempt["success"]+ else BackupError(attempt["error_code"] or BackupError.UNKNOWN)+ )+ except ValueError:+ code = BackupError.UNKNOWN+ error = None if success else _ERROR_MESSAGES[code]+ if pending and code != BackupError.PURGE_INCOMPLETE:+ error = (+ (error or "")+ + " "+ + _ERROR_MESSAGES[BackupError.PURGE_INCOMPLETE]+ )+ attempt = {+ "completed_at": attempt["completed_at"].isoformat(),+ "success": success,+ "error_code": None if success else code.value,+ "error": error,+ # Explicit, because error_code names the attempt's own failure+ # when a failed backup and an incomplete purge coincide.+ "purge_incomplete": pending,+ }+ return attemptdiff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.pyindex 7916fc0dfa..93e357c8d2 100644--- a/src/local_deep_research/database/encrypted_db.py+++ b/src/local_deep_research/database/encrypted_db.py@@ -2228,8 +2228,11 @@ class DatabaseManager:state = read_rekey_epoch_state(db_path)if state != RekeyEpochState(expected_epoch, None):return+ # record_failure=False: this purge records no status, and+ # the default failure record would go through the open+ # engine, whose claim waits on the sidecar held here.backup_service = BackupService(- username=username, password=password+ username=username, password=password, record_failure=False)# A pinned manager data_dir can differ from the backup# service's configured directory. The fence must vouch for@@ -2438,9 +2441,16 @@ class DatabaseManager:# integers within the settings' maximums, delete# nothing.retention = _read_backup_retention(engine)+ # record_failure=False: this cold open holds the rekey+ # sidecar, which the default failure record (through+ # the open-engine status path) would wait on. The+ # failure is logged below and the next login backup+ # records it.if retention is None:result = BackupService(- username=username, password=password+ username=username,+ password=password,+ record_failure=False,).create_backup(force=True, prune=False)else:max_backups, max_age_days = retention@@ -2449,6 +2459,7 @@ class DatabaseManager:password=password,max_backups=max_backups,max_age_days=max_age_days,+ record_failure=False,).create_backup(force=True)if result.success:logger.info(@@ -3543,12 +3554,30 @@ class DatabaseManager:which fences the login recovery path).Returns the ``BackupResult`` of the fresh backup, or ``None`` when- the refresh was skipped because this change no longer owns the- epoch: a later change purges and refreshes for itself, and a change+ the refresh was skipped (this change no longer owns the epoch, was+ not fenced, or the backup service targets another database): a later change purges and refreshes for itself, and a changewhose epoch was never published is resolved by the next successfulopen, which purges the superseded backups. Raises``RekeyLockContentionError`` if the sidecar stays held, so thecaller can report that old-key backups may remain.++ Every outcome except a purge that ran records an incomplete purge in+ the backup status: each skip, and every exception raised before+ ``purge_and_refresh`` started (contention, a sidecar or epoch fault,+ a failure to set up the backup service), which is then re-raised.+ Not recorded: a failure to resolve this user's database path, the+ first statement here, because there is then no database to record+ into; the route logs it.+ This refresh removed nothing in those cases, so old-key backups may+ remain. The status is written after the sidecar is released, through+ a connection opened with ``new_password``, never through a+ ``BackupService`` that could itself fail. A purge that ran records+ its own result, including an error after it (``purge_and_refresh``).+ The warning is cleared only by a later ``purge_and_refresh`` that+ verifies removal, i.e. another password change. The login-time+ recovery purge (``_purge_superseded_key_backups``) records no status,+ so it neither sets nor clears it: it lists with ``Path.glob`` and+ keeps backups it cannot classify, so it cannot vouch for removal."""from .backup.backup_service import BackupService@@ -3560,34 +3589,83 @@ class DatabaseManager:f"Backup refresh skipped for {username}: no committed ""password change on this thread to fence it against")+ self._record_backup_refresh_skipped(username, db_path, new_password)return Noneexpected_epoch = recorded[1]- with acquire_rekey_lock(- db_path,- local_wait=10.0,- contention_retry=_REKEY_CONTENTION_RETRY_SECONDS,- ):- state = read_rekey_epoch_state(db_path)- if state != RekeyEpochState(expected_epoch, None):- logger.info(- f"Backup refresh skipped for {username}: the rekey "- "epoch moved or is unresolved since this change"+ refresh_started = False+ skipped = False+ try:+ with acquire_rekey_lock(+ db_path,+ local_wait=10.0,+ contention_retry=_REKEY_CONTENTION_RETRY_SECONDS,+ ):+ state = read_rekey_epoch_state(db_path)+ if state != RekeyEpochState(expected_epoch, None):+ logger.info(+ f"Backup refresh skipped for {username}: the rekey "+ "epoch moved or is unresolved since this change"+ )+ skipped = True+ else:+ backup_service = BackupService(+ username=username,+ password=new_password,+ record_failure=False,+ )+ # A pinned manager data_dir can differ from the backup+ # service's configured directory. The fence must vouch+ # for the database whose backups would actually be+ # deleted.+ if backup_service.db_path != db_path:+ logger.warning(+ f"Backup refresh skipped for {username}: backup "+ "service targets a different database"+ )+ skipped = True+ else:+ refresh_started = True+ return backup_service.purge_and_refresh()+ except Exception:+ if not refresh_started:+ self._record_backup_refresh_skipped(+ username, db_path, new_password)- return None- backup_service = BackupService(- username=username, password=new_password+ raise+ if skipped:+ self._record_backup_refresh_skipped(username, db_path, new_password)+ return None++ @staticmethod+ def _record_backup_refresh_skipped(+ username: str, db_path: Path, new_password: str+ ) -> None:+ """Report that old-key backups may remain; never raises.++ Builds no ``BackupService``: its setup may be what failed.+ """+ try:+ from .backup.backup_service import password_status_session+ from .backup.backup_status import (+ BackupError,+ record_backup_result,+ )++ record_backup_result(+ username,+ False,+ BackupError.PURGE_INCOMPLETE,+ purge_incomplete=True,+ direct_session=lambda: password_status_session(+ db_path, new_password+ ),+ )+ except Exception as error:+ logger.warning(+ "Could not record the skipped backup refresh: "+ f"{type(error).__name__}")- # A pinned manager data_dir can differ from the backup- # service's configured directory. The fence must vouch for the- # database whose backups would actually be deleted.- if backup_service.db_path != db_path:- logger.warning(- f"Backup refresh skipped for {username}: backup "- "service targets a different database"- )- return None- return backup_service.purge_and_refresh()def user_exists(self, username: str) -> bool:"""Check if a user exists in the auth database."""diff --git a/src/local_deep_research/database/sqlcipher_compat.py b/src/local_deep_research/database/sqlcipher_compat.pyindex a91efa08a5..e41888eb2c 100644--- a/src/local_deep_research/database/sqlcipher_compat.py+++ b/src/local_deep_research/database/sqlcipher_compat.py@@ -240,7 +240,26 @@ def connect_sqlcipher(driver, database, **kwargs):driver.Cursor,bool(kwargs.get("check_same_thread", True)),)- return driver.connect(database, **kwargs)+ connection = driver.connect(database, **kwargs)+ # Native SQLCipher errors are written by the C library straight to+ # stderr, bypassing Python/Loguru sinks and their redaction, and can+ # disclose account activity (e.g. a failed key) to container logs. Keep+ # them disabled; callers still receive the database exception. Older+ # versions without this PRAGMA ignore it, as SQLite does for unknown+ # PRAGMAs.+ from ..utilities.resource_utils import safe_close++ cursor = None+ try:+ cursor = connection.cursor()+ cursor.execute("PRAGMA cipher_log_level = NONE")+ except BaseException:+ safe_close(cursor, "SQLCipher privacy cursor", allow_none=True)+ safe_close(connection, "SQLCipher connection")+ raise+ else:+ safe_close(cursor, "SQLCipher privacy cursor")+ return connectiondef get_sqlcipher_module():diff --git a/src/local_deep_research/web/routers/auth.py b/src/local_deep_research/web/routers/auth.pyindex 5883655a39..98ac00fd1e 100644--- a/src/local_deep_research/web/routers/auth.py+++ b/src/local_deep_research/web/routers/auth.py@@ -1514,7 +1514,10 @@ def change_password(# The manager reacquires the rekey sidecar and requires the epoch# this change committed before deleting anything, so a concurrent# (cross-process) change's fresh backup is never classified against- # a stale epoch. It returns None when a later change owns the epoch.+ # a stale epoch. It returns None when it skipped the purge (a later+ # change owns the epoch, the change was not fenced, or the backup+ # service targets another database); the backup status then records+ # that old-key backups may remain.try:result = db_manager.refresh_backups_after_password_change(username, new_password@@ -1522,16 +1525,26 @@ def change_password(if result is None:logger.info(f"Backup refresh after password change skipped for "- f"{_log_value_preview(username)}: a later rekey owns it"+ f"{_log_value_preview(username)}; backups under the old "+ "key may remain until a later purge")- elif result.success:+ elif result.success and not result.purge_incomplete:logger.info(f"Backups refreshed after password change for {_log_value_preview(username)}")+ elif result.success:+ logger.warning(+ f"New backup created after password change for "+ f"{_log_value_preview(username)}, but backups under the "+ "old key may remain"+ )else:+ # Whether the old-key backups were all removed is recorded+ # separately in the backup status (purge_incomplete).logger.error(f"Post-password-change backup failed for {_log_value_preview(username)}: "- f"{result.error}. Old backups were purged."+ f"{result.error}. See the backup status for whether "+ "old-key backups remain.")except RekeyLockContentionError:logger.exception(diff --git a/src/local_deep_research/web/routers/settings.py b/src/local_deep_research/web/routers/settings.pyindex f41aa7cc43..8ad317d3dc 100644--- a/src/local_deep_research/web/routers/settings.py+++ b/src/local_deep_research/web/routers/settings.py@@ -4571,12 +4571,40 @@ def api_get_warnings(def api_get_backup_status(request: Request, username: Annotated[str, Depends(require_auth)]):- """Get backup status for the current user."""+ """Get backup status for the current user.++ Always answers 200 to an authenticated caller. ``enabled`` is ``None``+ when reading the setting raises, for example because the user's database+ cannot be opened; a read error inside the settings table falls back to+ the default, as elsewhere. ``listing_available`` is ``False`` when the+ backup directory cannot be listed. Neither failure looks like "disabled"+ or "no backups yet".+ """+ if not username:+ return JSONResponse({"error": "Not authenticated"}, status_code=401)++ from ...database.backup.backup_status import get_backup_status+try:- from ...config.paths import get_user_backup_directory+ attempt_status = get_backup_status(username)+ except Exception:+ # get_backup_status already degrades on storage errors; this keeps+ # the documented always-200 contract for anything unforeseen.+ logger.exception("Error reading backup attempt status")+ attempt_status = {"status_available": False, "last_attempt": None}++ # Read the setting on its own: a settings failure must not be reported as+ # an unreadable backup directory. None means unknown, not disabled.+ backup_enabled = None+ try:+ backup_enabled = bool(+ _get_setting_from_session("backup.enabled", username, True)+ )+ except Exception:+ logger.exception("Error reading backup.enabled")- if not username:- return JSONResponse({"error": "Not authenticated"}, status_code=401)+ try:+ from ...config.paths import get_user_backup_directoryfrom ...utilities.formatting import human_size@@ -4585,9 +4613,14 @@ def api_get_backup_status(# Sort by modification time (not filename) for robustnessbackup_list = []total_size = 0- from ...database.backup.backup_service import is_safe_glob_result+ from ...database.backup.backup_service import (+ is_safe_glob_result,+ list_backup_files,+ )- for b in backup_dir.glob("ldr_backup_*.db"):+ # Not Path.glob: it swallows the error for an unreadable directory+ # and would report it as "no backups yet".+ for b in list_backup_files(backup_dir):# Symlink/traversal guard (main #4663): skip a planted symlink whose# target escapes the per-user backup dir, so its external file's# name/size/mtime can't be exfiltrated through this endpoint.@@ -4615,23 +4648,30 @@ def api_get_backup_status(for entry in backup_list:del entry["_mtime"]- backup_enabled = _get_setting_from_session(- "backup.enabled", username, True- )-return {- "enabled": bool(backup_enabled),+ "enabled": backup_enabled,+ "listing_available": True,"count": len(backup_list),"backups": backup_list,"total_size_bytes": total_size,"total_size_human": human_size(total_size),+ **attempt_status,}except Exception:logger.exception("Error getting backup status")- return JSONResponse(- {"error": "Failed to retrieve backup status"}, status_code=500- )+ # A storage/permission failure must not hide the very backup error+ # this status screen is meant to explain. Unknown is not an empty+ # directory; retain the attempt report and mark the listing unavailable.+ return {+ "enabled": backup_enabled,+ "listing_available": False,+ "count": None,+ "backups": [],+ "total_size_bytes": None,+ "total_size_human": None,+ **attempt_status,+ }@router.get("/api/ollama-status")diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.jsindex 4eedfa9d70..b3964f307e 100644--- a/src/local_deep_research/web/static/js/components/settings.js+++ b/src/local_deep_research/web/static/js/components/settings.js@@ -2541,8 +2541,33 @@.then(data => {const esc = window.escapeHtml || (s => String(s || ''));+ const lastAttempt = data.last_attempt;+ // With backups switched off, an old failure is history, not+ // news; an incomplete password-change purge still matters,+ // because old-password backups may remain on disk.+ const lastAttemptFailed = lastAttempt?.success === false && (+ data.enabled !== false+ || lastAttempt.purge_incomplete === true+ );let statusIcon, statusText, statusClass;- if (!data.enabled) {+ if (lastAttemptFailed) {+ statusIcon = 'fa-exclamation-triangle';+ statusText = 'Last backup attempt failed';+ statusClass = 'ldr-backup-warning';+ } else if (data.listing_available === false) {+ statusIcon = 'fa-exclamation-triangle';+ statusText = 'Backup files could not be listed';+ statusClass = 'ldr-backup-warning';+ } else if (data.status_available === false) {+ statusIcon = 'fa-exclamation-triangle';+ statusText = 'Backup attempt history is unavailable';+ statusClass = 'ldr-backup-warning';+ } else if (data.enabled === null) {+ // The setting could not be read: unknown, not disabled.+ statusIcon = 'fa-exclamation-triangle';+ statusText = 'Backup settings could not be read';+ statusClass = 'ldr-backup-warning';+ } else if (!data.enabled) {statusIcon = 'fa-times-circle';statusText = 'Backups disabled';statusClass = 'ldr-backup-disabled';@@ -2584,12 +2609,18 @@`;}- if (!data.enabled) {+ if (data.enabled === false) {html += `<div class="ldr-backup-hint"><small>Enable backups below to protect your data. Each backup uses disk space equal to your database size.</small></div>`;+ } else if (data.enabled === null) {+ // Unknown setting: the notice below says so; a hint here+ // could describe backups that are switched off.+ } else if (data.listing_available === false) {+ // Unreadable directory: the notice below gives the+ // remedy; a routine daily-backup hint would contradict it.} else if (data.count === 0) {html += `<div class="ldr-backup-hint">@@ -2665,6 +2696,35 @@}contentElement.innerHTML = html;++ if (lastAttemptFailed) {+ const warning = document.createElement('div');+ warning.className = 'ldr-backup-hint ldr-backup-failure';+ warning.setAttribute('role', 'alert');+ const date = new Date(lastAttempt.completed_at);+ const when = Number.isNaN(date.getTime()) ? '' : ` (${date.toLocaleString()})`;+ // Use textContent even if an alternate API supplies a+ // malformed error string or escapeHtml is unavailable.+ warning.textContent = `${lastAttempt.error || 'The backup could not be created.'}${when}`;+ contentElement.prepend(warning);+ }+ const notices = [];+ if (data.status_available === false) {+ notices.push('Backup attempt history is unavailable. Any result shown is the latest observation on this server.');+ }+ if (data.listing_available === false) {+ notices.push('The backup directory could not be read. Existing backups may still be present.');+ }+ if (data.enabled === null) {+ notices.push('Backup settings could not be read, so it is not known whether backups are enabled.');+ }+ notices.forEach(text => {+ const notice = document.createElement('div');+ notice.className = 'ldr-backup-hint ldr-backup-status-unavailable';+ notice.setAttribute('role', 'status');+ notice.textContent = text;+ contentElement.appendChild(notice);+ });}).catch(error => {SafeLogger.error('Error fetching backup status:', error);diff --git a/src/local_deep_research/web/warning_checks/__init__.py b/src/local_deep_research/web/warning_checks/__init__.pyindex c7de68f58a..8d194b3d8c 100644--- a/src/local_deep_research/web/warning_checks/__init__.py+++ b/src/local_deep_research/web/warning_checks/__init__.py@@ -379,7 +379,7 @@ def calculate_warnings(username: Optional[str] = None) -> List[dict]:if w:warnings.append(w)- # Check backup file status (lightweight filesystem glob)+ # Check backup file status (lightweight directory listing)dismiss_backup_info = settings_manager.get_setting("app.warnings.dismiss_backup_info", False)@@ -388,13 +388,18 @@ def calculate_warnings(username: Optional[str] = None) -> List[dict]:from ...utilities.formatting import human_sizefrom ...database.backup.backup_service import (is_safe_glob_result,+ list_backup_files,)if username:backup_dir = get_user_backup_directory(username)total_size = 0backup_count = 0- for f in backup_dir.glob("ldr_backup_*.db"):+ # Not Path.glob: it returns nothing for a directory it+ # cannot read, which would raise a false "no backups"+ # banner. A listing error skips both backup banners (see+ # the OSError handler below).+ for f in list_backup_files(backup_dir):# Skip symlinks / entries resolving outside backup_dir# so a planted symlink can't inflate the count/size# shown in warnings — same hardening as BackupService.@@ -424,6 +429,17 @@ def calculate_warnings(username: Optional[str] = None) -> List[dict]:)if w:warnings.append(w)+ except OSError as exc:+ # The backup directory, or an entry in it, could not be+ # accessed, so there is no count to base either banner on.+ # Skip both; the settings panel names the problem. Debug+ # level: this runs on every warnings poll. Exception type+ # only: no path and no secret.+ logger.debug(+ "Backup banners skipped: backup directory could not be "+ "accessed ({})",+ type(exc).__name__,+ )except Exception:logger.debug("Backup status check skipped")diff --git a/tests/conftest.py b/tests/conftest.pyindex 8e0614c0a4..f70fa6679a 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -1,3 +1,4 @@+import functoolsimport loggingimport osimport sys@@ -9,6 +10,7 @@ from contextlib import contextmanager, suppressfrom pathlib import Pathfrom unittest.mock import Mock, patch+import platformdirsimport pytestfrom loguru import loggerfrom sqlalchemy import create_engine, event@@ -461,6 +463,79 @@ def real_post_login_tasks(post_login_task_policy):return post_login_task_policy[email protected](scope="session", autouse=True)+def backup_status_store_policy():+ """Keep backup attempt reporting off the developer's real user databases.++ Backup tests often pin only service path helpers. Allow status access+ only when its actual data root is outside the platform's real root;+ otherwise skip writes and return no attempt. This also covers an already+ connected developer database without changing its credentials or sessions.+ Installed once because test executor threads can outlive their fixture.+ """+ try:+ from local_deep_research.config.paths import get_data_directory+ from local_deep_research.database.backup import backup_status+ except ImportError:+ # A broken install fails in the tests that need these modules; this+ # guard must not take every unrelated test down with it.+ yield+ return++ def resolves_to_real_data_dir() -> bool:+ try:+ real = Path(+ platformdirs.user_data_dir("local-deep-research")+ ).resolve()+ return get_data_directory().resolve().is_relative_to(real)+ except Exception:+ # An unusable LDR_DATA_DIR (relative, control characters) or a+ # stubbed path: the real store fails on its own without touching+ # anything, and the guard itself must never fail a test.+ return False++ real_record = backup_status.record_backup_result+ real_read = backup_status.get_backup_status+ real_flush = backup_status.persist_pending_backup_status++ @functools.wraps(real_record)+ def guarded_record(*args, **kwargs):+ if resolves_to_real_data_dir():+ return None+ return real_record(*args, **kwargs)++ @functools.wraps(real_read)+ def guarded_read(*args, **kwargs):+ if resolves_to_real_data_dir():+ return {"status_available": True, "last_attempt": None}+ return real_read(*args, **kwargs)++ @functools.wraps(real_flush)+ def guarded_flush(*args, **kwargs):+ if resolves_to_real_data_dir():+ return None+ return real_flush(*args, **kwargs)++ guards = {+ "record_backup_result": (real_record, guarded_record),+ "get_backup_status": (real_read, guarded_read),+ "persist_pending_backup_status": (real_flush, guarded_flush),+ }+ with pytest.MonkeyPatch.context() as patcher:+ for module in list(sys.modules.values()):+ module_name = getattr(module, "__name__", "")+ if not (+ isinstance(module_name, str)+ and module_name.startswith("local_deep_research")+ ):+ continue+ namespace = getattr(module, "__dict__", {})+ for name, (real, guard) in guards.items():+ if namespace.get(name) is real:+ patcher.setattr(module, name, guard)+ yield++@pytest.fixturedef temp_data_dir():"""Create a temporary data directory for testing."""diff --git a/tests/database/backup/test_backup_status.py b/tests/database/backup/test_backup_status.pynew file mode 100644index 0000000000..a735fcf1db--- /dev/null+++ b/tests/database/backup/test_backup_status.py@@ -0,0 +1,1501 @@+"""Backup failures survive worker changes and reach the authenticated UI."""++from collections import OrderedDict+from contextlib import contextmanager+from datetime import UTC, datetime, timedelta+import errno+import json+import os+from pathlib import Path+import subprocess+import sys+import threading+from unittest.mock import MagicMock++import pytest+from sqlalchemy import create_engine, inspect, select+from sqlalchemy.orm import Session++from local_deep_research.database import auth_db+from local_deep_research.database.backup import backup_status as status+from local_deep_research.database.backup.backup_executor import BackupExecutor+from local_deep_research.database.backup.backup_service import (+ BackupResult,+ BackupService,+)+++def _status_engine(path):+ from local_deep_research.database.sqlcipher_compat import (+ connect_sqlcipher,+ get_sqlcipher_module,+ )++ def connect():+ connection = connect_sqlcipher(+ get_sqlcipher_module(), str(path), check_same_thread=False+ )+ connection.execute("PRAGMA key = \"x'" + bytes(range(32)).hex() + "'\"")+ connection.execute("PRAGMA cipher_compatibility = 4")+ return connection++ return create_engine("sqlite://", creator=connect)+++def status_db(tmp_path, monkeypatch):+ engines = {}++ @contextmanager+ def session(username):+ if username not in engines:+ engines[username] = _status_engine(tmp_path / (username + ".db"))+ engine = engines[username]+ with Session(engine) as db:+ yield db++ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setattr(status, "_encrypted_status_session", session)+ monkeypatch.setattr(status, "_recent", OrderedDict())+ engine = _status_engine(tmp_path / "alice.db")+ engines["alice"] = engine+ try:+ yield engine+ finally:+ for item in engines.values():+ item.dispose()+++def service(status_db, tmp_path, monkeypatch):+ from local_deep_research.database.backup import backup_service++ backup_dir = tmp_path / "backups"+ backup_dir.mkdir()+ monkeypatch.setattr(+ backup_service, "get_user_backup_directory", lambda username: backup_dir+ )+ monkeypatch.setattr(+ backup_service, "get_encrypted_database_path", lambda: tmp_path+ )+ return BackupService("alice", "not-a-real-password")+++def test_no_attempt_is_distinct_from_success(status_db):+ assert status.get_backup_status("alice") == {+ "status_available": True,+ "last_attempt": None,+ }+++def test_failure_is_persistent_and_scoped_to_account(status_db):+ status.record_backup_result(+ "alice", False, status.BackupError.INSUFFICIENT_SPACE+ )+ status._recent.clear()+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["success"] is False+ assert attempt["error_code"] == "insufficient_space"+ assert "disk space" in attempt["error"]+ assert datetime.fromisoformat(attempt["completed_at"]).tzinfo is not None+ assert status.get_backup_status("bob")["last_attempt"] is None+++def test_another_process_can_read_the_encrypted_result(status_db, tmp_path):+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ script = """+import json+from contextlib import contextmanager+from pathlib import Path+from sqlalchemy import create_engine+from sqlalchemy.orm import Session+from local_deep_research.database.sqlcipher_compat import connect_sqlcipher, get_sqlcipher_module+from local_deep_research.database.backup import backup_status as status+import os++def connect():+ connection = connect_sqlcipher(get_sqlcipher_module(), str(Path(os.environ['LDR_DATA_DIR']) / 'alice.db'))+ key_sql = chr(34) + "x" + chr(39) + bytes(range(32)).hex() + chr(39) + chr(34)+ connection.execute('PRAGMA key = ' + key_sql)+ connection.execute('PRAGMA cipher_compatibility = 4')+ return connection+engine = create_engine('sqlite://', creator=connect)+with engine.connect() as connection:+ connection.exec_driver_sql('SELECT * FROM user_backup_attempt_status').all()+@contextmanager+def session(username):+ with Session(engine) as db:+ yield db+status._encrypted_status_session = session+print(json.dumps(status.get_backup_status('alice')))+engine.dispose()+"""+ result = subprocess.run(+ [sys.executable, "-c", script],+ cwd=Path(__file__).resolve().parents[3],+ capture_output=True,+ text=True,+ timeout=45,+ check=False,+ )+ assert result.returncode == 0, result.stderr+ report = json.loads(result.stdout)+ assert report["status_available"] is True, result.stderr+ assert report["last_attempt"]["error_code"] == "verification"+ assert (+ not (tmp_path / "alice.db").read_bytes().startswith(b"SQLite format 3")+ )+++def test_later_success_clears_failure(status_db):+ status.record_backup_result("alice", False)+ status.record_backup_result("alice", True)+ status._recent.clear()+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["success"] is True+ assert attempt["error"] is None+ assert attempt["error_code"] is None+++def test_delayed_older_writer_cannot_replace_newer_result(+ status_db, monkeypatch+):+ now = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [now, now - timedelta(seconds=10)]+ monkeypatch.setattr(status, "datetime", clock)+ status.record_backup_result("alice", True)+ status.record_backup_result("alice", False)+ status._recent.clear()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_failed_status_write_keeps_local_failure_and_marks_unavailable(+ status_db, monkeypatch+):+ monkeypatch.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=OSError("private path")),+ )+ status.record_backup_result(+ "alice", False, status.BackupError.STORAGE_UNAVAILABLE+ )+ report = status.get_backup_status("alice")+ assert report["status_available"] is False+ assert report["last_attempt"]["error_code"] == "storage_unavailable"+ assert "private path" not in str(report)+++def test_data_root_change_does_not_reuse_local_history(+ status_db, tmp_path, monkeypatch+):+ status.record_backup_result("alice", False)+ monkeypatch.setattr(+ status,+ "_status_key",+ lambda username: (str(tmp_path / "another"), username),+ )+ monkeypatch.setattr(+ status, "_encrypted_status_session", MagicMock(side_effect=OSError())+ )+ assert status.get_backup_status("alice") == {+ "status_available": False,+ "last_attempt": None,+ }+++def test_local_fallback_is_bounded(status_db, monkeypatch):+ monkeypatch.setattr(status, "_RECENT_LIMIT", 2)+ monkeypatch.setattr(+ status, "_encrypted_status_session", MagicMock(side_effect=OSError())+ )+ for username in ("alice", "bob", "carol"):+ status.record_backup_result(username, False)+ assert len(status._recent) == 2+ assert status.get_backup_status("alice")["last_attempt"] is None+ assert status.get_backup_status("carol")["last_attempt"]["success"] is False++[email protected]("operation", ["create_backup", "purge_and_refresh"])+def test_real_missing_database_failure_is_recorded(service, operation):+ result = getattr(service, operation)()+ assert result.success is False+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "source_missing"+ )+++ ("operation", "step"),+ [+ ("create_backup", "_find_verified_backup"),+ ("purge_and_refresh", "_export_backup"),+ ],+)+def test_unexpected_failure_around_the_export_is_recorded_and_reraised(+ service, monkeypatch, operation, step+):+ """Both public operations record what the export itself never reports.++ The injected error stands in for any unexpected failure in the work+ around the export. It is not a model of an unreadable backup directory:+ ``Path.glob`` swallows that error, which is why the panel lists the+ directory with ``list_backup_files`` instead.+ """+ monkeypatch.setattr(+ service,+ step,+ MagicMock(side_effect=OSError(errno.EIO, "private-path")),+ )+ with pytest.raises(OSError):+ getattr(service, operation)()+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["error_code"] == "storage_unavailable"+ assert "private-path" not in str(attempt)+++def test_every_error_code_has_a_fixed_message():+ assert set(status._ERROR_MESSAGES) == set(status.BackupError)+++def test_password_change_on_an_unlistable_directory_is_recorded_as_failed(+ service, monkeypatch, loguru_caplog+):+ """``Path.glob`` finds no old backup in a directory the server can write+ but not list, so nothing is deleted. The fresh backup is still written,+ but the attempt must not read as a clean success."""+ if os.name != "posix" or os.geteuid() == 0:+ pytest.skip("permission bits are not enforced for root or on Windows")+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ service.backup_dir.chmod(0o300)+ try:+ with loguru_caplog.at_level("WARNING"):+ result = service.purge_and_refresh()+ finally:+ service.backup_dir.chmod(0o700)+ assert result.success is True+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["success"] is False+ assert attempt["error_code"] == "purge_incomplete"+ assert "Old-password backups may remain" in attempt["error"]+ assert any(+ record.levelname == "WARNING"+ and "Could not list old backups" in record.getMessage()+ for record in loguru_caplog.records+ )+++def test_password_change_on_a_listable_directory_is_a_clean_success(+ service, monkeypatch+):+ old_backup = service.backup_dir / "ldr_backup_20260101_000000.db"+ old_backup.write_bytes(b"x")+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert not old_backup.exists()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_password_change_without_a_backup_directory_is_a_clean_success(+ service, monkeypatch+):+ """A missing directory holds no old backups, so nothing is left behind."""+ service.backup_dir.rmdir()+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_real_low_disk_space_failure_is_recorded(service, monkeypatch):+ from local_deep_research.database.backup import backup_service++ service.db_path.write_bytes(b"source")+ monkeypatch.setattr(+ backup_service.shutil, "disk_usage", lambda path: MagicMock(free=0)+ )+ result = service.create_backup(force=True)+ assert result.success is False+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "insufficient_space"+ )+++def test_verified_daily_reuse_does_not_erase_a_failed_new_backup(+ service, monkeypatch+):+ status.record_backup_result("alice", False)+ monkeypatch.setattr(+ service,+ "_find_verified_backup",+ lambda paths: (Path("existing.db"), 100),+ )+ assert service.create_backup().success is True+ assert status.get_backup_status("alice")["last_attempt"]["success"] is False+++def test_new_success_records_recovery(service, monkeypatch):+ status.record_backup_result("alice", False)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.create_backup(force=True).success is True+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_unexpected_error_never_persists_exception_contents(+ service, monkeypatch, status_db+):+ secret = "password-and-private-path-must-not-be-recorded"+ monkeypatch.setattr(+ service, "_export_backup", MagicMock(side_effect=RuntimeError(secret))+ )+ with pytest.raises(RuntimeError):+ service.create_backup(force=True)+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert secret not in str(dict(row))+ assert secret not in str(status.get_backup_status("alice"))+++def test_status_storage_failure_does_not_fail_successful_backup(+ service, monkeypatch+):+ monkeypatch.setattr(+ status, "_encrypted_status_session", MagicMock(side_effect=OSError())+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.create_backup(force=True).success is True+++def test_constructor_failure_is_reported(status_db, monkeypatch):+ from local_deep_research.database.backup import backup_service++ monkeypatch.setattr(+ backup_service,+ "get_user_backup_directory",+ MagicMock(side_effect=PermissionError(errno.EACCES, "private-path")),+ )+ with pytest.raises(PermissionError):+ BackupService("alice", "private-password")+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "permission_denied"+ )+++def test_queue_failure_is_reported_and_can_be_retried(status_db):+ # Construct an isolated executor without starting its singleton thread pool.+ executor = object.__new__(BackupExecutor)+ executor._pending_backups = set()+ executor._pending_lock = threading.Lock()+ executor._executor = MagicMock()+ executor._executor.submit.side_effect = RuntimeError("executor closed")+ with pytest.raises(RuntimeError):+ executor.submit_backup("alice", "private-password")+ assert not executor._pending_backups+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "scheduling"+ )+ executor._executor.submit.side_effect = None+ assert executor.submit_backup("alice", "private-password") is True+++def api_client(status_db, service, monkeypatch):+ from fastapi import FastAPI+ from fastapi.testclient import TestClient+ from starlette.middleware.sessions import SessionMiddleware+ from local_deep_research.web.routers import settings+ from local_deep_research.web.dependencies.auth import require_auth++ app = FastAPI()+ app.add_middleware(SessionMiddleware, secret_key="backup-status-test")+ app.include_router(settings.router)+ app.dependency_overrides[require_auth] = lambda: "alice"+ monkeypatch.setattr(+ settings, "_get_setting_from_session", lambda *args: True+ )+ from local_deep_research.config import paths++ monkeypatch.setattr(+ paths, "get_user_backup_directory", lambda username: service.backup_dir+ )+ with TestClient(app) as client:+ yield client+++def test_real_backup_failure_reaches_authenticated_endpoint(+ service, api_client+):+ assert service.create_backup().success is False+ response = api_client.get("/settings/api/backup-status")+ assert response.status_code == 200+ body = response.json()+ assert body["listing_available"] is True+ assert body["last_attempt"]["error_code"] == "source_missing"+ assert "not-a-real-password" not in response.text+ assert str(service.db_path) not in response.text+++def test_unusable_backup_directory_still_reports_the_failure(+ service, api_client, monkeypatch+):+ """``get_user_backup_directory`` raises when it cannot create or restrict+ the directory, for example when another account owns it."""+ from local_deep_research.config import paths++ assert service.create_backup().success is False+ monkeypatch.setattr(+ paths,+ "get_user_backup_directory",+ MagicMock(side_effect=PermissionError()),+ )+ response = api_client.get("/settings/api/backup-status")+ assert response.status_code == 200+ assert response.json()["listing_available"] is False+ assert response.json()["count"] is None+ assert response.json()["last_attempt"]["success"] is False+++def test_unlistable_directory_is_not_reported_as_empty(service, api_client):+ """A directory the server can write but not list is not "no backups yet".++ ``Path.glob`` returns [] for it without raising, so this needs a real+ permission denial, which root and Windows do not enforce.+ """+ if os.name != "posix" or os.geteuid() == 0:+ pytest.skip("permission bits are not enforced for root or on Windows")+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ (service.backup_dir / "ldr_backup_20260101_000000.db").write_bytes(b"x")+ service.backup_dir.chmod(0o300)+ try:+ assert list(service.backup_dir.glob("ldr_backup_*.db")) == []+ response = api_client.get("/settings/api/backup-status")+ finally:+ service.backup_dir.chmod(0o700)+ assert response.status_code == 200+ body = response.json()+ assert body["listing_available"] is False+ assert body["count"] is None+ assert body["last_attempt"]["error_code"] == "verification"+++def test_unreadable_settings_are_not_blamed_on_the_directory(+ service, api_client, monkeypatch+):+ from local_deep_research.web.routers import settings++ (service.backup_dir / "ldr_backup_20260101_000000.db").write_bytes(b"x")+ monkeypatch.setattr(+ settings,+ "_get_setting_from_session",+ MagicMock(side_effect=RuntimeError("settings database unavailable")),+ )+ response = api_client.get("/settings/api/backup-status")+ assert response.status_code == 200+ body = response.json()+ assert body["enabled"] is None+ assert body["listing_available"] is True+ assert body["count"] == 1+ assert "settings database unavailable" not in response.text+++def test_list_backup_files_matches_only_backup_databases(tmp_path):+ from local_deep_research.database.backup.backup_service import (+ list_backup_files,+ )++ wanted = tmp_path / "ldr_backup_20260101_000000.db"+ for name in (+ wanted.name,+ "ldr_backup_20260101_000000.db.salt",+ "ldr_backup_20260101_000000.db.tmp",+ "notes.db",+ ):+ (tmp_path / name).write_bytes(b"x")+ assert list_backup_files(tmp_path) == [wanted]+ with pytest.raises(FileNotFoundError):+ list_backup_files(tmp_path / "missing")+++def test_endpoint_reads_authenticated_owner_only(service, api_client):+ from local_deep_research.web.dependencies.auth import require_auth++ assert service.create_backup().success is False+ api_client.app.dependency_overrides[require_auth] = lambda: "bob"+ assert (+ api_client.get("/settings/api/backup-status").json()["last_attempt"]+ is None+ )++[email protected]_session_check+def test_endpoint_requires_authentication(api_client):+ api_client.app.dependency_overrides.clear()+ assert api_client.get("/settings/api/backup-status").status_code == 401+++def test_encrypted_status_is_not_a_plaintext_sqlite_database(+ status_db, tmp_path+):+ import sqlite3++ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ data = (tmp_path / "alice.db").read_bytes()+ assert b"verification" not in data+ assert b"alice" not in data+ with sqlite3.connect(tmp_path / "alice.db") as connection:+ with pytest.raises(sqlite3.DatabaseError):+ connection.execute(+ "SELECT * FROM user_backup_attempt_status"+ ).fetchall()+++def test_deletion_failure_remains_visible_after_a_successful_backup(+ service, monkeypatch+):+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ monkeypatch.setattr(+ service,+ "_remove_backup_files",+ MagicMock(side_effect=PermissionError()),+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert old.exists()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+ assert service.create_backup(force=True).success is True+ status._recent.clear()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_only_a_verified_new_purge_clears_the_warning(service, monkeypatch):+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ remove = service._remove_backup_files+ monkeypatch.setattr(+ service,+ "_remove_backup_files",+ MagicMock(side_effect=PermissionError()),+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ service.purge_and_refresh()+ monkeypatch.setattr(service, "_remove_backup_files", remove)+ service.purge_and_refresh()+ status._recent.clear()+ assert not old.exists()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True++[email protected]("encrypted, connected", [(True, False), (False, True)])+def test_connection_unavailable_never_reopens_or_uses_plaintext_storage(+ monkeypatch, tmp_path, encrypted, connected+):+ from local_deep_research.database.encrypted_db import db_manager++ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setattr(status, "_recent", OrderedDict())+ monkeypatch.setattr(db_manager, "has_encryption", encrypted)+ monkeypatch.setattr(+ db_manager, "is_user_connected", lambda username: connected+ )+ get_session = MagicMock()+ monkeypatch.setattr(db_manager, "get_session", get_session)+ auth_session = MagicMock(+ side_effect=AssertionError("No plaintext status store")+ )+ monkeypatch.setattr(auth_db, "auth_db_session", auth_session)+ status.record_backup_result("alice", False)+ report = status.get_backup_status("alice")+ assert report["status_available"] is False+ assert report["last_attempt"]["success"] is False+ get_session.assert_not_called()+ auth_session.assert_not_called()+ assert not list(tmp_path.rglob("*.db"))+++def test_purge_removes_orphaned_salt_and_temporary_pairs(service, monkeypatch):+ names = [+ "ldr_backup_20260101_000000.db.salt",+ "ldr_backup_20260101_000001.db.tmp",+ "ldr_backup_20260101_000001.db.tmp.salt",+ "ldr_backup_20260101_000002.db.damaged-20261018_170000_123456",+ "ldr_backup_20260101_000002.db.damaged-20261018_170000_123456.salt",+ "ldr_backup_20260101_000003.db.damaged-20261018_170000_123456.salt",+ ]+ for name in names:+ (service.backup_dir / name).write_bytes(b"old-pair")+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ service.purge_and_refresh()+ assert not any((service.backup_dir / name).exists() for name in names)+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_production_session_path_uses_only_the_existing_encrypted_engine(+ tmp_path, monkeypatch+):+ from local_deep_research.database.encrypted_db import db_manager++ engine = _status_engine(tmp_path / "existing-user.db")+ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setattr(status, "_recent", OrderedDict())+ monkeypatch.setattr(db_manager, "has_encryption", True)+ monkeypatch.setattr(+ db_manager, "is_user_connected", lambda username: username == "alice"+ )+ get_session = MagicMock(side_effect=lambda username: Session(engine))+ monkeypatch.setattr(db_manager, "get_session", get_session)+ auth_session = MagicMock(+ side_effect=AssertionError("No plaintext status store")+ )+ monkeypatch.setattr(auth_db, "auth_db_session", auth_session)+ try:+ status.record_backup_result(+ "alice", False, status.BackupError.VERIFICATION+ )+ status._recent.clear()+ report = status.get_backup_status("alice")+ assert report["status_available"] is True+ assert report["last_attempt"]["error_code"] == "verification"+ assert get_session.call_count == 2+ auth_session.assert_not_called()+ assert not (tmp_path / "ldr_auth.db").exists()+ assert not (tmp_path / "encrypted_databases").exists()+ finally:+ engine.dispose()++[email protected]("existing_row", [False, True])+def test_disconnected_purge_warning_survives_connected_ordinary_success(+ status_db, monkeypatch, existing_row+):+ if existing_row:+ status.record_backup_result("alice", True)+ with monkeypatch.context() as offline:+ offline.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", True, purge_incomplete=True)+ status.record_backup_result("alice", True)+ status.record_backup_result("alice", True)+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["success"] is True+ assert row["purge_incomplete"] is True+ status._recent.clear()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_disconnected_verified_clear_survives_connected_ordinary_success(+ status_db, monkeypatch+):+ status.record_backup_result("alice", True, purge_incomplete=True)+ with monkeypatch.context() as offline:+ offline.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", True, purge_incomplete=False)+ status.record_backup_result("alice", True)+ status.record_backup_result("alice", True)+ status._recent.clear()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def test_ordinary_success_does_not_revive_a_persisted_warning_cleared_elsewhere(+ status_db, monkeypatch+):+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [start, start + timedelta(seconds=2)]+ monkeypatch.setattr(status, "datetime", clock)+ status.record_backup_result("alice", True, purge_incomplete=True)+ with status_db.begin() as connection:+ connection.execute(+ status._attempts.update()+ .where(status._attempts.c.username == "alice")+ .values(+ purge_incomplete=False,+ completed_at=start + timedelta(seconds=1),+ )+ )+ status.record_backup_result("alice", True)+ status._recent.clear()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def _old_backup_that_cannot_be_removed(service, monkeypatch):+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ monkeypatch.setattr(+ service,+ "_remove_backup_files",+ MagicMock(side_effect=PermissionError()),+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ # Next login's scheduled backup finds today's verified backup and+ # returns early, recording no new attempt.+ monkeypatch.setattr(+ service,+ "_find_verified_backup",+ lambda paths: (Path("existing.db"), 100),+ )+ return old+++def test_purge_after_engine_disposal_survives_daily_reuse_and_restart(+ service, status_db, monkeypatch+):+ """#7443 B4: change_password() disposes the user's engine before the+ purge runs, so the open-engine status path is unavailable. The purge+ result must still be persisted, using the password the purge holds."""+ _old_backup_that_cannot_be_removed(service, monkeypatch)++ @contextmanager+ def password_session():+ with Session(status_db) as db:+ yield db++ opened_with_password = MagicMock(side_effect=password_session)+ monkeypatch.setattr(+ service, "_password_status_session", opened_with_password+ )+ with monkeypatch.context() as disposed:+ disposed.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ assert service.purge_and_refresh().success is True+ opened_with_password.assert_called_once_with()+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True++ assert service.create_backup().success is True+ status._recent.clear() # server restart+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_daily_reuse_persists_a_purge_observed_while_disconnected(+ service, status_db, monkeypatch+):+ """#7443 B4: if even the purge's own connection fails, the daily-reuse+ early return at the next login writes the pending observation before a+ restart can discard it."""+ _old_backup_that_cannot_be_removed(service, monkeypatch)+ monkeypatch.setattr(+ service,+ "_password_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ with monkeypatch.context() as disposed:+ disposed.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ assert service.purge_and_refresh().success is True+ # Precondition: nothing could be persisted while disconnected.+ assert not inspect(status_db).has_table(status._attempts.name)++ assert service.create_backup().success is True+ status._recent.clear() # server restart+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_pending_purge_survives_a_newer_ordinary_success_from_another_worker(+ status_db, monkeypatch+):+ """#7443 R1: worker A observes an incomplete purge while the database is+ closed; worker B then persists a newer ordinary success. When A writes+ its older observation, the attempt stays B's but the purge flag stays."""+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [start, start + timedelta(seconds=5)]+ monkeypatch.setattr(status, "datetime", clock)+ worker_a = status._recent+ with monkeypatch.context() as disconnected:+ disconnected.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", True, purge_incomplete=True)++ monkeypatch.setattr(status, "_recent", OrderedDict()) # worker B+ status.record_backup_result("alice", True)++ monkeypatch.setattr(status, "_recent", worker_a)+ status.get_backup_status("alice") # A writes its pending observation++ monkeypatch.setattr(status, "_recent", OrderedDict()) # any worker+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["error_code"] == "purge_incomplete"+ assert attempt["completed_at"] == (start + timedelta(seconds=5)).isoformat()+++def test_directory_with_a_backup_name_does_not_hold_the_purge_warning(+ service, monkeypatch+):+ """#7443 R2: the purge never deletes a directory, so counting one as a+ remaining old-password backup would keep the warning up forever."""+ decoy = service.backup_dir / "ldr_backup_20260101_000000.db"+ decoy.mkdir()+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert decoy.is_dir()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is True+++def _assert_purge_incomplete_recorded(status_db):+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True+ status._recent.clear()+ report = status.get_backup_status("alice")["last_attempt"]+ assert report["success"] is False+ assert "Old-password backups may remain" in report["error"]+++def test_purge_counts_old_backups_it_cannot_inspect(+ service, status_db, monkeypatch+):+ """#7443 B1: in a directory that can be listed but not searched, every+ lstat fails with EACCES. On Python 3.14 the Path.is_* helpers turn that+ into False, which made every old backup look absent and recorded the+ purge as verified clean while the old-password files remained."""+ from local_deep_research.database.backup import backup_service++ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ real_lstat = os.lstat++ def unsearchable(path, *args, **kwargs):+ if Path(path).parent == service.backup_dir:+ raise PermissionError(errno.EACCES, "Permission denied")+ return real_lstat(path, *args, **kwargs)++ monkeypatch.setattr(backup_service.os, "lstat", unsearchable)+ # Removing an entry also needs search permission on its directory.+ monkeypatch.setattr(+ service,+ "_remove_backup_files",+ MagicMock(side_effect=PermissionError(errno.EACCES, "denied")),+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert old.exists()+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True++[email protected]("mode", [0o400, 0o600])+def test_purge_in_a_real_unsearchable_directory_is_incomplete(+ service, status_db, monkeypatch, mode+):+ """#7443 B1, against the real filesystem: names listable, entries not."""+ if os.name != "posix" or os.geteuid() == 0:+ pytest.skip("permission bits are not enforced for root or on Windows")+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ service.backup_dir.chmod(mode)+ try:+ assert service.purge_and_refresh().success is True+ finally:+ service.backup_dir.chmod(0o700)+ assert old.exists()+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True+++def test_purge_error_before_the_backup_is_recorded_as_incomplete(+ service, status_db, monkeypatch+):+ """#7443 B1: on Python 3.12/3.13 an inspection error escapes the purge+ before the fresh backup. The user's engine is already disposed, so the+ incomplete purge must still be written through the password session."""+ (service.backup_dir / "ldr_backup_20260101_000000.db").write_bytes(b"x")+ monkeypatch.setattr(+ service,+ "_is_purgeable",+ MagicMock(side_effect=PermissionError(errno.EACCES, "denied")),+ )++ @contextmanager+ def password_session():+ with Session(status_db) as db:+ yield db++ opened_with_password = MagicMock(side_effect=password_session)+ monkeypatch.setattr(+ service, "_password_status_session", opened_with_password+ )+ with monkeypatch.context() as disposed:+ disposed.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ with pytest.raises(PermissionError):+ service.purge_and_refresh()+ opened_with_password.assert_called_once_with()+ _assert_purge_incomplete_recorded(status_db)+++def test_purge_tie_or_step_back_keeps_incomplete_in_the_database(+ status_db, monkeypatch+):+ """#7443: on an equal purge timestamp, or a clock that stepped back,+ "incomplete" must win over "clean" in the stored merge."""+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [start, start, start - timedelta(seconds=1)]+ monkeypatch.setattr(status, "datetime", clock)+ status.record_backup_result("alice", True, purge_incomplete=False)+ status.record_backup_result("alice", True, purge_incomplete=True)+ status._recent.clear()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+ status.record_backup_result("alice", True, purge_incomplete=False)+ status._recent.clear()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_purge_step_back_keeps_incomplete_in_the_local_observation(+ status_db, monkeypatch+):+ """#7443: the same rule for the in-memory observation, used while the+ encrypted database is closed."""+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [start, start - timedelta(seconds=1)]+ monkeypatch.setattr(status, "datetime", clock)+ monkeypatch.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", True, purge_incomplete=False)+ status.record_backup_result("alice", True, purge_incomplete=True)+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_real_password_status_session_on_a_keyed_database(+ tmp_path, monkeypatch+):+ """#7443: the real _password_status_session body, on a real SQLCipher+ database: it writes with the right password, writes nothing with a+ wrong one, and never creates a missing database."""+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ try:+ get_sqlcipher_module()+ except ImportError:+ pytest.skip("SQLCipher driver not installed")+ from local_deep_research.database.backup import backup_service+ from local_deep_research.database.sqlcipher_utils import (+ create_sqlcipher_connection,+ )++ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setattr(status, "_recent", OrderedDict())+ monkeypatch.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ monkeypatch.setattr(+ backup_service,+ "get_user_backup_directory",+ lambda username: tmp_path / "backups",+ )+ monkeypatch.setattr(+ backup_service, "get_encrypted_database_path", lambda: tmp_path+ )+ right = BackupService("alice", "right-password")+ connection = create_sqlcipher_connection(+ str(right.db_path), "right-password", creation_mode=True+ )+ connection.execute("CREATE TABLE marker (id INTEGER)")+ connection.commit()+ connection.close()++ def stored_purge_flags():+ conn = create_sqlcipher_connection(str(right.db_path), "right-password")+ try:+ tables = conn.execute(+ "SELECT name FROM sqlite_master WHERE name = ?",+ (status._attempts.name,),+ ).fetchall()+ if not tables:+ return []+ return conn.execute(+ "SELECT purge_incomplete FROM user_backup_attempt_status"+ ).fetchall()+ finally:+ conn.close()++ wrong = BackupService("alice", "wrong-password")+ status.record_backup_result(+ "alice",+ True,+ purge_incomplete=True,+ direct_session=wrong._password_status_session,+ )+ assert stored_purge_flags() == []++ status.record_backup_result(+ "alice",+ True,+ purge_incomplete=True,+ direct_session=right._password_status_session,+ )+ assert stored_purge_flags() == [(1,)]++ missing = BackupService("bob", "any-password")+ status.record_backup_result(+ "bob",+ True,+ purge_incomplete=True,+ direct_session=missing._password_status_session,+ )+ assert not missing.db_path.exists()++[email protected]("deferred_write", ["status_read", "daily_reuse"])+def test_deferred_write_does_not_revive_a_newer_clean_purge(+ service, status_db, monkeypatch, deferred_write+):+ """#7443: worker A stores an incomplete purge, then fails to store an+ ordinary attempt; worker B stores a newer verified clean purge. A's+ later deferred write must not resend its stale stored purge state,+ which the step-back rule would let win over B's clean purge."""+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [+ start,+ start + timedelta(seconds=5),+ start + timedelta(seconds=10),+ ]+ monkeypatch.setattr(status, "datetime", clock)+ status.record_backup_result("alice", True, purge_incomplete=True)+ with monkeypatch.context() as offline:+ offline.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", False)+ worker_a = status._recent++ monkeypatch.setattr(status, "_recent", OrderedDict()) # worker B+ status.record_backup_result("alice", True, purge_incomplete=False)++ monkeypatch.setattr(status, "_recent", worker_a)+ if deferred_write == "status_read":+ status.get_backup_status("alice")+ else:+ monkeypatch.setattr(+ service,+ "_find_verified_backup",+ lambda paths: (Path("existing.db"), 100),+ )+ assert service.create_backup().success is True++ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is False+ assert row["purge_checked_at"] == start + timedelta(seconds=10)+++def test_unwritten_purge_behind_a_written_attempt_is_still_flushed(+ status_db, monkeypatch+):+ """#7443: an observation is pending while EITHER fact is unwritten. A+ written attempt merged over an unwritten purge state must not make the+ purge state look written."""+ with monkeypatch.context() as offline:+ offline.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result("alice", True, purge_incomplete=True)+ key = status._status_key("alice")+ # A newer attempt that is already stored (e.g. read back from the+ # database) is merged over the cached, unwritten purge state.+ status._remember(+ key,+ {+ "completed_at": datetime.now(UTC) + timedelta(seconds=1),+ "success": True,+ "error_code": None,+ "persisted": True,+ "purge_incomplete": False,+ "purge_checked_at": None,+ "purge_persisted": True,+ },+ )+ status.persist_pending_backup_status("alice")+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True+++def test_purge_result_uses_the_password_session_before_the_engine(+ service, status_db, monkeypatch+):+ """#7443: the purge runs under the rekey sidecar, which the open-engine+ status path waits on; the password session must be tried first."""++ @contextmanager+ def password_session():+ with Session(status_db) as db:+ yield db++ monkeypatch.setattr(+ service,+ "_password_status_session",+ MagicMock(side_effect=password_session),+ )+ engine_path = MagicMock(side_effect=AssertionError("engine status path"))+ monkeypatch.setattr(status, "_encrypted_status_session", engine_path)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ engine_path.assert_not_called()+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is False+++def test_backup_appearing_after_the_first_listing_keeps_the_purge_incomplete(+ service, monkeypatch+):+ """#7443: only the post-purge re-listing sees a file written after the+ deletion pass enumerated the directory."""+ from local_deep_research.database.backup import backup_service++ late = service.backup_dir / "ldr_backup_20260101_000000.db"+ real_listing = backup_service.list_backup_files+ listings = []++ def listing(directory, *, include_auxiliary=False):+ listings.append(directory)+ if len(listings) == 1:+ return [] # The deletion pass saw an empty directory...+ late.write_bytes(b"old-key-backup") # ...then a file appeared.+ return real_listing(directory, include_auxiliary=include_auxiliary)++ monkeypatch.setattr(backup_service, "list_backup_files", listing)+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert len(listings) == 2+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_failed_unlink_keeps_the_purge_incomplete_on_its_own(+ service, monkeypatch+):+ """#7443: a failed deletion is incomplete even when the re-listing+ (here made to come back empty) would not show the file."""+ from local_deep_research.database.backup import backup_service++ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ real_listing = backup_service.list_backup_files+ listings = []++ def listing(directory, *, include_auxiliary=False):+ listings.append(directory)+ if len(listings) == 1:+ return real_listing(directory, include_auxiliary=include_auxiliary)+ return []++ monkeypatch.setattr(backup_service, "list_backup_files", listing)+ monkeypatch.setattr(+ service,+ "_remove_backup_files",+ MagicMock(side_effect=PermissionError(errno.EACCES, "denied")),+ )+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ assert service.purge_and_refresh().success is True+ assert len(listings) == 2+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def _listings(monkeypatch, first, second):+ """Make the purge's two directory listings behave as given."""+ from local_deep_research.database.backup import backup_service++ real_listing = backup_service.list_backup_files+ calls = []++ def listing(directory, *, include_auxiliary=False):+ calls.append(directory)+ outcome = first if len(calls) == 1 else second+ if isinstance(outcome, BaseException):+ raise outcome+ if outcome == "real":+ return real_listing(directory, include_auxiliary=include_auxiliary)+ return outcome++ monkeypatch.setattr(backup_service, "list_backup_files", listing)+ return calls+++def test_unreadable_post_purge_listing_keeps_the_purge_incomplete(+ service, monkeypatch+):+ """#7443 B2: the deletion pass saw nothing, the old file is still there,+ and only the re-listing's error says the purge cannot be confirmed."""+ old = service.backup_dir / "ldr_backup_20260101_000000.db"+ old.write_bytes(b"old-key-backup")+ calls = _listings(monkeypatch, [], PermissionError(errno.EACCES, "denied"))+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ result = service.purge_and_refresh()+ assert result.success is True+ assert result.purge_incomplete is True+ assert len(calls) == 2+ assert old.exists()+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_unreadable_first_listing_alone_keeps_the_purge_incomplete(+ service, monkeypatch+):+ """#7443 B2: the first listing fails and the re-listing comes back empty;+ only the first listing's error proves nothing was confirmed removed."""+ calls = _listings(monkeypatch, PermissionError(errno.EACCES, "denied"), [])+ monkeypatch.setattr(+ service, "_export_backup", lambda **kwargs: BackupResult(success=True)+ )+ result = service.purge_and_refresh()+ assert result.purge_incomplete is True+ assert len(calls) == 2+ assert (+ status.get_backup_status("alice")["last_attempt"]["error_code"]+ == "purge_incomplete"+ )+++def test_unparseable_stored_row_is_replaced_by_the_next_write(status_db):+ """#7443 R9: one corrupt value must not block every later status write.+ The stored purge flag survives the replacement."""+ from sqlalchemy import text++ status.record_backup_result("alice", True, purge_incomplete=True)+ with status_db.begin() as connection:+ connection.execute(+ text(+ "UPDATE user_backup_attempt_status SET completed_at = "+ "'not a date'"+ )+ )+ status._recent.clear()+ status.record_backup_result("alice", False, status.BackupError.VERIFICATION)+ status._recent.clear()+ report = status.get_backup_status("alice")+ assert report["status_available"] is True+ assert report["last_attempt"]["error_code"] == "verification"+ assert "Old-password backups may remain" in report["last_attempt"]["error"]+++def test_report_flags_a_pending_purge_behind_another_failure_code(status_db):+ """#7443: a failed attempt keeps its own error code while a purge is+ pending, so the report must carry the purge state explicitly."""+ status.record_backup_result(+ "alice",+ False,+ status.BackupError.PERMISSION_DENIED,+ purge_incomplete=True,+ )+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["error_code"] == "permission_denied"+ assert attempt["purge_incomplete"] is True++ status.record_backup_result("alice", False, status.BackupError.UNKNOWN)+ status.record_backup_result("alice", True, purge_incomplete=False)+ status.record_backup_result("alice", False, status.BackupError.UNKNOWN)+ attempt = status.get_backup_status("alice")["last_attempt"]+ assert attempt["error_code"] == "unknown"+ assert attempt["purge_incomplete"] is False+++def _corrupt(engine, assignments):+ from sqlalchemy import text++ with engine.begin() as connection:+ connection.execute(+ text(f"UPDATE user_backup_attempt_status SET {assignments}")+ )+++def _stored_purge(engine):+ with engine.connect() as connection:+ return connection.execute(+ select(status._attempts.c.purge_incomplete)+ ).scalar_one()+++def _carry_unwritten_purge(monkeypatch, purge_incomplete):+ """Observe a purge while the database is closed (cache only)."""+ with monkeypatch.context() as offline:+ offline.setattr(+ status,+ "_encrypted_status_session",+ MagicMock(side_effect=RuntimeError()),+ )+ status.record_backup_result(+ "alice", True, purge_incomplete=purge_incomplete+ )+++ ("stored", "carried", "expected"),+ [+ # An unreadable flag with no purge state to replace it: incomplete.+ ("'x'", None, True),+ # An unreadable flag replaced by the clean purge this write carries.+ ("'x'", False, False),+ # A readable flag survives the repair as it was stored.+ ("0", None, False),+ ("1", None, True),+ ],+)+def test_repair_of_an_unreadable_row_keeps_or_replaces_the_purge_flag(+ status_db, monkeypatch, stored, carried, expected+):+ """#7443 R4: each branch of the purge-flag repair is pinned."""+ status.record_backup_result("alice", True, purge_incomplete=False)+ _corrupt(+ status_db,+ f"completed_at = 'not a date', purge_incomplete = {stored}",+ )+ status._recent.clear()+ if carried is not None:+ _carry_unwritten_purge(monkeypatch, carried)+ status.record_backup_result("alice", True)+ assert _stored_purge(status_db) is expected+++def test_repair_keeps_a_newer_merged_purge_over_an_older_carried_one(+ status_db, monkeypatch+):+ """#7443 R2: worker A stored "incomplete" at T5; worker B carries an+ older clean purge from T3. The upsert keeps A's state; repairing a+ corrupt attempt column must not then replace it with B's."""+ start = datetime.now(UTC)+ clock = MagicMock()+ clock.now.side_effect = [+ start + timedelta(seconds=3),+ start + timedelta(seconds=5),+ start + timedelta(seconds=6),+ ]+ monkeypatch.setattr(status, "datetime", clock)+ _carry_unwritten_purge(monkeypatch, False) # worker B, at T3+ worker_b = status._recent+ monkeypatch.setattr(status, "_recent", OrderedDict()) # worker A+ status.record_backup_result("alice", True, purge_incomplete=True)+ _corrupt(status_db, "completed_at = 'not a date'")+ monkeypatch.setattr(status, "_recent", worker_b)+ status.record_backup_result("alice", True) # B writes, at T6+ with status_db.connect() as connection:+ row = connection.execute(select(status._attempts)).mappings().one()+ assert row["purge_incomplete"] is True+ assert row["purge_checked_at"] == start + timedelta(seconds=5)+++def test_repair_lets_a_carried_purge_replace_an_unreadable_timestamp(+ status_db, monkeypatch+):+ """#7443 R3: the merge cannot order against an unreadable purge+ timestamp, so a verified clean purge this write carries decides."""+ status.record_backup_result("alice", True, purge_incomplete=True)+ _corrupt(status_db, "purge_checked_at = 'zzz'")+ status._recent.clear()+ status.record_backup_result("alice", True, purge_incomplete=False)+ assert _stored_purge(status_db) is False+ status._recent.clear()+ assert status.get_backup_status("alice")["last_attempt"]["success"] is Truediff --git a/tests/database/backup/test_backup_status_isolation.py b/tests/database/backup/test_backup_status_isolation.pynew file mode 100644index 0000000000..d485a0ac82--- /dev/null+++ b/tests/database/backup/test_backup_status_isolation.py@@ -0,0 +1,149 @@+"""Backup code paths must not write the user's real data directory under test.++Backup attempts persist only in an already-open encrypted user database and+otherwise stay in memory. The shared test guard still prevents an accidental+write to a developer's already-connected database when only backup service+path helpers were patched. Namespace/status reads must not create directories.++The "real" data directory here is a watched directory inside ``tmp_path``+reached through a patched ``platformdirs.user_data_dir`` with ``LDR_DATA_DIR``+unset, so a regression cannot touch a developer's actual files.+"""++import errno+import threading+from unittest.mock import MagicMock++import platformdirs+import pytest++from local_deep_research.database.backup import backup_service, backup_status+from local_deep_research.database.backup.backup_executor import BackupExecutor+from local_deep_research.database.backup.backup_service import BackupService+++def real_data_dir(tmp_path, monkeypatch):+ """A watched stand-in for the real platform data directory."""+ watched = tmp_path / "real-user-data"+ monkeypatch.delenv("LDR_DATA_DIR", raising=False)+ monkeypatch.setattr(+ platformdirs, "user_data_dir", lambda *args, **kwargs: str(watched)+ )+ return watched+++def backup_dir(tmp_path, monkeypatch, real_data_dir):+ """Point the path helpers at ``tmp_path`` the way the backup tests do.++ Only the helpers move; the data directory itself stays unpinned, which is+ exactly what reached the real auth database.+ """+ directory = tmp_path / "backups"+ directory.mkdir()+ monkeypatch.setattr(+ backup_service, "get_user_backup_directory", lambda username: directory+ )+ monkeypatch.setattr(+ backup_service,+ "get_encrypted_database_path",+ lambda: tmp_path / "databases",+ )+ return directory+++def service(backup_dir):+ return BackupService("alice", "not-a-real-password")+++def _assert_nothing_written(watched):+ written = (+ sorted(str(path.relative_to(watched)) for path in watched.rglob("*"))+ if watched.exists()+ else []+ )+ assert not written, f"a backup path wrote outside tmp_path: {written}"+++def test_failed_backup_leaves_the_real_data_dir_alone(service, real_data_dir):+ result = service.create_backup(force=True)++ assert result.error_code == backup_status.BackupError.SOURCE_MISSING+ _assert_nothing_written(real_data_dir)+++def test_password_change_refresh_leaves_the_real_data_dir_alone(+ service, real_data_dir+):+ result = service.purge_and_refresh()++ assert result.error_code == backup_status.BackupError.SOURCE_MISSING+ _assert_nothing_written(real_data_dir)+++def test_constructor_failure_leaves_the_real_data_dir_alone(+ monkeypatch, backup_dir, real_data_dir+):+ monkeypatch.setattr(+ backup_service,+ "get_user_backup_directory",+ MagicMock(side_effect=PermissionError(errno.EACCES, "private-path")),+ )++ with pytest.raises(PermissionError):+ BackupService("alice", "not-a-real-password")++ _assert_nothing_written(real_data_dir)+++def test_background_backup_failure_leaves_the_real_data_dir_alone(+ monkeypatch, backup_dir, real_data_dir+):+ monkeypatch.setattr(+ backup_service,+ "get_user_backup_directory",+ MagicMock(side_effect=PermissionError(errno.EACCES, "private-path")),+ )+ executor = object.__new__(BackupExecutor)++ assert executor._run_backup("alice", "pw", 1, 7).success is False++ _assert_nothing_written(real_data_dir)+++def test_scheduling_failure_leaves_the_real_data_dir_alone(real_data_dir):+ executor = object.__new__(BackupExecutor)+ executor._pending_backups = set()+ executor._pending_lock = threading.Lock()+ executor._executor = MagicMock()+ executor._executor.submit.side_effect = RuntimeError("executor closed")++ with pytest.raises(RuntimeError):+ executor.submit_backup("alice", "pw")++ _assert_nothing_written(real_data_dir)+++def test_reading_the_status_leaves_the_real_data_dir_alone(real_data_dir):+ report = backup_status.get_backup_status("alice")++ assert report["last_attempt"] is None+ _assert_nothing_written(real_data_dir)+++def test_a_pinned_data_dir_still_records_the_failure(+ service, tmp_path, monkeypatch+):+ """A pinned data root alone does not authorize a plaintext status store."""+ pinned = tmp_path / "pinned-data"+ monkeypatch.setenv("LDR_DATA_DIR", str(pinned))++ assert service.create_backup(force=True).success is False++ assert not list(pinned.rglob("*.db"))+ report = backup_status.get_backup_status("alice")+ assert report["status_available"] is False+ attempt = report["last_attempt"]+ assert attempt["error_code"] == "source_missing"diff --git a/tests/database/backup/test_superseded_key_purge.py b/tests/database/backup/test_superseded_key_purge.pyindex 4210198f77..e11296dbf0 100644--- a/tests/database/backup/test_superseded_key_purge.py+++ b/tests/database/backup/test_superseded_key_purge.py@@ -911,6 +911,282 @@ def test_refresh_holds_cross_process_rekey_lock_through_the_purge(manager):assert observed and set(observed) == {"contended"}+# --- Backup status after the route refresh (#7443) ---------------------------+++def _stored_purge_flag(db_path, password):+ """The purge flag stored in the user database, or None if none is."""+ from local_deep_research.database.backup import backup_status+ from local_deep_research.database.sqlcipher_utils import (+ create_sqlcipher_connection,+ )++ connection = create_sqlcipher_connection(str(db_path), password)+ try:+ if not connection.execute(+ "SELECT name FROM sqlite_master WHERE name = ?",+ (backup_status._attempts.name,),+ ).fetchall():+ return None+ row = connection.execute(+ "SELECT purge_incomplete FROM user_backup_attempt_status"+ ).fetchone()+ return None if row is None else bool(row[0])+ finally:+ connection.close()+++def status_cache(monkeypatch):+ from collections import OrderedDict++ from local_deep_research.database.backup import backup_status++ monkeypatch.setattr(backup_status, "_recent", OrderedDict())+ return backup_status+++def test_refresh_records_a_verified_purge_without_the_engine_status_path(+ manager, status_cache, monkeypatch+):+ """The refresh holds the rekey sidecar; the open-engine status path+ takes the same sidecar and would stall until its wait bound. The purge+ result must go through the password session without trying it."""+ from unittest.mock import MagicMock++ username, db_path = _user(manager)+ _backup(username, OLD)+ assert manager.change_password(username, OLD, NEW) is True+ engine_path = MagicMock(side_effect=AssertionError("engine status path"))+ monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)++ result = manager.refresh_backups_after_password_change(username, NEW)++ assert result is not None and result.success, result+ engine_path.assert_not_called()+ assert _stored_purge_flag(db_path, NEW) is False+++def test_skipped_refresh_records_an_incomplete_purge(manager, status_cache):+ """A change whose epoch was never published is skipped: nothing is+ removed now, so old-key backups may remain."""+ username, db_path = _user(manager)+ _backup(username, OLD)+ with patch.object(+ encrypted_db,+ "commit_rekey_epoch",+ side_effect=OSError(28, "No space left on device"),+ ):+ assert manager.change_password(username, OLD, NEW) is True++ assert manager.refresh_backups_after_password_change(username, NEW) is None+ assert _stored_purge_flag(db_path, NEW) is True+++def test_unfenced_refresh_records_an_incomplete_purge(manager, status_cache):+ username, db_path = _user(manager)+ _backup(username, OLD)++ assert manager.refresh_backups_after_password_change(username, OLD) is None+ assert _stored_purge_flag(db_path, OLD) is True+++def test_contended_refresh_records_an_incomplete_purge(manager, status_cache):+ username, db_path = _user(manager)+ _backup(username, OLD)+ assert manager.change_password(username, OLD, NEW) is True++ with patch.object(+ encrypted_db,+ "acquire_rekey_lock",+ side_effect=RekeyLockContentionError(+ db_path.with_name(f"{db_path.name}.rekey.lock")+ ),+ ):+ with pytest.raises(RekeyLockContentionError):+ manager.refresh_backups_after_password_change(username, NEW)+ assert _stored_purge_flag(db_path, NEW) is True+++def _refresh_fault(kind, db_path, monkeypatch):+ """Install one failure of the refresh before its purge starts."""+ import errno+ from contextlib import contextmanager++ from local_deep_research.database.backup import backup_service+ from local_deep_research.database.rekey_lock import (+ RekeyEpochState,+ RekeyEpochUnreadableError,+ RekeyRecoveryRequiredError,+ RekeySidecarIOError,+ RekeySidecarOperation,+ )++ if kind == "sidecar_io":++ def unlockable(*args, **kwargs):+ raise RekeySidecarIOError(+ db_path, RekeySidecarOperation.ACQUIRE, errno.EIO+ )++ monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", unlockable)+ return RekeySidecarIOError+ if kind in ("epoch_unreadable", "recovery_required"):+ error = (+ RekeyEpochUnreadableError(db_path, errno.EIO)+ if kind == "epoch_unreadable"+ else RekeyRecoveryRequiredError(db_path)+ )++ def unreadable(path):+ raise error++ monkeypatch.setattr(encrypted_db, "read_rekey_epoch_state", unreadable)+ return type(error)+ if kind == "release_after_skip":++ @contextmanager+ def failing_release(*args, **kwargs):+ yield+ raise RekeySidecarIOError(+ db_path, RekeySidecarOperation.UNLOCK, errno.EIO+ )++ monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", failing_release)+ monkeypatch.setattr(+ encrypted_db,+ "read_rekey_epoch_state",+ lambda path: RekeyEpochState("a-later-epoch", None),+ )+ return RekeySidecarIOError+ assert kind == "service_setup"++ def no_backup_directory(username):+ raise PermissionError(errno.EPERM, "Operation not permitted")++ monkeypatch.setattr(+ backup_service, "get_user_backup_directory", no_backup_directory+ )+ return PermissionError+++ "kind",+ [+ "sidecar_io",+ "epoch_unreadable",+ "recovery_required",+ "release_after_skip",+ "service_setup",+ ],+)+def test_refresh_failing_before_its_purge_records_an_incomplete_purge(+ manager, status_cache, monkeypatch, kind+):+ """#7443: any error that ends the refresh before it purged leaves the+ old-key backups in place, so it is recorded as an incomplete purge and+ re-raised. The record goes through the new password, never through the+ open-engine path (which waits on the sidecar) or a BackupService (whose+ setup may be what failed)."""+ from unittest.mock import MagicMock++ username, db_path = _user(manager)+ old_backup = _backup(username, OLD)+ assert manager.change_password(username, OLD, NEW) is True+ engine_path = MagicMock(side_effect=AssertionError("engine status path"))+ monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)+ error_type = _refresh_fault(kind, db_path, monkeypatch)++ with pytest.raises(error_type):+ manager.refresh_backups_after_password_change(username, NEW)++ assert old_backup.exists()+ engine_path.assert_not_called()+ assert _stored_purge_flag(db_path, NEW) is True+++def test_refresh_for_a_different_service_database_records_an_incomplete_purge(+ manager, status_cache, monkeypatch, tmp_path+):+ """#7443 R6: the service resolving another database is a skip."""+ from local_deep_research.database.backup import backup_service++ username, db_path = _user(manager)+ old_backup = _backup(username, OLD)+ assert manager.change_password(username, OLD, NEW) is True+ elsewhere = tmp_path / "elsewhere"+ elsewhere.mkdir()+ monkeypatch.setattr(+ backup_service, "get_encrypted_database_path", lambda: elsewhere+ )++ assert manager.refresh_backups_after_password_change(username, NEW) is None+ assert old_backup.exists()+ assert _stored_purge_flag(db_path, NEW) is True+++def test_recovery_purge_setup_failure_never_uses_the_engine_status_path(+ manager, status_cache, monkeypatch+):+ """#7443: the login-recovery purge holds the rekey sidecar. A+ BackupService setup failure there must not record through the open+ engine, whose claim waits on that same sidecar (a self-stall)."""+ import errno+ from unittest.mock import MagicMock++ from local_deep_research.database.backup import backup_service++ username, db_path = _user(manager)+ old_backup = _backup(username, OLD)+ _completed_change_left_pending(manager, username, db_path)+ engine_path = MagicMock(side_effect=AssertionError("engine status path"))+ monkeypatch.setattr(status_cache, "_encrypted_status_session", engine_path)++ def no_backup_directory(username):+ raise PermissionError(errno.EPERM, "Operation not permitted")++ monkeypatch.setattr(+ backup_service, "get_user_backup_directory", no_backup_directory+ )++ assert manager.open_user_database(username, NEW) is not None+ engine_path.assert_not_called()+ assert old_backup.exists() # The best-effort purge was skipped.+++def test_release_error_after_a_purge_that_ran_adds_no_record(+ manager, status_cache, monkeypatch+):+ """#7443: once purge_and_refresh ran, it recorded its own result; an+ error releasing the sidecar afterwards must not overwrite a verified+ clean purge with "incomplete"."""+ import errno+ from contextlib import contextmanager++ from local_deep_research.database.rekey_lock import (+ RekeySidecarIOError,+ RekeySidecarOperation,+ )++ username, db_path = _user(manager)+ old_backup = _backup(username, OLD)+ assert manager.change_password(username, OLD, NEW) is True++ @contextmanager+ def release_fails(*args, **kwargs):+ yield+ raise RekeySidecarIOError(+ db_path, RekeySidecarOperation.UNLOCK, errno.EIO+ )++ monkeypatch.setattr(encrypted_db, "acquire_rekey_lock", release_fails)++ with pytest.raises(RekeySidecarIOError):+ manager.refresh_backups_after_password_change(username, NEW)+ assert not old_backup.exists()+ assert _stored_purge_flag(db_path, NEW) is False++def _publish_after_purge_contention(db_path, advance, held, attempted, pipe):"""Publish only after the parent has actually encountered our OS lock."""from pathlib import Pathdiff --git a/tests/database/test_alembic_migrations.py b/tests/database/test_alembic_migrations.pyindex dd083406d8..85327ea6d4 100644--- a/tests/database/test_alembic_migrations.py+++ b/tests/database/test_alembic_migrations.py@@ -836,6 +836,53 @@ class TestUsersTableExclusion:assert table in tables, f"User table '{table}' not found"+class TestAuthDatabaseRefusal:+ """run_migrations() must refuse the central auth database.++ If the auth engine were ever passed to it, migration 0001's create_all+ would fill ldr_auth.db with user-database tables. Recording a backup+ attempt must not add a table there (status lives only in the encrypted+ user database), so the auth database keeps the shape the guard expects.+ """++ @pytest.fixture+ def auth_engine(self, tmp_path, monkeypatch):+ """An engine on a real, freshly initialised auth database."""+ from local_deep_research.database import auth_db++ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ auth_db.init_auth_database()+ engine = create_engine(f"sqlite:///{auth_db.get_auth_db_path()}")+ yield engine+ engine.dispose()+ auth_db.dispose_auth_engine()++ def test_refuses_a_fresh_auth_database(self, auth_engine):+ with pytest.raises(RuntimeError, match="auth database"):+ run_migrations(auth_engine)+ assert inspect(auth_engine).get_table_names() == ["users"]++ def test_still_refuses_after_a_backup_attempt_is_recorded(+ self, auth_engine+ ):+ from local_deep_research.database.backup import backup_status++ backup_status.record_backup_result(+ "alice", False, backup_status.BackupError.UNKNOWN+ )+ assert set(inspect(auth_engine).get_table_names()) == {+ "users",+ }++ with pytest.raises(RuntimeError, match="auth database"):+ run_migrations(auth_engine)++ # Refused before any user-database table was created.+ assert set(inspect(auth_engine).get_table_names()) == {+ "users",+ }++class TestCheckDatabaseSchemaWithAlembic:"""Tests for check_database_schema function with Alembic."""diff --git a/tests/database/test_migration_chain_integrity.py b/tests/database/test_migration_chain_integrity.pyindex f1c02bad15..c546fc3df5 100644--- a/tests/database/test_migration_chain_integrity.py+++ b/tests/database/test_migration_chain_integrity.py@@ -198,6 +198,18 @@ KNOWN_CONNECTION_SITES = {"database/backup/backup_service.py::BackupService._open_backup_with_current_key": ("exempt:backup-verify"),+ # #7443: writes a password-change purge result into the user database+ # with the password just entered, because the open engine's status path+ # takes the rekey sidecar the purge already holds. "user-db", not exempt:+ # the Session it yields writes to the user database. Every connection+ # comes from creator=create_sqlcipher_connection (salted key, cipher+ # pragmas, apply_performance_pragmas, key check), so no second open path+ # exists. It refuses a missing file, and NullPool plus engine.dispose()+ # in a finally keep no connection or key past the with block. Callers+ # run it either under the sidecar with this change's epoch still+ # committed (purge_and_refresh), or after release, where a later rekey+ # makes the keyed open fail, so nothing is written under a stale key.+ "database/backup/backup_service.py::password_status_session": "user-db","database/encrypted_db.py::DatabaseManager._check_encryption_available": ("user-db"),diff --git a/tests/database/test_sqlcipher_connection_factory.py b/tests/database/test_sqlcipher_connection_factory.pyindex 1879e5ba61..96f6c21b86 100644--- a/tests/database/test_sqlcipher_connection_factory.py+++ b/tests/database/test_sqlcipher_connection_factory.py@@ -36,7 +36,10 @@ def mock_sqlcipher():"""Mock all SQLCipher dependencies for _make_sqlcipher_connection."""mock_conn = MagicMock()mock_cursor = MagicMock()- mock_conn.cursor.return_value = mock_cursor+ privacy_cursor = MagicMock()+ # Each DB-API cursor() call creates a distinct cursor. The connector's+ # pre-key privacy setup owns its cursor; the factory owns the keyed one.+ mock_conn.cursor.side_effect = [privacy_cursor, mock_cursor]mock_module = MagicMock()mock_module.connect.return_value = mock_conn@@ -54,6 +57,7 @@ def mock_sqlcipher():"module": mock_module,"conn": mock_conn,"cursor": mock_cursor,+ "privacy_cursor": privacy_cursor,"get_module": mock_get,"set_key": mock_set_key,"verify": mock_verify,@@ -142,11 +146,18 @@ class TestMakeSqlcipherConnection:"""Cursor must be closed before returning."""manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")+ mock_sqlcipher["privacy_cursor"].close.assert_called_once()mock_sqlcipher["cursor"].close.assert_called_once()def test_call_order(self, manager, mock_sqlcipher):"""Operations must happen in order: key → pragmas → verify → performance."""call_order = []+ mock_sqlcipher["privacy_cursor"].execute.side_effect = lambda *a: (+ call_order.append("privacy")+ )+ mock_sqlcipher["privacy_cursor"].close.side_effect = lambda: (+ call_order.append("privacy-close")+ )mock_sqlcipher["set_key"].side_effect = lambda *a, **kw: (call_order.append("key"))@@ -162,7 +173,33 @@ class TestMakeSqlcipherConnection:manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")- assert call_order == ["key", "pragmas", "verify", "performance"]+ assert call_order == [+ "privacy",+ "privacy-close",+ "key",+ "pragmas",+ "verify",+ "performance",+ ]+ mock_sqlcipher["privacy_cursor"].execute.assert_called_once_with(+ "PRAGMA cipher_log_level = NONE"+ )++ def test_privacy_failure_closes_resources_before_setting_a_key(+ self, manager, mock_sqlcipher+ ):+ mock_sqlcipher["privacy_cursor"].execute.side_effect = RuntimeError(+ "private diagnostic configuration failed"+ )+ with pytest.raises(+ RuntimeError, match="diagnostic configuration failed"+ ):+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")+ mock_sqlcipher["privacy_cursor"].close.assert_called_once()+ mock_sqlcipher["conn"].close.assert_called_once()+ mock_sqlcipher["set_key"].assert_not_called()+ mock_sqlcipher["pragmas"].assert_not_called()+ mock_sqlcipher["verify"].assert_not_called()def test_busy_timeout_is_set_before_verification(self, manager, mock_sqlcipherdiff --git a/tests/hooks/test_custom_checks_raw_sql.py b/tests/hooks/test_custom_checks_raw_sql.pyindex be4b18049b..a2348895ef 100644--- a/tests/hooks/test_custom_checks_raw_sql.py+++ b/tests/hooks/test_custom_checks_raw_sql.py@@ -293,3 +293,28 @@ class TestOrmPatternsNotFlagged:"service.py",)assert result.returncode == 0+++class TestSqlCipherCompatibilityInfrastructure:+ def test_driver_privacy_configuration_is_allowed_at_its_exact_path(self):+ result = _run_hook(+ ' cursor.execute("PRAGMA cipher_log_level = NONE")\n',+ "src/local_deep_research/database/sqlcipher_compat.py",+ )+ assert result.returncode == 0++ def test_other_raw_sql_in_the_same_file_is_still_rejected(self):+ """The exemption covers one exact statement, not the whole file."""+ result = _run_hook(+ 'cursor.execute("PRAGMA cipher_log_level = NONE")\n'+ 'cursor.execute("SELECT * FROM documents")\n',+ "src/local_deep_research/database/sqlcipher_compat.py",+ )+ assert result.returncode == 1++ def test_same_basename_elsewhere_still_rejects_raw_sql(self):+ result = _run_hook(+ 'connection.execute("SELECT * FROM documents")\n',+ "src/local_deep_research/services/sqlcipher_compat.py",+ )+ assert result.returncode == 1diff --git a/tests/js/components/settings-fetch-error-handling.test.js b/tests/js/components/settings-fetch-error-handling.test.jsindex d127a87695..9b8b379052 100644--- a/tests/js/components/settings-fetch-error-handling.test.js+++ b/tests/js/components/settings-fetch-error-handling.test.js@@ -177,6 +177,120 @@ describe('settings request error handling', () => {document.getElementById('ldr-backup-status-content').textContent).toContain('Backups disabled');+ const backupError = '<img src=x onerror=alert(1)> Storage unavailable';+ const backupListing = {+ enabled: true,+ listing_available: true,+ status_available: true,+ count: 1,+ total_size_human: '4.0 KB',+ backups: [{ created_at: '2026-10-15T12:00:00Z', size_human: '4.0 KB' }],+ last_attempt: {+ success: false,+ error: backupError,+ completed_at: '2026-10-15T12:30:00Z',+ },+ };+ backupResponse = () => new Response(JSON.stringify(backupListing), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ let renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Last backup attempt failed');+ expect(renderedBackup.textContent).toContain('4.0 KB');+ expect(renderedBackup.querySelector('[role="alert"]').textContent).toContain(backupError);+ expect(renderedBackup.querySelector('img')).toBeNull();++ backupResponse = () => new Response(JSON.stringify({+ ...backupListing, listing_available: false, count: null, backups: [],+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Last backup attempt failed');+ expect(renderedBackup.textContent).toContain('Existing backups may still be present.');+ expect(renderedBackup.textContent).not.toContain('No backups yet');+ expect(renderedBackup.textContent).not.toContain('Backups are created once per day');++ backupResponse = () => new Response(JSON.stringify({+ ...backupListing, status_available: false, last_attempt: null,+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Backup attempt history is unavailable');+ expect(renderedBackup.querySelector('.ldr-backup-ok')).toBeNull();++ backupResponse = () => new Response(JSON.stringify({+ ...backupListing, enabled: null, last_attempt: null,+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Backup settings could not be read');+ expect(renderedBackup.textContent).not.toContain('Backups disabled');+ expect(renderedBackup.textContent).not.toContain('backup directory could not be read');+ expect(renderedBackup.textContent).not.toContain('Backups are created once per day');+ expect(renderedBackup.querySelector('.ldr-backup-ok')).toBeNull();++ backupResponse = () => new Response(JSON.stringify({+ ...backupListing,+ listing_available: false,+ status_available: false,+ count: null,+ backups: [],+ last_attempt: null,+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Any result shown is the latest observation on this server.');+ expect(renderedBackup.textContent).toContain('The backup directory could not be read.');+ expect(renderedBackup.textContent).not.toContain('No backups yet');++ backupResponse = () => new Response(JSON.stringify({+ ...backupListing, last_attempt: { success: true, error: null },+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('1 backup');+ expect(renderedBackup.querySelector('[role="alert"]')).toBeNull();++ // Backups switched off: an old ordinary failure is not shown as+ // current news next to the "Enable backups" hint...+ backupResponse = () => new Response(JSON.stringify({+ ...backupListing,+ enabled: false,+ last_attempt: { ...backupListing.last_attempt, error_code: 'permission_denied', purge_incomplete: false },+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.textContent).toContain('Backups disabled');+ expect(renderedBackup.textContent).not.toContain('Last backup attempt failed');+ expect(renderedBackup.querySelector('[role="alert"]')).toBeNull();++ // ...but an incomplete password-change purge still is, whatever+ // error code the attempt itself carries.+ for (const errorCode of ['purge_incomplete', 'permission_denied', 'unknown']) {+ backupResponse = () => new Response(JSON.stringify({+ ...backupListing,+ enabled: false,+ last_attempt: {+ success: false,+ error_code: errorCode,+ error: `${errorCode}: Old-password backups may remain.`,+ completed_at: '2026-10-15T12:30:00Z',+ purge_incomplete: true,+ },+ }), { status: 200 });+ document.querySelector('[data-tab="app"]').click();+ await vi.advanceTimersByTimeAsync(151);+ renderedBackup = document.getElementById('ldr-backup-status-content');+ expect(renderedBackup.querySelector('[role="alert"]').textContent)+ .toContain(`${errorCode}: Old-password backups may remain.`);+ }+document.getElementById('test-notification-button').click();await vi.advanceTimersByTimeAsync(1);diff --git a/tests/security/test_benchmark_and_temp_lifecycle.py b/tests/security/test_benchmark_and_temp_lifecycle.pyindex 436c7ffe52..0112b65593 100644--- a/tests/security/test_benchmark_and_temp_lifecycle.py+++ b/tests/security/test_benchmark_and_temp_lifecycle.py@@ -339,9 +339,12 @@ def test_sqlcipher_probe_removes_real_temp_file_on_success(encrypted_db, "apply_performance_pragmas", lambda _: None)monkeypatch.setattr(encrypted_db, "get_sqlcipher_version", lambda _: "4.0")- cursor = SimpleNamespace(close=lambda: None)++ def new_cursor():+ return SimpleNamespace(execute=lambda _sql: None, close=lambda: None)+connection = SimpleNamespace(- cursor=lambda: cursor,+ cursor=new_cursor,execute=lambda _sql: SimpleNamespace(fetchone=lambda: (1,)),close=lambda: None,)diff --git a/tests/security/test_identity_paths_and_host_effects.py b/tests/security/test_identity_paths_and_host_effects.pyindex eede664916..ebf8e7a4fe 100644--- a/tests/security/test_identity_paths_and_host_effects.py+++ b/tests/security/test_identity_paths_and_host_effects.py@@ -1,6 +1,7 @@import itertoolsfrom pathlib import Pathfrom types import SimpleNamespace+from unittest.mock import create_autospecimport pytestfrom fastapi.testclient import TestClient@@ -266,7 +267,12 @@ def backup_service_in_tmpdir(tmp_path, monkeypatch):)service = BackupService("testuser", "password")monkeypatch.setattr(- service, "_create_backup_impl", lambda: BackupResult(success=True)+ service,+ "_create_backup_impl",+ create_autospec(+ service._create_backup_impl,+ return_value=BackupResult(success=True),+ ),)return service@@ -277,48 +283,55 @@ def test_purge_does_not_delete_backup_entries_outside_root():"""Defence in depth: a listing entry outside backup_dir is not deleted.- list_backups only reports bare names from backup_dir, so this is not a- reachable attack today; the test monkeypatches list_backups to inject- the hostile path. purge_and_refresh re-checks each joined path with- is_safe_glob_result and skips anything outside backup_dir. This was a- strict xfail until that check landed.+ list_backup_files only reports entries of backup_dir, so this is not a+ reachable attack today; the test monkeypatches list_backup_files to+ inject the hostile path. purge_and_refresh skips any entry that is not+ directly in backup_dir (_is_purgeable) and re-checks containment with+ is_safe_glob_result before each unlink. This was a strict xfail until+ that check landed."""service = backup_service_in_tmpdiroutside_canary = tmp_path / "outside.db"outside_canary.write_bytes(b"do not delete")if forged_path.startswith("/"):forged_path = str(outside_canary)+ forged_entry = service.backup_dir / forged_pathmonkeypatch.setattr(- service,- "list_backups",- lambda: [{"filename": "forged.db", "path": forged_path}],+ backup_service,+ "list_backup_files",+ lambda directory, *, include_auxiliary: [forged_entry],)- service.purge_and_refresh()+ result = service.purge_and_refresh()- assert outside_canary.exists()+ assert result.success is True+ assert outside_canary.read_bytes() == b"do not delete"+ # An entry outside the backup directory is not a backup the purge could+ # remove, so it must not hold the purge warning up forever (#7443 R2).+ service._create_backup_impl.assert_called_once_with(purge_incomplete=False)@pytest.mark.skipif(__import__("sys").platform == "win32", reason="Unix-specific test")def test_purge_does_not_follow_forged_backup_symlink(- tmp_path, monkeypatch, backup_service_in_tmpdir+ tmp_path, backup_service_in_tmpdir):service = backup_service_in_tmpdiroutside_canary = tmp_path / "outside.db"outside_canary.write_bytes(b"do not delete")- symlink = service.backup_dir / "forged.db"+ symlink = service.backup_dir / "ldr_backup_forged.db"try:symlink.symlink_to(outside_canary)except OSError:pytest.skip("Cannot create symlinks in this environment")- monkeypatch.setattr(- service,- "list_backups",- lambda: [{"filename": "forged.db", "path": symlink.name}],- )-- service.purge_and_refresh()-- assert outside_canary.exists()+ # Let the real raising enumeration see a matching backup name. A mock+ # of the legacy list_backups API would no longer exercise this path.+ result = service.purge_and_refresh()++ assert result.success is True+ assert outside_canary.read_bytes() == b"do not delete"+ assert symlink.is_symlink()+ # The purge never deletes a symlink's target, so the link itself is not a+ # remaining old-password backup and cannot keep the warning up (#7443 R2).+ service._create_backup_impl.assert_called_once_with(purge_incomplete=False)diff --git a/tests/security/test_sqlcipher_private_diagnostics.py b/tests/security/test_sqlcipher_private_diagnostics.pynew file mode 100644index 0000000000..26e28d2685--- /dev/null+++ b/tests/security/test_sqlcipher_private_diagnostics.py@@ -0,0 +1,81 @@+"""Native SQLCipher errors must not be written straight to stderr."""++import json+import os+import subprocess+import sys++import pytest++from local_deep_research.database import sqlcipher_compat++[email protected]("private", [False, True])+def test_real_native_error_output_is_suppressed(tmp_path, private):+ # Exercise the installed driver in a fresh process so its global native+ # log level cannot affect this worker or another test's connections.+ script = """+import json+import sys+from local_deep_research.database.sqlcipher_compat import connect_sqlcipher, get_sqlcipher_module++driver = get_sqlcipher_module()+path, private = sys.argv[1], sys.argv[2] == 'true'+source = connect_sqlcipher(driver, path)+source.execute('PRAGMA key = "x' + chr(39) + bytes(range(32)).hex() + chr(39) + '"')+source.execute('PRAGMA cipher_compatibility = 4')+source.execute('CREATE TABLE private_data (value TEXT)')+source.execute('INSERT INTO private_data VALUES (?)', ('test-native-event',))+source.commit()+source.close()+connection = connect_sqlcipher(driver, path) if private else driver.connect(path)+if not private:+ connection.execute('PRAGMA cipher_log = stderr')+ connection.execute('PRAGMA cipher_log_level = ERROR')+supported = connection.execute('PRAGMA cipher_log_level').fetchone() is not None+connection.execute('PRAGMA key = "x' + chr(39) + bytes(reversed(range(32))).hex() + chr(39) + '"')+connection.execute('PRAGMA cipher_compatibility = 4')+denied = False+try:+ connection.execute('SELECT * FROM private_data').fetchall()+except driver.DatabaseError:+ denied = True+connection.close()+print(json.dumps({'denied': denied, 'supported': supported}))+"""+ result = subprocess.run(+ [+ sys.executable,+ "-c",+ script,+ str(tmp_path / "private.db"),+ str(private).lower(),+ ],+ capture_output=True,+ text=True,+ timeout=45,+ check=True,+ env={**os.environ, "LDR_DATA_DIR": str(tmp_path)},+ )+ report = json.loads(result.stdout)+ assert report["denied"] is True+ if private:+ assert result.stderr == ""+ else:+ if not report["supported"]:+ pytest.skip("This driver has no configurable native diagnostics")+ assert "sqlcipher" in result.stderr.lower()+++def test_failed_private_configuration_does_not_return_a_connection(monkeypatch):+ from unittest.mock import Mock++ connection = Mock()+ connection.cursor.return_value.execute.side_effect = RuntimeError(+ "privacy configuration failed"+ )+ driver = Mock(sqlite_version_info=(3, 53, 1))+ driver.connect.return_value = connection+ with pytest.raises(RuntimeError, match="privacy configuration failed"):+ sqlcipher_compat.connect_sqlcipher(driver, ":memory:")+ connection.close.assert_called_once()diff --git a/tests/web/routers/test_migration_antipattern_guards.py b/tests/web/routers/test_migration_antipattern_guards.pyindex 045938814c..014adc2105 100644--- a/tests/web/routers/test_migration_antipattern_guards.py+++ b/tests/web/routers/test_migration_antipattern_guards.py@@ -585,6 +585,23 @@ DICT_RETURN_ALLOWLIST: dict[str, str] = {"{'running': False, 'error': ...} shape as the response.status_code ""!= 200 branch just above it in the same try block."),+ "src/local_deep_research/web/routers/settings.py::api_get_backup_status": (+ "GET /settings/api/backup-status always answers 200 to "+ "authenticated callers with {'enabled', 'listing_available', "+ "'count', 'backups', 'total_size_*', ...} plus the last-attempt "+ "report; the except branch returns the exact same shape with "+ "listing_available=False / count=None -- a status report on "+ "whether the backup directory could be read, not error "+ "propagation. The 200 matters here (#7199): the last-attempt "+ "failure record is fetched before the try block, and a non-200 "+ "would make the shared JS client's catch() wipe the section to "+ "'Could not load backup status.', discarding that failure report "+ "exactly when storage trouble makes the listing failure and the "+ "backup failure correlated. Verified line-by-line against the "+ "non-exceptional return in the same try block; the paired "+ "Flask-snapshot divergence is recorded in "+ "EXPECTED_STATUS_CODES_LOST (test_route_table_parity.py)."+ ),}diff --git a/tests/web/test_route_table_parity.py b/tests/web/test_route_table_parity.pyindex 92006a05c9..e83c5002db 100644--- a/tests/web/test_route_table_parity.py+++ b/tests/web/test_route_table_parity.py@@ -1676,6 +1676,18 @@ EXPECTED_STATUS_CODES_LOST = {# the same 302 for browser requests. Paired with the auth-gate entry# above and with the 401 in EXPECTED_STATUS_CODES_GAINED.("GET", "/auth/change-password"): [302],+ # --- Backup status probe: Flask's 500 error envelope became an+ # in-band 200 status report (#7199). The endpoint reads the+ # last-attempt failure record before its try block; if listing the+ # backup directory then raises, a 500 would make the JS client's+ # catch() replace the section with a generic "Could not load backup+ # status." -- discarding the failure report this screen exists to+ # surface, exactly when storage trouble makes the two failures+ # correlated. The 200 body keeps the same keys as the success branch+ # with listing_available=False / count=None; the always-200 shape is+ # recorded in DICT_RETURN_ALLOWLIST+ # (test_migration_antipattern_guards.py). Auth failures still 401.+ ("GET", "/settings/api/backup-status"): [500],}# Status codes a FastAPI route can return that its Flask counterpart coulddiff --git a/tests/web/warning_checks/test_orchestrator.py b/tests/web/warning_checks/test_orchestrator.pyindex 73a9123b98..2f3dff39f8 100644--- a/tests/web/warning_checks/test_orchestrator.py+++ b/tests/web/warning_checks/test_orchestrator.py@@ -368,7 +368,7 @@ class TestCalculateWarningsMultipleWarnings:"llm.model": "llama3.1:70b",})- # tmp_path is an empty real directory: backup_dir.glob(...) finds no+ # tmp_path is an empty real directory: listing it finds no# ldr_backup_*.db files -> backup_count == 0 -> "no_backups" fires# (backup.enabled defaults to True in _make_settings_manager).with _patch_orchestrator(mgr, db_session=mock_db_session):@@ -550,8 +550,9 @@ class TestCalculateWarningsNoneProvider:class TestCalculateWarningsBackupGlobHardening:- """The backup glob in calculate_warnings() must skip symlinks, so a- planted symlink cannot inflate the backup count/size used for warnings.+ """The backup listing in calculate_warnings() must skip symlinks, so a+ planted symlink cannot inflate the backup count/size used for warnings,+ and must not mistake a directory it cannot read for an empty one."""def test_symlink_is_not_counted_as_a_backup(self, tmp_path):@@ -582,6 +583,46 @@ class TestCalculateWarningsBackupGlobHardening:assert "no_backups" in typesassert "backup_info" not in types+ @pytest.mark.nonroot+ def test_unlistable_directory_is_not_reported_as_having_no_backups(+ self, tmp_path, loguru_caplog+ ):+ """Path.glob returns nothing for a directory it cannot read, so a+ real permission denial is needed (root and Windows do not enforce+ one). Neither banner may be claimed when the count is unknown, and+ the skip leaves a warning for operators."""+ from local_deep_research.web.warning_checks import calculate_warnings++ if os.name != "posix" or os.geteuid() == 0:+ pytest.skip("permission bits are not enforced for root or Windows")+ backup_dir = tmp_path / "backups"+ backup_dir.mkdir()+ (backup_dir / "ldr_backup_20260101_000000.db").write_bytes(b"x")+ backup_dir.chmod(0o300)+ try:+ with loguru_caplog.at_level("DEBUG"):+ with _patch_orchestrator(_make_settings_manager()):+ with patch(+ "local_deep_research.config.paths.get_user_backup_directory",+ return_value=backup_dir,+ ):+ warnings = calculate_warnings(username="test")+ finally:+ backup_dir.chmod(0o700)++ types = {w["type"] for w in warnings}+ assert "no_backups" not in types+ assert "backup_info" not in types+ skipped = [+ record.getMessage()+ for record in loguru_caplog.records+ if record.levelname == "DEBUG"+ and "Backup banners skipped" in record.getMessage()+ ]+ assert len(skipped) == 1+ assert "PermissionError" in skipped[0]+ assert str(tmp_path) not in skipped[0]+class TestCalculateWarningsUnprotectedBannerUsesEffectiveScope:"""The loud "protection disabled" banner must be driven by the RESOLVED