* refactor(database): stage restores before publishing restore_from_file is now stage_restore + publish_staged (database/ restore_stage.py). The stage lives in a .pm-restore-* folder in the library's own folder, is migrated, has damaged workflow documents repaired from an inline copy or cleared, is verified with every document read and is bound by its SHA-256. Publishing keeps a unique, standalone, 0600 safety backup (microsecond names), copies the pages through the backup API as before and then truncates the WAL, which stayed database-sized after a restore. The safety backup writer moves to database/safety_backup.py so the database layer and maintenance share it. * fix(database): read the verify streams one row at a time The provenance and review-evidence checks iterated their cursors; on a managed connection iteration prefetches 128 rows, so up to 128 large values were held at once while the docstrings promised one. Both now use fetchone. * feat(backup): review a restore, run it as an Activity job, verify backups - POST /restore/preview stages an upload in the library's folder and answers what it holds beside the live library, writing nothing live; DELETE discards it; at most two stages are held (429 too-large). - POST /restore/apply rechecks the live fingerprint (409 plan-changed) and starts "Database restore": exclusive, not cancellable, indeterminate. It waits for request leases, publishes the exact staged bytes, swaps the database, prunes safety backups and logs counts, repairs and the backup. - The legacy /restore runs the same stage and publish as an exclusive hold with its old response, so a restore and the health check, Move library and every other job refuse each other. - JobRegistry.lease: Needs review decisions and maintenance and restore previews are refused while an exclusive job runs, and a restore waits for them before it switches the file. - GET /backup holds a guarded slot, verifies a standalone snapshot before streaming it and removes the closed temp file; GET /backup/status lists the safety backups on disk and this session's verified download. * docs(testing): backup and restore contract * test(system): a restore refuses a running health check The health route test relied on a legacy restore interleaving with a running check; the restore is now exclusive and refuses it, naming the check. The diagnostics backup tests follow the backup handler into py/api/backup_restore.py. * fix(backup): drop stale restore previews off the event loop A restore drops the previews reviewed against the old library; their stage files are now removed in the executor, with the old database's connections. * test(backup): wait for the backup hold to end before reading its state CI (py3.11/3.12 on Linux, every macOS) read the registry right after the client had the last byte, before the handler left its hold and removed its temp file: the job was still running. The tests now wait on that state. * test(images): the scan dialog's Back up now leaves a finished backup hold A backup download now holds a guarded registry slot while it streams (#249 S8), so the jobs list holds that finished hold after the dialog's Back up now; the spec still proves Cancel starts no scan. * fix(backup): final review round for the restore and backup routes - I1: once the stage's copy replaced the library the restore succeeded: pruning, reopening self.db, dropping old previews and closing the old object only log, so the job finishes and the legacy /restore answers 200 instead of "the library was not changed". The generation is always bumped. - M1: a backup download holds its guarded slot only while the snapshot is written; the stream runs outside it, so a stalled download blocks nothing. - M2: a restore preview leases only its read of the live library, not the upload or the staging; a restore whose drain still times out keeps the reviewed stage under its token. - M3: the reviewed-preview store changes only on the event loop; stage folders are removed in the executor. - M4: a document whose body inflates completely while only its recorded hash or only its length disagrees is re-keyed (or merged into the document holding that text) instead of dropped. - M5: a backup download is refused only when the snapshot is not SQLite or fails integrity_check; a semantic check failure still downloads, logged, sent as X-PromptManager-Verify and recorded as not verified. - M6: a refused, failed or cancelled preview removes its stage, also when the cancel lands while a worker still stages; a timer removes expired stages without another request. - M7, N1: repair reads rows one at a time and counts distinct images. - N3: the live fingerprint never commits a transaction it did not open. * test(backup): close the old library the close-failure test leaves open CI (py3.12 Linux and the coverage gate) found the old object's executor-thread connections still open at teardown: the test patches its close_all to fail, and once the object is collected close_all_instances no longer reaches it. The test now closes it itself. * fix(backup): re-review round for restore repair, apply and backup cleanup - R1: when re-keying merges an intact body into a holder whose own body was damaged, the rewritten holder leaves the damaged list, so the clear pass no longer deletes it and unlinks the images just moved to it. - R2: a losing duplicate apply removes a stage only when the store still held it, never one another apply's job has taken. - R3: a restore that times out waiting for requests keeps its review only while it is unexpired and its slot is free; otherwise the stage goes and the job says to try again. - R4: a backup download cancelled while its snapshot is written removes the snapshot once the worker finishes.
147 lines
5.1 KiB
Python
147 lines
5.1 KiB
Python
"""Reviewed restores: staged uploads held under single-use tokens (#249 S8).
|
|
|
|
A restore preview stages the upload (``database/restore_stage.py``) and
|
|
keeps it here under a ``secrets.token_urlsafe(32)`` token with the live
|
|
library's fingerprint and the database identity it was reviewed against.
|
|
``take`` hands the stage to the restore job exactly once; a second apply of
|
|
the token names that job (``PlanUsed``). An expired, discarded or unknown
|
|
token raises ``PlanExpired``; its stage folder is removed. A restart forgets
|
|
every token (the startup sweep removes the folders).
|
|
|
|
At most ``max_stages`` stages are held. Bytes are bounded by free disk space
|
|
(the upload is refused when the disk has no room, and held stages use that
|
|
disk), not by a fixed budget. A stage being applied belongs to its job and is
|
|
never evicted or counted.
|
|
|
|
The store is only read and changed on the event loop (it has no lock). Its
|
|
entries own stage folders, whose removal is blocking: every method that
|
|
drops an entry returns it (or keeps it in ``take_doomed``), and the caller
|
|
removes the folder in the executor (``HeldRestore.staged.discard``).
|
|
"""
|
|
|
|
import secrets
|
|
import time
|
|
from dataclasses import dataclass
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
from .maintenance_plans import PlanExpired, PlanUsed
|
|
|
|
STAGE_TTL_SECONDS = 900
|
|
MAX_STAGES = 2
|
|
_USED_KEPT = 32 # applied tokens remembered for plan-used answers
|
|
|
|
|
|
class TooManyStages(Exception):
|
|
"""Every stage slot is held by a reviewed restore."""
|
|
|
|
|
|
@dataclass
|
|
class HeldRestore:
|
|
staged: Any # database.restore_stage.StagedRestore
|
|
fingerprint: str
|
|
db_identity: Any
|
|
live: Dict[str, int]
|
|
expires_at: float
|
|
|
|
|
|
class RestorePlanStore:
|
|
def __init__(
|
|
self,
|
|
clock: Optional[Callable[[], float]] = None,
|
|
ttl: float = STAGE_TTL_SECONDS,
|
|
max_stages: int = MAX_STAGES,
|
|
):
|
|
self._ttl = ttl
|
|
self._max = max_stages
|
|
self._clock = clock or time.time
|
|
self._held: Dict[str, HeldRestore] = {}
|
|
self._used: Dict[str, str] = {}
|
|
self._doomed: List[HeldRestore] = []
|
|
|
|
@property
|
|
def ttl(self) -> float:
|
|
return self._ttl
|
|
|
|
def now(self) -> float:
|
|
return self._clock()
|
|
|
|
def __len__(self):
|
|
return len(self._held)
|
|
|
|
def full(self) -> bool:
|
|
return len(self._held) >= self._max
|
|
|
|
def issue(self, staged, fingerprint, db_identity, live) -> str:
|
|
"""Hold ``staged`` and return its token; TooManyStages when full."""
|
|
if self.full():
|
|
raise TooManyStages()
|
|
token = secrets.token_urlsafe(32)
|
|
self._held[token] = HeldRestore(
|
|
staged=staged,
|
|
fingerprint=fingerprint,
|
|
db_identity=db_identity,
|
|
live=dict(live),
|
|
expires_at=self._clock() + self._ttl,
|
|
)
|
|
return token
|
|
|
|
def peek(self, token) -> HeldRestore:
|
|
if token in self._used:
|
|
raise PlanUsed(self._used[token])
|
|
held = self._held.get(token)
|
|
if held is None:
|
|
raise PlanExpired()
|
|
if held.expires_at <= self._clock():
|
|
self._doomed.append(self._held.pop(token))
|
|
raise PlanExpired()
|
|
return held
|
|
|
|
def take(self, token, job_id) -> HeldRestore:
|
|
"""Hand the held stage to the job applying it (single use)."""
|
|
held = self.peek(token)
|
|
del self._held[token]
|
|
self._used[token] = job_id
|
|
while len(self._used) > _USED_KEPT:
|
|
self._used.pop(next(iter(self._used)))
|
|
return held
|
|
|
|
def give_back(self, token, held) -> bool:
|
|
"""Hold a taken stage again under its token (its job did not apply it).
|
|
|
|
False, holding nothing, when the review expired meanwhile or its slot
|
|
was reused (the store is full): the caller then discards the stage.
|
|
"""
|
|
if held.expires_at <= self._clock() or self.full():
|
|
return False
|
|
self._used.pop(token, None)
|
|
self._held[token] = held
|
|
return True
|
|
|
|
def entry(self, token) -> Optional[HeldRestore]:
|
|
"""The held entry, expired or not (for the response that issued it)."""
|
|
return self._held.get(token)
|
|
|
|
def pop(self, token) -> Optional[HeldRestore]:
|
|
"""Drop the token; its entry (to discard) or None when it was not held."""
|
|
return self._held.pop(token, None)
|
|
|
|
def pop_expired(self) -> List[HeldRestore]:
|
|
"""Drop every expired entry; them, with any ``peek`` found expired."""
|
|
now = self._clock()
|
|
expired = [t for t, held in self._held.items() if held.expires_at <= now]
|
|
return [self._held.pop(token) for token in expired] + self.take_doomed()
|
|
|
|
def pop_all(self) -> List[HeldRestore]:
|
|
"""Drop every entry (the library was replaced); them, to discard."""
|
|
held = list(self._held.values()) + self.take_doomed()
|
|
self._held.clear()
|
|
return held
|
|
|
|
def take_doomed(self) -> List[HeldRestore]:
|
|
doomed, self._doomed = self._doomed, []
|
|
return doomed
|
|
|
|
def next_expiry(self) -> Optional[float]:
|
|
"""When the soonest held entry expires (clock time), or None."""
|
|
return min((held.expires_at for held in self._held.values()), default=None)
|