~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(web): cascade delete_research children explicitly (#6716)
Scc87434df2bd
diff --git a/.github/workflows/docker-tests.yml b/.github/workflows/docker-tests.ymlindex bf7e1513c8..9c63d75b9e 100644--- a/.github/workflows/docker-tests.yml+++ b/.github/workflows/docker-tests.yml@@ -293,6 +293,7 @@ jobs:--color=no \-v \--durations=20 \+ -o faulthandler_timeout=60 \-n auto \-m 'not integration and not fd_canary and not serial and not nonroot' \--ignore=tests/ui_tests \diff --git a/changelog.d/6716.bugfix.md b/changelog.d/6716.bugfix.mdnew file mode 100644index 0000000000..ca950811f6--- /dev/null+++ b/changelog.d/6716.bugfix.md@@ -0,0 +1,37 @@+Hardened `research_history` deletes against out-of-repo and partial+schemas. `DELETE /api/delete/{id}`, `POST /api/clear_history`, and+`ChatService.delete_attempt` now route through a single application-level+cascade (`_delete_research_with_dependents`) instead of trusting the+schema's `ON DELETE CASCADE` rules alone. The fix is defensive: in-repo+every FK on `research_history.id` is `CASCADE`/`SET NULL` from birth,+but a partial schema or third-party plugin whose child-table FK lacks+`CASCADE` would otherwise raise `sqlcipher3.IntegrityError: FOREIGN KEY+constraint failed` (with `PRAGMA foreign_keys = ON`) and surface as a+500. (With SQLite's default `foreign_keys = OFF` the same delete+succeeds silently and orphans rows instead — a separate orphaning mode+the explicit deletes also repair, but not the 500 discriminated here.)+The `#5074` `IN_PROGRESS` / claimed-queue predicates now run inside the+helper's parent `DELETE` so a worker that wins the race between the+route's pre-check and the cascade is still observed.++The cascade now handles the full transitive closure derived from+`Base.metadata` at runtime (`_research_cascade_plan`), so plain new+data tables are picked up automatically at any depth — including+depth-2 rows under `research_resources` (`download_tracker` /+`download_duplicates` / `download_attempts` / `download_queue` /+`paper_appearances`), whose FKs are `NO ACTION` in live schemas and+aborted deletes of researches with downloaded sources even with the+first-level cascade. Rows that must survive (chat history,+library/sync/synthesis links) are cleared via an explicit survival+policy (`_CASCADE_SET_NULL`); the introspection test asserts plan+completeness over the full descendant closure, so a new table needing+survival semantics fails CI until it gains a policy entry. Each+research's child writes run inside a `SAVEPOINT`: when the parent+`DELETE` guard refuses the row (concurrent `IN_PROGRESS` flip or queue+claim), the helper rolls back that research's children and returns+`False`, so `clear_history` can safely commit siblings in the same+transaction. The helper forces a DEFERRED `BEGIN` before the savepoint+when the driver has no transaction yet (legacy SQLite/SQLCipher mode+leaves SELECT-only sessions without one, where a standalone savepoint's+RELEASE would commit early), so a caller `rollback()` after a later+failure still restores parent, children and preserved links.diff --git a/src/local_deep_research/chat/service.py b/src/local_deep_research/chat/service.pyindex 5314a9db37..6779aee474 100644--- a/src/local_deep_research/chat/service.py+++ b/src/local_deep_research/chat/service.py@@ -32,6 +32,7 @@ from ..web.routes.globals import (is_research_thread_alive,set_termination_flag,)+from ..web.routers.research import _delete_research_with_dependentsfrom ..constants import ResearchStatus# Standard exception tuple for service-layer DB operations@@ -1057,34 +1058,44 @@ class ChatService:db.query(ChatMessage).filter(msg_filter).count())- # ChatMessage.research_id FK is ON DELETE SET NULL, not- # CASCADE — explicit delete is required to remove the rows- # (otherwise the bubbles linger with a stale research_id).+ # ChatMessage rows that reference the research must+ # be removed (not SET-NULL-preserved — the chat UI+ # treats an attempt's bubbles as part of the attempt,+ # so we want them gone with the row). The helper+ # below SET-NULLs any ``chat_messages.research_id``+ # rows that survived the user_message_id branch; by+ # then the assistant rows are gone so the SET NULL+ # is a no-op against ``research_id``.db.query(ChatMessage).filter(msg_filter).delete(synchronize_session=False)- # chat_progress_steps FK is ON DELETE CASCADE, so the- # research_history.delete() below would clean them up.- # Delete explicitly so the count is predictable and the- # tx is self-contained if the CASCADE pragma ever flips.- db.query(ChatProgressStep).filter(- ChatProgressStep.research_id == research_id- ).delete(synchronize_session=False)-- # user_active_research row (per-user cap counter).- # Filtered by research_id; the username filter is- # belt-and-braces (this user's DB only contains their own- # rows).+ # user_active_research row (per-user cap counter) is+ # not a child of ``research_history.id`` via FK so+ # the helper does not touch it. Delete it here so+ # the cap counter stays accurate.db.query(UserActiveResearch).filter(UserActiveResearch.research_id == research_id).delete(synchronize_session=False)- # ResearchHistory last so its CASCADE doesn't fire while- # our explicit deletes are pending.- db.query(ResearchHistory).filter(- ResearchHistory.id == research_id- ).delete(synchronize_session=False)+ # ``_delete_research_with_dependents`` performs the+ # explicit, application-level cascade (see its+ # docstring for why we don't trust the schema's+ # ``ON DELETE CASCADE`` rules alone). The two+ # ``require_*`` guards are off: this path has+ # already established that (a) no live worker+ # thread owns the row (``AttemptInProgress`` was+ # raised above otherwise), and (b) the row may be+ # ``IN_PROGRESS`` *stale* (thread dead) — exactly+ # the shape ``#5074`` was protecting against, but+ # applied through the chat service's own liveness+ # gate rather than the helper's IN_PROGRESS check.+ _delete_research_with_dependents(+ db,+ research_id,+ require_status_not_in_progress=False,+ require_no_claimed_queue_row=False,+ )# Decrement message_count. Skipped when no messages were# removed (e.g. an attempt that crashed before anydiff --git a/src/local_deep_research/web/routers/research.py b/src/local_deep_research/web/routers/research.pyindex a76bb643ad..8b8aa3c518 100644--- a/src/local_deep_research/web/routers/research.py+++ b/src/local_deep_research/web/routers/research.py@@ -30,7 +30,7 @@ from pathlib import Pathfrom loguru import loggerfrom ...settings.logger import log_settings-from sqlalchemy import case, func+from sqlalchemy import case, func, not_, text, tuple_from sqlalchemy.orm import Sessionfrom typing import Optional, Annotated@@ -59,6 +59,7 @@ from ..services.pdf_service import ()from ...database.models import (+ Base,QueuedResearch,ResearchHistory,UserActiveResearch,@@ -1661,6 +1662,486 @@ def _unlink_report_file_if_inside_root(report_path: Optional[str]) -> bool:return False+# ---------------------------------------------------------------------------+# Application-level cascade for ``research_history`` deletions+# ---------------------------------------------------------------------------+#+# Schema reality (see ``src/local_deep_research/database/models/``;+# table names are the physical SQLite names): the ``research_history``+# descendant closure spans two dozen tables at depths 1-3, with mixed+# ``ON DELETE`` rules — several depth-2 edges (``download_tracker`` /+# ``download_duplicates`` → ``research_resources``,+# ``download_attempts`` / ``download_duplicates`` →+# ``download_tracker.url_hash``) were never declared CASCADE at all.+#+# Declared rules cannot be trusted anyway: legacy user databases keep+# whatever rules existed when each table was created (SQLite bakes FK+# rules into ``CREATE TABLE``), and ``PRAGMA foreign_keys = ON`` (set+# on every production connection) turns any gap into ``FOREIGN KEY+# constraint failed`` instead of silent orphans. The helper below+# therefore performs the *entire* cascade explicitly in application+# code and never depends on declared rules firing.+#+# How it works: ``_research_cascade_plan()`` derives the plan from+# ``Base.metadata`` on first use (metadata is fixed per process, so+# the plan is built once and cached):+# 1. Walk every FK edge transitively reachable from+# ``research_history`` — any depth, including edges that target+# non-PK unique columns such as ``download_tracker.url_hash``.+# 2. Every reached edge is classified by ``_CASCADE_SET_NULL``:+# listed ``(table, column)`` pairs are nulled (those rows survive+# — chat history, library/sync/synthesis rows outlive any single+# research); every other reached edge becomes a bulk ``DELETE``.+# 3. At runtime each step is scoped to rows reachable from the named+# research (parent keys collected top-down), nulls run first,+# deletes run deepest-first in topological order (the planner+# asserts acyclicity, so a future cyclic FK fails loudly instead+# of violating constraints at runtime).+# Sibling researches are untouched. ``url_hash`` is shared across+# researches for the same URL, so attempt/duplicate rows under a+# shared hash are inherently shared and go with the hash.+#+# Known limitations:+# 1. Tables with no FK path to ``research_history`` are invisible to+# the walk and keep prior behaviour — e.g. ``document_chunks``+# links via a rule-less ``source_id`` column (no constraint to+# violate; orphans possible, as before). Plain non-FK+# ``research_id`` columns are not deletion scopes; their rows are+# reached through their non-nullable FK key columns instead.+# 2. **Side effects.** The report file on disk and the per-user cap+# counter (``UserActiveResearch``) stay outside the helper:+# not FK targets.+#+# Coverage contract (guarded by ``TestHelperCoverageBaseMetadata`` and+# ``TestDepth2ResourceChildCascade``): the plan must classify *every*+# FK edge in the ``research_history`` descendant closure. Plain data+# tables need nothing and are picked up automatically; rows that must+# survive need an entry in ``_CASCADE_SET_NULL``. A new table, a+# renamed column, or policy drift fails CI instead of orphaning or+# 500ing in production.+_CASCADE_ROOT_TABLE = "research_history"++# (child_table, child_column) pairs whose rows SURVIVE a research+# delete with the link cleared. Every other edge reached by the+# metadata walk becomes a bulk DELETE. Keep this list to exactly the+# cases where deletion would destroy data owned by someone else:+# chat history outlives the research it discusses; library, sync and+# synthesis rows outlive any single research.+_CASCADE_SET_NULL = frozenset(+ {+ ("chat_messages", "research_id"),+ ("documents", "resource_id"),+ ("research_resources", "document_id"),+ ("download_tracker", "library_document_id"),+ ("note_syntheses", "result_document_id"),+ ("zotero_item_map", "document_id"),+ }+)++_cascade_plan_cache = None+++def _research_cascade_plan():+ """Derive ``(set_null_steps, delete_steps, table_to_class)`` from+ ``Base.metadata``.++ Each step is ``(child_table, child_column, parent_table,+ parent_column)``. Delete steps additionally carry their runtime+ order: deepest tables first, children before the parents they+ reference (topological over the reached subgraph). The result is+ cached — model metadata is fixed per process.+ """+ global _cascade_plan_cache+ if _cascade_plan_cache is not None:+ return _cascade_plan_cache++ table_to_class = {}+ for mapper in Base.registry.mappers:+ cls = mapper.class_+ table_name = getattr(cls.__table__, "name", None)+ if table_name:+ if table_name in table_to_class:+ raise AssertionError(+ f"cascade planner: table {table_name!r} maps to two classes"+ )+ table_to_class[table_name] = cls++ children_of: dict = {}+ for table in Base.metadata.tables.values():+ for fk in table.foreign_keys:+ children_of.setdefault(fk.column.table.name, []).append(+ (table.name, fk.parent.name, fk.column.name)+ )++ reached_edges = []+ levels = {_CASCADE_ROOT_TABLE: 0}+ queue = [_CASCADE_ROOT_TABLE]+ seen_edges = set()+ while queue:+ parent = queue.pop(0)+ for child, column, parent_column in sorted(children_of.get(parent, [])):+ key = (child, column, parent, parent_column)+ if key in seen_edges:+ continue+ seen_edges.add(key)+ if child not in table_to_class:+ raise AssertionError(+ "cascade planner: table "+ f"{child!r} references {parent!r} but has no "+ "mapped class — map it or exclude it explicitly"+ )+ reached_edges.append(key)+ if child not in levels:+ levels[child] = levels[parent] + 1+ queue.append(child)++ # A table's rows are deleted only when a deleted parent references+ # them through a non-surviving edge. Tables reached solely through+ # ``_CASCADE_SET_NULL`` edges (chat history, syntheses, sync state)+ # survive: edges targeting them need no step, since a row that is+ # never deleted cannot block anything.+ deleted_tables = {_CASCADE_ROOT_TABLE}+ changed = True+ while changed:+ changed = False+ for child, column, parent, _parent_column in reached_edges:+ if (+ parent in deleted_tables+ and (child, column) not in _CASCADE_SET_NULL+ and child not in deleted_tables+ ):+ deleted_tables.add(child)+ changed = True++ set_null_steps = [+ key for key in reached_edges if (key[0], key[1]) in _CASCADE_SET_NULL+ ]+ delete_steps = [+ key+ for key in reached_edges+ if (key[0], key[1]) not in _CASCADE_SET_NULL+ and key[2] in deleted_tables+ ]++ # Order deletes so every child goes before the parents it+ # references (Kahn's algorithm, table-name tie-break for+ # determinism). The planner asserts acyclicity: a future cyclic+ # FK must fail loudly here, not as a runtime IntegrityError.+ delete_tables = {step[0] for step in delete_steps}+ waiting_on = {table: set() for table in delete_tables}+ for child, _column, parent, _parent_column in delete_steps:+ if parent in waiting_on:+ waiting_on[parent].add(child)+ ordered_tables: list = []+ ready = sorted(+ table for table, blockers in waiting_on.items() if not blockers+ )+ while ready:+ table = ready.pop(0)+ ordered_tables.append(table)+ for other, blockers in waiting_on.items():+ if table in blockers:+ blockers.discard(table)+ if not blockers and other not in ordered_tables:+ ready.append(other)+ ready.sort()+ if len(ordered_tables) != len(waiting_on):+ raise AssertionError(+ "cascade planner: cyclic FKs among "+ f"{sorted(waiting_on)} — cannot order deletes"+ )+ table_rank = {table: rank for rank, table in enumerate(ordered_tables)}+ delete_steps.sort(key=lambda step: (table_rank[step[0]], step[0]))++ _cascade_plan_cache = (set_null_steps, delete_steps, table_to_class)+ return _cascade_plan_cache+++def _ensure_outer_db_transaction(db_session: Session) -> None:+ """Start a DB-level transaction when none exists yet.++ SQLite (and SQLCipher) in legacy transaction mode — the mode this+ app uses on every production connection (``isolation_level=""``,+ DEFERRED) — only opens a database transaction implicitly before+ DML. Pure ``SELECT``s leave the driver with no transaction even+ though the SQLAlchemy ``Session`` reports ``in_transaction() ==+ True``. Calling ``Session.begin_nested()`` in that state issues a+ standalone ``SAVEPOINT`` outside any ``BEGIN``; its ``RELEASE``+ then commits immediately instead of joining the caller's+ transaction, so a later ``db_session.rollback()`` cannot restore+ the rows (``clear_history`` ends up partly applied after a later+ failure). See the SQLAlchemy SQLite dialect docs on legacy+ transaction mode with the pysqlite driver.++ Probing the driver-level ``in_transaction`` flag and issuing a+ plain (DEFERRED) ``BEGIN`` when it is ``False`` makes the+ following ``begin_nested()`` a true nested savepoint: ``RELEASE``+ keeps the outer transaction open and the caller still owns+ ``commit`` / ``rollback``. DEFERRED (not IMMEDIATE) preserves the+ read-concurrency property production relies on (SELECTs take only+ a SHARED lock). Best-effort: when the driver state cannot be+ determined, proceed without forcing ``BEGIN`` rather than risking+ a spurious ``cannot start a transaction within a transaction``+ that would poison the session.+ """+ try:+ connection = db_session.connection()+ raw = getattr(+ getattr(connection, "connection", None), "driver_connection", None+ )+ if raw is None:+ logger.warning(+ "research cascade: driver_connection unavailable, proceeding "+ "without a forced BEGIN — a standalone savepoint may commit "+ "early if the driver has no transaction"+ )+ return+ in_txn = getattr(raw, "in_transaction", None)+ if in_txn is None:+ logger.warning(+ "research cascade: driver does not expose in_transaction, "+ "proceeding without a forced BEGIN — a standalone savepoint "+ "may commit early if the driver has no transaction"+ )+ return+ is_in_txn = in_txn() if callable(in_txn) else bool(in_txn)+ if not is_in_txn:+ db_session.execute(text("BEGIN"))+ except Exception:+ logger.exception(+ "research cascade: could not ensure outer transaction before "+ "savepoint — proceeding with a possibly standalone savepoint "+ "that may commit early"+ )+++def _delete_research_with_dependents(+ db_session: Session,+ research_id: str,+ *,+ require_status_not_in_progress: bool = True,+ require_no_claimed_queue_row: bool = True,+) -> bool:+ """Delete ``research_history.id = research_id`` and every row that+ references it, *atomically*.++ All child-table deletes and the parent delete are issued against the+ same SQLAlchemy session without an intervening commit, so the row+ is removed in one transaction. The two ``require_*`` flags gate the+ parent DELETE on the same predicates the original ``#5074`` guard+ embedded inline:++ * ``require_status_not_in_progress`` — refuses rows still+ ``IN_PROGRESS`` (a worker owns them).+ * ``require_no_claimed_queue_row`` — refuses rows whose+ ``QueuedResearch`` entry has ``is_processing = True`` (a worker is+ about to start or has not finished writing back the row).++ When either flag is ``False`` the corresponding predicate is dropped+ entirely. Both default to ``True`` so the helper applies the full+ guard by default — callers that genuinely want an unconditional+ cascade (e.g. the chat service's per-attempt teardown, where a+ pre-loaded liveness check has already ruled out a live worker) opt+ out explicitly. The predicates run **inside** the parent ``DELETE``+ so a concurrent claim that lands between the helper's read and its+ delete is observed by SQL and the row is preserved.++ Atomicity per research: child writes run inside a ``SAVEPOINT``+ (``Session.begin_nested``). When the parent ``DELETE`` deletes zero+ rows — ghost row or a guard refusal — the savepoint is rolled back+ so the ``False`` return leaves parent *and* children untouched. A+ ``True`` return releases the savepoint into the caller's outer+ transaction (the caller still owns ``commit`` / ``rollback``). This+ is what makes ``clear_history``'s multi-row loop safe: one+ protected row's rollback does not discard its siblings' deletes.++ The savepoint is only nested when a real database transaction+ already exists. Phase A is read-only, and in legacy transaction+ mode (production SQLCipher/SQLite with ``isolation_level=""``)+ SELECTs do not open one — so the helper forces a DEFERRED+ ``BEGIN`` first (see ``_ensure_outer_db_transaction``). Without+ it, ``RELEASE`` commits immediately and a later caller+ ``rollback()`` cannot restore parent or children.++ The helper performs an explicit, application-level cascade driven+ by ``_research_cascade_plan()`` (derived from ``Base.metadata``,+ so new plain-data tables are picked up with no code change). See+ the block above for why we don't rely on the DB-level+ ``ON DELETE CASCADE``. Caller is responsible for the surrounding+ transaction (``commit`` / ``rollback``).++ Returns ``True`` iff the ``research_history`` row was deleted.+ """+ set_null_steps, delete_steps, table_to_class = _research_cascade_plan()++ def _pk_columns(table):+ return [table.c[name] for name in table.primary_key.columns.keys()]++ def _pk_filter(table, pk_values):+ pk_cols = _pk_columns(table)+ if len(pk_cols) == 1:+ return pk_cols[0].in_([row[0] for row in pk_values])+ return tuple_(*pk_cols).in_(pk_values)++ # Phase A: resolve scopes top-down. ``scopes[table]`` holds the PK+ # tuples reachable from the named research; each step's parent+ # values are the referenced-column values over those rows. Edges+ # resolve in rounds (a step resolves once its parent table's scope+ # is final), so same-level parents — e.g. ``note_references``+ # scoped by both ``research_history`` and ``documents`` — work.+ # Read-only SELECTs only; no writes happen before Phase B.+ root_table = table_to_class[_CASCADE_ROOT_TABLE].__table__+ scopes = {+ _CASCADE_ROOT_TABLE: db_session.query(*_pk_columns(root_table))+ .filter(root_table.c.id == research_id)+ .all()+ }+ edge_parent_values: dict = {}++ def _resolve_step(child, column, parent, parent_column):+ parent_table = table_to_class[parent].__table__+ parent_pks = scopes.get(parent, [])+ if not parent_pks:+ edge_parent_values[(child, column, parent, parent_column)] = []+ return []+ values = [+ value+ for (value,) in db_session.query(parent_table.c[parent_column])+ .filter(_pk_filter(parent_table, parent_pks))+ .distinct()+ .all()+ if value is not None+ ]+ edge_parent_values[(child, column, parent, parent_column)] = values+ return values++ # A table's scope is final once every DELETE edge into it has+ # resolved (SET NULL edges never extend a scope — nulled rows+ # survive, so nothing traverses from them). DELETE edges form a+ # DAG (asserted by the planner), and every such edge is processed+ # parent-first, so this loop always terminates; the AssertionError+ # below is a safety net, not an expected path.+ all_steps = set_null_steps + delete_steps+ delete_step_set = set(delete_steps)+ resolved_tables = {_CASCADE_ROOT_TABLE}+ remaining_steps = set(all_steps)+ while remaining_steps:+ resolvable = sorted(+ step for step in remaining_steps if step[2] in resolved_tables+ )+ if not resolvable:+ raise AssertionError(+ "cascade planner: unresolvable scope chain from "+ f"{sorted(remaining_steps)} — cyclic FKs?"+ )+ for step in resolvable:+ child, column, parent, parent_column = step+ values = _resolve_step(child, column, parent, parent_column)+ if step in delete_step_set and values:+ child_table = table_to_class[child].__table__+ scopes.setdefault(child, []).extend(+ tuple(row)+ for row in db_session.query(*_pk_columns(child_table))+ .filter(child_table.c[column].in_(values))+ .all()+ )+ remaining_steps.discard(step)+ # Mark a table resolved once every DELETE edge into it has+ # resolved. NB: this must scan tables, not remaining steps —+ # a table whose edges all resolved in an earlier round is no+ # longer in ``remaining_steps`` but still needs marking so+ # deeper edges become resolvable.+ done_steps = set(all_steps) - remaining_steps+ for table in {step[0] for step in delete_steps}:+ if table not in resolved_tables and all(+ incoming in done_steps+ for incoming in delete_steps+ if incoming[0] == table+ ):+ resolved_tables.add(table)++ # Ghost row fast-path: no writes, no savepoint.+ if not scopes[_CASCADE_ROOT_TABLE]:+ return False++ # Phases B–D run inside a SAVEPOINT so a zero-row parent DELETE+ # (ghost or guard refusal) rolls back the child writes for *this*+ # research only. Without this, ``clear_history`` would commit the+ # orphaned children of a skipped row along with its siblings.+ # The outer DB transaction must exist first: in legacy mode a+ # SELECT-only session has none, and a standalone SAVEPOINT's+ # RELEASE commits early (caller rollback then restores nothing).+ _ensure_outer_db_transaction(db_session)+ savepoint = db_session.begin_nested()+ try:+ # Phase B: clear the surviving links first (all such columns are+ # nullable). Nulling before deleting means the deletes below never+ # meet a row that still points at them, on any schema.+ for child, column, parent, parent_column in set_null_steps:+ values = edge_parent_values[(child, column, parent, parent_column)]+ if not values:+ continue+ cls = table_to_class[child]+ db_session.query(cls).filter(+ getattr(cls, column).in_(values)+ ).update({column: None}, synchronize_session=False)++ # Phase C: bulk deletes in planner order (deepest first), each+ # scoped to its own edge. Scopes were fully resolved in Phase A,+ # so no reads are needed here.+ for child, column, parent, parent_column in delete_steps:+ values = edge_parent_values[(child, column, parent, parent_column)]+ if not values:+ continue+ cls = table_to_class[child]+ child_table = cls.__table__+ db_session.query(cls).filter(+ child_table.c[column].in_(values)+ ).delete(synchronize_session=False)++ # Phase D: finally, the parent row itself. The ``#5074``+ # predicates are applied INSIDE this DELETE so a concurrent queue+ # claim or status write observed by the row sees them atomically —+ # mirroring the old ``DELETE ... WHERE id = ? AND status !=+ # IN_PROGRESS AND ~claimed_queue_row`` shape. A zero-row return+ # means the row did not exist or one of the guards blocked it;+ # callers that map the latter to a 400 must check the row+ # externally (the helper deliberately does not distinguish).+ parent_filters = [ResearchHistory.id == research_id]+ if require_status_not_in_progress:+ parent_filters.append(+ ResearchHistory.status != ResearchStatus.IN_PROGRESS+ )+ if require_no_claimed_queue_row:+ claimed_queue_row = db_session.query(QueuedResearch.id).filter(+ QueuedResearch.research_id == research_id,+ QueuedResearch.is_processing.is_(True),+ )+ parent_filters.append(not_(claimed_queue_row.exists()))++ deleted = (+ db_session.query(ResearchHistory)+ .filter(*parent_filters)+ .delete(synchronize_session=False)+ )++ if not deleted:+ # Guard refused (or ghost raced away): undo this research's+ # child writes, keep the outer transaction for siblings.+ savepoint.rollback()+ return False+ savepoint.commit()+ return True+ except Exception:+ try:+ savepoint.rollback()+ except Exception as rollback_exc:+ logger.debug(+ f"research cascade savepoint rollback failed: {rollback_exc}"+ )+ raise++@router.delete("/api/delete/{research_id}")def delete_research(request: Request,@@ -1683,8 +2164,13 @@ def delete_research(status = research.statusreport_path = research.report_path- # Don't allow deleting research in progress- # Don't allow deleting research in progress.+ # Atomic guard, part 1: refuse the obvious "row is+ # IN_PROGRESS" case before opening the helper. The helper+ # repeats this check *inside* the parent DELETE so a row+ # that becomes IN_PROGRESS between here and the DELETE+ # wins the race (the same #5074 invariant the old+ # inline-WHERE shape preserved). The two checks together+ # guarantee we never silently delete a row a worker owns.if status == ResearchStatus.IN_PROGRESS:return JSONResponse({@@ -1694,28 +2180,19 @@ def delete_research(status_code=400,)- # Delete the DB record atomically, but never out from under an- # active run: skip rows still IN_PROGRESS or claimed by a- # processing queue row (#5074), so a queued/running research can't- # be deleted from beneath the worker.- claimed_queue_row = (+ # Atomic guard, part 2: the claimed-queue predicate. Again,+ # the helper repeats this inside the parent DELETE; the+ # pre-check exists only to fast-path the 400 so we don't+ # pay for the cascade on a row we already know is claimed.+ is_claimed = db_session.query(db_session.query(QueuedResearch.id).filter(QueuedResearch.research_id == research_id,QueuedResearch.is_processing.is_(True),).exists()- )- deleted = (- db_session.query(ResearchHistory)- .filter(- ResearchHistory.id == research_id,- ResearchHistory.status != ResearchStatus.IN_PROGRESS,- ~claimed_queue_row,- )- .delete(synchronize_session=False)- )- if not deleted:+ ).scalar()+ if is_claimed:return JSONResponse({"status": "error",@@ -1724,6 +2201,31 @@ def delete_research(status_code=400,)+ # Application-level cascade with the #5074 predicates+ # baked into the parent DELETE (see+ # ``_delete_research_with_dependents``).+ deleted = _delete_research_with_dependents(db_session, research_id)+ if not deleted:+ # A concurrent request claimed the row between our+ # pre-check and the helper's parent DELETE — the+ # #5074 invariant holds (the row still exists, the+ # worker is in charge). Surface the same 400 the+ # pre-check would have emitted.+ if db_session.get(ResearchHistory, research_id) is not None:+ return JSONResponse(+ {+ "status": "error",+ "message": (+ "Cannot delete research that is in progress"+ ),+ },+ status_code=400,+ )+ # Concurrent delete instead: the row is already gone,+ # honour the user's intent.+ db_session.commit()+ return {"status": "success"}+# Delete report file if it exists. `report_path` comes from the# DB column — normally written by the research worker pointing at# the user's reports dir, but a corrupted row (or future buggy@@ -1731,6 +2233,15 @@ def delete_research(# and confirms containment before unlinking._unlink_report_file_if_inside_root(report_path)+ # ``user_active_researches`` is not a CASCADE child of+ # ``research_history.id`` (see ``chat/service.py``'s parallel+ # cleanup), so the helper above does not touch it. Drop any+ # leftover per-user counter row here so deleting a research+ # doesn't leave the slot counted as occupied forever.+ db_session.query(UserActiveResearch).filter(+ UserActiveResearch.research_id == research_id,+ ).delete(synchronize_session=False)+from ..queue.lifecycle_cleanup import cleanup_queued_research_statecleanup_queued_research_state(db_session, [research_id])@@ -1778,24 +2289,26 @@ def clear_history(deleted_ids = []for research_id in deletable_ids:- claimed_queue_row = (+ # The #5074 atomic guard is applied INSIDE the helper's+ # parent DELETE; a row whose queue entry was claimed+ # after this snapshot was taken is observed by SQL and+ # preserved. The pre-check below is purely a fast-path+ # so we don't pay for the cascade on a row we already+ # know is claimed.+ is_claimed = db_session.query(db_session.query(QueuedResearch.id).filter(QueuedResearch.research_id == research_id,QueuedResearch.is_processing.is_(True),).exists()- )- deleted = (- db_session.query(ResearchHistory)- .filter(- ResearchHistory.id == research_id,- ResearchHistory.status != ResearchStatus.IN_PROGRESS,- ~claimed_queue_row,- )- .delete(synchronize_session=False)- )- if deleted:+ ).scalar()+ if is_claimed:+ continue+ # Application-level cascade with the IN_PROGRESS and+ # claimed-queue predicates baked into the parent+ # DELETE; see ``_delete_research_with_dependents``.+ if _delete_research_with_dependents(db_session, research_id):deleted_ids.append(research_id)if deleted_ids:diff --git a/tests/chat/test_chat_attempt_delete_retry.py b/tests/chat/test_chat_attempt_delete_retry.pyindex 2ccf8ea6c9..d856c915d2 100644--- a/tests/chat/test_chat_attempt_delete_retry.py+++ b/tests/chat/test_chat_attempt_delete_retry.py@@ -444,6 +444,111 @@ class TestDeleteAttemptService:is None)+ def test_delete_attempt_cascades_note_references_via_helper(+ self, setup_database_for_all_tests+ ):+ """Chokepoint coverage: ``ChatService.delete_attempt`` is the+ third place that deletes a ``ResearchHistory`` row. Before+ the helper, the chat service hand-rolled its own cascade and+ missed ``NoteReference`` (a citation-link table on+ ``target_research_id``); rows in that table pointing at the+ deleted attempt would orphan and surface an FK violation on+ the next attempt insert. After the helper, every+ FK-referencing row pointing at the attempt is gone — this+ test pins that expansion."""+ from local_deep_research.database.models import (+ NoteReference,+ )++ SessionLocal = setup_database_for_all_tests+ username = f"alice_{uuid.uuid4().hex[:8]}"+ service = ChatService(username=username)+ # A document to act as NoteReference.note_id (the FK requires+ # a row in the documents table — but NoteReference FK is to+ # Documents(id) without ON DELETE CASCADE, so we just need+ # the row to exist).+ from local_deep_research.database.models import (+ Collection,+ Document,+ SourceType,+ )++ with SessionLocal() as db:+ session_id = _make_session(db, username)+ research_id = str(uuid.uuid4())+ user_msg_id, _ = _seed_attempt(+ db,+ username,+ session_id,+ research_id,+ research_status=ResearchStatus.FAILED.value,+ )++ source_type_id = f"st-{research_id}"+ db.add(+ SourceType(+ id=source_type_id,+ name=source_type_id,+ display_name="Research Download",+ icon="download",+ )+ )+ doc_id = f"doc-{research_id}"+ db.add(+ Document(+ id=doc_id,+ research_id=research_id,+ source_type_id=source_type_id,+ document_hash=f"hash-{research_id}",+ file_size=10,+ file_type="txt",+ )+ )+ db.add(+ Collection(+ id=f"coll-{research_id}",+ name="c",+ collection_type="user_collection",+ )+ )+ db.add(+ NoteReference(+ note_id=doc_id,+ target_research_id=research_id,+ created_at=datetime.now(UTC),+ )+ )+ db.commit()++ # Sanity: a NoteReference row pointing at our research.+ assert (+ db.query(NoteReference)+ .filter_by(target_research_id=research_id)+ .count()+ == 1+ )++ with _service_db(SessionLocal):+ assert service.delete_attempt(session_id, research_id) is True++ with SessionLocal() as db:+ # The attempt is gone AND the NoteReference row that+ # pointed at it — the helper now owns that branch.+ assert (+ db.query(ResearchHistory).filter_by(id=research_id).first()+ is None+ )+ assert (+ db.query(NoteReference)+ .filter_by(target_research_id=research_id)+ .count()+ == 0+ ), (+ "ChatService.delete_attempt must route through the "+ "helper so NoteReference rows pointing at the "+ "attempt are removed"+ )+# ===========================================================================# Service-level: ChatService.get_original_attempt_querydiff --git a/tests/database/test_model_constraint_contracts.py b/tests/database/test_model_constraint_contracts.pyindex 29177c844c..ae5f76d3ac 100644--- a/tests/database/test_model_constraint_contracts.py+++ b/tests/database/test_model_constraint_contracts.py@@ -583,18 +583,19 @@ def test_research_delete_cascade_is_blocked_by_no_action_parents():def test_deleting_a_research_with_a_downloaded_pdf_raises(db):- """DEFECT: a research whose sources were downloaded can never be- deleted.-- ``web/routers/research.py::delete_research`` issues a bulk- ``ResearchHistory ... .delete(synchronize_session=False)`` and leans- entirely on DB-level CASCADE. That cascade reaches- ``research_resources``, whose rows are still referenced NOT NULL /- NO ACTION from ``download_tracker.first_resource_id``. With- ``PRAGMA foreign_keys=ON`` (set for every real connection in+ """Negative control for the raw bulk-delete pattern (not the route).++ A bare ``ResearchHistory ... .delete(synchronize_session=False)``+ that leans entirely on DB-level CASCADE aborts: the cascade+ reaches ``research_resources``, whose rows are still referenced+ NOT NULL / NO ACTION from ``download_tracker.first_resource_id``.+ With ``PRAGMA foreign_keys=ON`` (set for every real connection in``sqlcipher_utils.py``) SQLite aborts the whole statement. The- route's blanket ``except Exception`` turns it into an opaque HTTP 500,- so the row is undeletable forever.+ route no longer uses this pattern -- it goes through+ ``_delete_research_with_dependents``, which deletes the blocking+ rows first (see ``test_delete_research_helper_cleans_...`` below+ and ``TestDepth2ResourceChildCascade``) -- so this test pins the+ pattern, not the current route behaviour."""research_id = str(uuid.uuid4())db.add(@@ -636,17 +637,24 @@ def test_deleting_a_research_with_a_downloaded_pdf_raises(db):assert db.get(ResearchHistory, research_id) is not None-def test_delete_research_route_has_no_download_tracker_cleanup():- """The route relies on the cascade alone -- nothing deletes the- blocking rows first."""- source = (SRC_ROOT / "web" / "routers" / "research.py").read_text(- encoding="utf-8"+def test_delete_research_helper_cleans_download_tracker_rows():+ """The NO ACTION blockers above must be deleted explicitly before+ the ``research_resources`` rows go away -- production 2026-09-23+ proved the DB cascade alone 500s on researches with downloaded+ sources. The cascade is metadata-driven+ (``_research_cascade_plan``), so the tables are not named in the+ helper body; assert instead that the plan classifies the+ download-tracker edges as deletes."""+ from local_deep_research.web.routers.research import (+ _research_cascade_plan,)- start = source.index("def delete_research(")- body = source[start : source.index("\ndef ", start + 1)]- assert ".delete(synchronize_session=False)" in body- assert "DownloadTracker" not in body- assert "DownloadDuplicates" not in body++ _set_null_steps, delete_steps, _table_to_class = _research_cascade_plan()+ pairs = {(step[0], step[1]) for step in delete_steps}+ assert ("download_tracker", "first_resource_id") in pairs+ assert ("download_duplicates", "resource_id") in pairs+ assert ("download_duplicates", "url_hash") in pairs+ assert ("download_attempts", "url_hash") in pairs# ===========================================================================diff --git a/tests/web/routers/test_delete_research_cascade.py b/tests/web/routers/test_delete_research_cascade.pynew file mode 100644index 0000000000..86e821962c--- /dev/null+++ b/tests/web/routers/test_delete_research_cascade.py@@ -0,0 +1,1739 @@+"""Regression tests for the ``research_history`` deletion cascade.++Background+----------+``DELETE /api/delete/{research_id}`` and ``POST /api/clear_history`` used+to issue a bulk ``ResearchHistory.delete(synchronize_session=False)`` and+rely on SQLite's ``ON DELETE CASCADE`` rules (set up by the model layer+and enabled at connect time by ``apply_performance_pragmas``) to clean up+the rows in ``app_logs`` (model ``ResearchLog``),+``research_resources``, etc. In-repo every FK on+``research_history.id`` is ``CASCADE``/``SET NULL`` from birth, so a+fresh schema never raises — the fix is defensive against out-of-repo+/ partial schemas where a child table's FK lacks ``ON DELETE+CASCADE``. With ``PRAGMA foreign_keys = ON`` that shape raises+``FOREIGN KEY constraint failed`` and the route surfaces a 500 (with+the SQLite default of ``OFF`` the same delete succeeds silently and+orphans rows instead — a separate orphaning mode the explicit+deletes also repair, but not the 500 discriminated here). The fix+performs the first-level cascade explicitly at the application layer;+these tests pin six properties:++1. **Coverage** — every model that declares an FK on+ ``research_history.id`` is deleted (or, for the SET NULL FK on+ ``chat_messages.research_id``, cleared). An introspection test+ walks ``Base.metadata`` so a future FK that lands outside the+ list fails CI.+2. **Robustness against partial schemas** — the route-level tests+ rebuild a CASCADE-bearing child table (``research_strategies``)+ *without* ``ON DELETE CASCADE``, mirroring the production+ fragility. On ``main`` the route returns 500; on this branch it+ answers 200 with the row gone. A negative-control class proves the+ same broken behaviour still happens for the bulk-delete shape that+ the helper replaced.+3. **Atomic guard** — the ``#5074`` predicates (``status !=+ IN_PROGRESS`` and ``~claimed_queue_row``) live INSIDE the helper's+ parent ``DELETE``, so a row that a worker claims between the+ route's pre-check and the helper's delete is preserved. Tests+ below simulate the race by flipping the row's status exactly+ between the two statements and assert the row survives.+4. **FK enforcement** — the cascade works with ``PRAGMA+ foreign_keys = ON``, which is what production sets on every+ connection. The existing ``db`` fixture in+ ``test_research_mutating_route_effects.py`` does not enable this+ pragma, so its tests cannot distinguish the broken behavior from+ the working one. This file's ``db_with_fk`` fixture does.+5. **Public contract** — the routes' response shapes and the+ ``cleanup_queued_research_state`` call are unchanged.+6. **Caller rollback** — the helper's ``SAVEPOINT`` stays nested in+ the caller's transaction (a DEFERRED ``BEGIN`` is forced when the+ driver has none yet, since legacy mode leaves SELECT-only+ sessions without one), so a caller ``rollback()`` after success —+ or a later failure in a multi-row ``clear_history`` — restores+ parent, children and preserved links.++The fixtures here build a real in-memory SQLite database that mirrors+the production schema (same ``Base.metadata.create_all``) and enable+``PRAGMA foreign_keys`` on every connection, exactly as+``apply_performance_pragmas`` does in production.+"""++from contextlib import contextmanager+from datetime import UTC, datetime+from unittest.mock import patch++import pytest+from sqlalchemy import create_engine, event+from sqlalchemy.orm import sessionmaker+from sqlalchemy.pool import StaticPool+++_NOW = datetime(2025, 1, 1, tzinfo=UTC)++_RR = "local_deep_research.web.routers.research"+_LIFECYCLE_CLEANUP = (+ "local_deep_research.web.queue.lifecycle_cleanup."+ "cleanup_queued_research_state"+)++_ENGINE_KW = {+ "connect_args": {"check_same_thread": False},+ "poolclass": StaticPool,+}+++def db_with_fk():+ """A real in-memory SQLite database with FK enforcement ON, mirroring+ production. Without ``PRAGMA foreign_keys = ON`` the cascade rules+ are inert and the broken behaviour is invisible; this fixture makes+ the cascade rules actually fire, which is what the bug needs to+ reproduce."""+ from local_deep_research.database.models import Base++ engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)++ @event.listens_for(engine, "connect")+ def _enable_fk(dbapi, _connection_record):+ dbapi.execute("PRAGMA foreign_keys = ON")++ Base.metadata.create_all(engine)+ session = sessionmaker(bind=engine)()+ try:+ yield session+ finally:+ session.close()+ engine.dispose()+++def _patch_session(session):+ @contextmanager+ def _ctx(*_args, **_kwargs):+ yield session++ return patch(f"{_RR}.get_user_db_session", _ctx)+++def _add_research(session, rid, status="completed", **kwargs):+ from local_deep_research.database.models import ResearchHistory++ row = ResearchHistory(+ id=rid,+ query=kwargs.pop("query", "q"),+ mode=kwargs.pop("mode", "quick"),+ status=status,+ created_at=kwargs.pop("created_at", _NOW),+ **kwargs,+ )+ session.add(row)+ session.commit()+ return row+++def _populate_all_child_tables(session, research_id):+ """Insert one row in every table that declares an FK on+ ``research_history.id``. Returns the inserted rows so a test can+ assert they were all deleted.++ The ``chat_messages.research_id`` row is the only one whose FK has+ ``ON DELETE SET NULL``: the message is preserved with its link+ cleared. All the others use ``ON DELETE CASCADE`` and must vanish+ entirely.+ """+ from local_deep_research.database.models import (+ ChatMessage,+ ChatProgressStep,+ ChatRole,+ ChatMessageType,+ ChatSession,+ ChatSessionStatus,+ Collection,+ Document,+ NoteReference,+ NoteResearch,+ ResearchLog,+ ResearchResource,+ ResearchStrategy,+ SourceType,+ )++ # A SourceType row is required before any Document can be inserted+ # -- ``documents.source_type_id`` is non-nullable.+ source_type_id = "st-" + research_id+ session.add(+ SourceType(+ id=source_type_id,+ name="research_download_" + research_id,+ display_name="Research Download",+ icon="download",+ )+ )++ # ChatMessage is SET NULL; the chat_sessions row must exist before+ # chat_messages.session_id FK can be satisfied.+ session_id = "sess-" + research_id+ session.add(+ ChatSession(+ id=session_id,+ title="t",+ status=ChatSessionStatus.ACTIVE,+ )+ )+ chat_msg = ChatMessage(+ id="cm-" + research_id,+ session_id=session_id,+ research_id=research_id,+ role=ChatRole.USER,+ message_type=ChatMessageType.QUERY,+ content="hello",+ sequence_number=1,+ created_at=_NOW,+ )+ session.add(chat_msg)++ progress_step = ChatProgressStep(+ id="cps-" + research_id,+ research_id=research_id,+ session_id=session_id,+ content="step",+ sequence_number=1,+ created_at=_NOW,+ )+ session.add(progress_step)++ coll = Collection(+ id="coll-" + research_id,+ name="c",+ collection_type="user_collection",+ )+ session.add(coll)+ document = Document(+ id="doc-" + research_id,+ research_id=research_id,+ source_type_id=source_type_id,+ document_hash="hash-" + research_id,+ file_size=10,+ file_type="txt",+ )+ session.add(document)++ note_research = NoteResearch(+ document_id=document.id,+ research_id=research_id,+ created_at=_NOW,+ )+ session.add(note_research)++ note_ref = NoteReference(+ note_id=document.id,+ target_research_id=research_id,+ created_at=_NOW,+ )+ session.add(note_ref)++ log = ResearchLog(+ timestamp=_NOW,+ message="m",+ module="mod",+ function="fn",+ line_no=1,+ level="INFO",+ research_id=research_id,+ )+ session.add(log)++ resource = ResearchResource(+ research_id=research_id,+ title="r",+ url="https://example.com",+ source_type="web",+ created_at=_NOW,+ )+ session.add(resource)++ strategy = ResearchStrategy(+ research_id=research_id,+ strategy_name="s",+ created_at=_NOW,+ )+ session.add(strategy)++ session.commit()++ return {+ "chat_message": chat_msg,+ "progress_step": progress_step,+ "document": document,+ "collection": coll,+ "note_research": note_research,+ "note_ref": note_ref,+ "log": log,+ "resource": resource,+ "strategy": strategy,+ }+++# ---------------------------------------------------------------------------+# Direct unit tests on the helper+# ---------------------------------------------------------------------------+++class TestDeleteResearchWithDependentsHelper:+ """The helper is the single source of truth for which child rows go+ away when a research is removed. Pin its behaviour against every+ FK-referencing model."""++ def test_removes_research_history_and_all_cascade_children(+ self, db_with_fk+ ):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ inserted = _populate_all_child_tables(db_with_fk, "r1")++ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ db_with_fk.commit()++ from local_deep_research.database.models import (+ ChatMessage,+ ChatProgressStep,+ Document,+ NoteReference,+ NoteResearch,+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ ResearchStrategy,+ )++ # Parent and every CASCADE child gone.+ assert db_with_fk.get(ResearchHistory, "r1") is None+ assert db_with_fk.query(ResearchResource).count() == 0+ assert db_with_fk.query(ResearchLog).count() == 0+ assert db_with_fk.query(ResearchStrategy).count() == 0+ assert db_with_fk.query(ChatProgressStep).count() == 0+ assert db_with_fk.query(NoteResearch).count() == 0+ assert db_with_fk.query(NoteReference).count() == 0+ # Document cascade — documents.research_id is CASCADE, the+ # helper deletes them explicitly. document_chunks /+ # document_blobs / etc. cascade off documents.id via their+ # own rules.+ assert db_with_fk.query(Document).count() == 0++ # ChatMessage.research_id is ON DELETE SET NULL: the message+ # row survives, only the link is cleared.+ surviving_msg = db_with_fk.get(ChatMessage, inserted["chat_message"].id)+ assert surviving_msg is not None, "chat content must be preserved"+ assert surviving_msg.research_id is None, (+ "chat_messages.research_id must be cleared, not the row"+ )++ def test_no_op_when_research_id_does_not_exist(self, db_with_fk):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ assert _delete_research_with_dependents(db_with_fk, "ghost") is False++ def test_with_fk_enforcement_on_does_not_raise_integrity_error(+ self, db_with_fk+ ):+ """Reproduces the production bug surface: with+ ``PRAGMA foreign_keys = ON``, every known FK-referencing+ table has a row pointing at the research. The helper must+ NOT raise, even though the DB-level cascade path has been+ seen to silently fail on real installs (see the long comment+ above ``_delete_research_with_dependents``)."""+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")++ # Must not raise. The bug surfaced as a raw+ # ``sqlcipher3.IntegrityError`` from inside the route.+ result = _delete_research_with_dependents(db_with_fk, "r1")+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ assert result is True+ assert db_with_fk.get(ResearchHistory, "r1") is None++ def test_old_bulk_delete_pattern_fails_when_known_child_table_has_no_cascade(+ self, db_with_fk+ ):+ """Negative control: prove the OLD bulk-delete pattern fails+ under the exact failure mode the helper exists to fix.++ We add an extra child table whose FK to+ ``research_history.id`` has NO ``ON DELETE CASCADE`` rule,+ mimicking the real-world shape: an install where the live+ schema's CASCADE coverage was incomplete (e.g. pre-migration+ 0008). A raw ``.delete()`` then raises ``IntegrityError``+ exactly as the production logs show.+ """+ from sqlalchemy import text+ from sqlalchemy.exc import IntegrityError++ _add_research(db_with_fk, "r1")++ # Sanity: confirm the fixture's FK pragma is actually ON.+ # Without it, the production failure mode does not surface+ # — every delete succeeds because SQLite silently ignores+ # FK rules when ``foreign_keys = OFF``.+ fk_state = db_with_fk.execute(text("PRAGMA foreign_keys")).scalar()+ assert fk_state == 1, (+ "test fixture must run with PRAGMA foreign_keys = ON "+ "for the failure mode to surface"+ )++ # A child table whose FK has NO ``ON DELETE CASCADE`` and+ # holds a row pointing at our research. Any code path that+ # trusts the schema's cascade rules alone will raise+ # FOREIGN KEY constraint failed when deleting the parent.+ db_with_fk.execute(+ text(+ "CREATE TABLE orphan_children ("+ " id INTEGER PRIMARY KEY,"+ " research_id VARCHAR(36) NOT NULL,"+ " FOREIGN KEY (research_id) "+ " REFERENCES research_history(id)"+ ")"+ )+ )+ db_with_fk.execute(+ text("INSERT INTO orphan_children (research_id) VALUES (:rid)"),+ {"rid": "r1"},+ )+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ # The old pattern: a single bulk DELETE on the parent.+ # This is the exact statement that raised FOREIGN KEY+ # constraint failed in production logs.+ with pytest.raises(IntegrityError) as excinfo:+ db_with_fk.query(ResearchHistory).filter(+ ResearchHistory.id == "r1"+ ).delete(synchronize_session=False)+ db_with_fk.commit()++ # The DBAPI message is the reliable signal — class can vary+ # by dialect but the wording is identical.+ assert "FOREIGN KEY" in str(excinfo.value).upper()++ def test_only_targets_the_named_research(self, db_with_fk):+ """Sibling rows belonging to a different research must survive.++ The bulk ``.delete()`` shape filters on ``research_id``; if a+ future refactor accidentally drops the filter, every research+ in the table disappears. This test pins the filter."""+ from local_deep_research.database.models import (+ ResearchLog,+ ResearchResource,+ ResearchStrategy,+ )+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r2")++ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ db_with_fk.commit()+ # Expire so the asserts below don't try to refresh the just-+ # deleted ORM identities for r1's rows.+ db_with_fk.expire_all()++ def _count_for(model, research_id):+ return (+ db_with_fk.query(model)+ .filter(model.research_id == research_id)+ .count()+ )++ # r1's children are gone across the board ...+ for model in (ResearchLog, ResearchResource, ResearchStrategy):+ assert _count_for(model, "r1") == 0, (+ f"{model.__name__} rows for r1 should have been deleted"+ )+ # ... and r2's children are untouched (the WHERE research_id+ # filter must isolate the cascade to the named row).+ for model in (ResearchLog, ResearchResource, ResearchStrategy):+ assert _count_for(model, "r2") == 1, (+ f"{model.__name__} rows for r2 must survive deleting r1"+ )+++def _populate_download_family(session, research_id, url):+ """Insert a downloaded-source row family for one research.++ Mirrors production: a ``ResearchResource`` with a downloaded file+ gains ``download_tracker`` / ``download_duplicates`` /+ ``download_attempts`` rows (keyed by ``url_hash`` /+ ``first_resource_id`` / ``resource_id``) plus ``download_queue``+ and ``paper_appearances`` rows keyed by ``resource_id``. All of+ these reference ``research_resources.id`` with NO ACTION in live+ schemas, so they block the ``ResearchResource`` delete.+ """+ import hashlib++ from local_deep_research.database.models import (+ DownloadAttempt,+ DownloadDuplicates,+ DownloadQueue,+ DownloadTracker,+ Paper,+ PaperAppearance,+ ResearchResource,+ )++ url_hash = hashlib.sha256(url.encode()).hexdigest()+ resource = ResearchResource(+ research_id=research_id,+ title="r",+ url=url,+ source_type="web",+ created_at=_NOW,+ )+ session.add(resource)+ session.flush()++ session.add(+ DownloadTracker(+ url=url,+ url_hash=url_hash,+ first_resource_id=resource.id,+ is_downloaded=True,+ )+ )+ session.flush()+ session.add(+ DownloadDuplicates(+ url_hash=url_hash,+ resource_id=resource.id,+ research_id=research_id,+ )+ )+ session.add(+ DownloadAttempt(+ url_hash=url_hash,+ attempt_number=1,+ succeeded=True,+ )+ )+ session.add(+ DownloadQueue(+ resource_id=resource.id,+ research_id=research_id,+ )+ )+ paper = Paper(doi="10.1234/" + research_id)+ session.add(paper)+ session.flush()+ session.add(+ PaperAppearance(+ paper_id=paper.id,+ resource_id=resource.id,+ )+ )+ session.commit()+ return resource+++class TestDepth2ResourceChildCascade:+ """Depth-2 rows under ``research_resources`` must not block delete.++ ``download_tracker.first_resource_id``,+ ``download_duplicates.resource_id``, ``download_queue.resource_id``+ and ``paper_appearances.resource_id`` reference+ ``research_resources.id`` with NO ACTION in live schemas (the+ defect pinned by+ ``test_research_delete_cascade_is_blocked_by_no_action_parents``).+ Production 2026-09-23: deleting a research with downloaded sources+ raised ``FOREIGN KEY constraint failed`` from inside+ ``_delete_research_with_dependents`` even with PR #6716 applied,+ because the helper stopped at the first level.+ """++ def test_helper_removes_download_family_with_fk_on(self, db_with_fk):+ from local_deep_research.database.models import (+ DownloadAttempt,+ DownloadDuplicates,+ DownloadQueue,+ DownloadTracker,+ PaperAppearance,+ ResearchHistory,+ ResearchResource,+ )+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_download_family(+ db_with_fk, "r1", "https://example.com/r1.pdf"+ )++ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ db_with_fk.commit()+ db_with_fk.expire_all()++ assert db_with_fk.get(ResearchHistory, "r1") is None+ assert db_with_fk.query(ResearchResource).count() == 0+ assert db_with_fk.query(DownloadTracker).count() == 0+ assert db_with_fk.query(DownloadDuplicates).count() == 0+ assert db_with_fk.query(DownloadAttempt).count() == 0+ assert db_with_fk.query(DownloadQueue).count() == 0+ assert db_with_fk.query(PaperAppearance).count() == 0++ def test_sibling_research_download_family_survives(self, db_with_fk):+ from local_deep_research.database.models import (+ DownloadAttempt,+ DownloadDuplicates,+ DownloadQueue,+ DownloadTracker,+ ResearchResource,+ )+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2")+ # Distinct URLs so the two families share no url_hash rows.+ _populate_download_family(+ db_with_fk, "r1", "https://example.com/r1.pdf"+ )+ _populate_download_family(+ db_with_fk, "r2", "https://example.com/r2.pdf"+ )++ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ db_with_fk.commit()+ db_with_fk.expire_all()++ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "r2")+ .count()+ == 1+ )+ assert db_with_fk.query(DownloadTracker).count() == 1+ assert db_with_fk.query(DownloadDuplicates).count() == 1+ assert db_with_fk.query(DownloadAttempt).count() == 1+ assert db_with_fk.query(DownloadQueue).count() == 1+++# ---------------------------------------------------------------------------+# DELETE /api/delete/{research_id} — FK-aware integration+# ---------------------------------------------------------------------------+++class TestDeleteResearchRouteWithFKEnforcement:+ """Pin the public route's behaviour against the same FK-enforcing+ schema that breaks the old bulk-delete path."""++ def test_delete_succeeds_when_child_tables_have_rows(+ self, authenticated_client, db_with_fk+ ):+ """Happy-path with a fresh ``create_all`` schema: every+ FK-referencing table has a row, ``PRAGMA foreign_keys = ON``+ is set, the route must answer 200 and remove the research+ AND its dependents. This passes both before and after the+ fix (a fresh schema carries every ``CASCADE`` rule), so it+ is NOT the bug discriminator — see+ ``TestRouteSurvivesPartialSchema`` for the test that returns+ 500 on the old bulk-delete path and 200 with the helper."""+ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")++ with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):+ response = authenticated_client.delete("/api/delete/r1")++ assert response.status_code == 200, response.text[:300]+ assert response.get_json()["status"] == "success"++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ assert db_with_fk.get(ResearchHistory, "r1") is None+ assert db_with_fk.query(ResearchLog).count() == 0+ assert db_with_fk.query(ResearchResource).count() == 0++ def test_delete_still_refuses_in_progress_with_fk_on(+ self, authenticated_client, db_with_fk+ ):+ """The early ``status == IN_PROGRESS`` guard has to survive the+ helper refactor. Without it, a worker that is mid-research can+ have its row removed from under it."""+ _add_research(db_with_fk, "live", status="in_progress")+ _populate_all_child_tables(db_with_fk, "live")++ with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):+ response = authenticated_client.delete("/api/delete/live")++ assert response.status_code == 400, response.text[:300]+ assert "in progress" in response.get_json()["message"].lower()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "live") is not None++ def test_delete_still_cleans_queue_state(+ self, authenticated_client, db_with_fk+ ):+ """``cleanup_queued_research_state`` is the second step of the+ original handler; it must still run with the research_id."""+ _add_research(db_with_fk, "r1")++ with (+ _patch_session(db_with_fk),+ patch(_LIFECYCLE_CLEANUP) as cleanup,+ ):+ response = authenticated_client.delete("/api/delete/r1")++ assert response.status_code == 200, response.text[:300]+ cleanup.assert_called_once()+ assert cleanup.call_args[0][1] == ["r1"]+++# ---------------------------------------------------------------------------+# POST /api/clear_history — FK-aware integration+# ---------------------------------------------------------------------------+++class TestClearHistoryRouteWithFKEnforcement:+ """Happy-path twin for ``clear_history`` with a fresh+ ``create_all`` schema. Like the delete happy-path above, this+ passes both before and after the fix — the discriminator is+ ``TestRouteSurvivesPartialSchema.test_clear_history_route_handles_partial_schema``."""++ def test_clear_history_succeeds_when_each_research_has_children(+ self, authenticated_client, db_with_fk+ ):+ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r2")++ with (+ _patch_session(db_with_fk),+ patch(f"{_RR}.get_active_research_ids", return_value=[]),+ patch(_LIFECYCLE_CLEANUP),+ ):+ response = authenticated_client.post("/api/clear_history")++ assert response.status_code == 200, response.text[:300]+ assert response.get_json()["status"] == "success"++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ assert db_with_fk.query(ResearchHistory).count() == 0+ assert db_with_fk.query(ResearchLog).count() == 0+ assert db_with_fk.query(ResearchResource).count() == 0+++# ---------------------------------------------------------------------------+# Partial-schema discriminator — the test the original+# ``test_delete_succeeds_when_child_tables_have_rows`` lacked. A+# fresh ``create_all`` schema has every CASCADE rule in place, so+# ``db_with_fk`` alone cannot reproduce the production fragility;+# the production shape would surface a row whose FK lacks+# ``ON DELETE CASCADE``. We rebuild the table that way below and+# prove the route still answers 200 (and main would 500).+# ---------------------------------------------------------------------------+++def _rebuild_research_strategies_without_cascade(session, *research_ids):+ """Drop and rebuild ``research_strategies`` without+ ``ON DELETE CASCADE``. Mirrors the shape of an out-of-repo /+ partial schema: the FK column is intact, but the deletion rule+ is not, so a bulk ``DELETE FROM research_history WHERE id = ?``+ raises ``FOREIGN KEY constraint failed`` (with ``PRAGMA+ foreign_keys = ON``) when the table holds a row pointing at+ that parent.++ Re-inserts one ``research_strategies`` row per ``research_id``+ so the partial schema is non-empty — the empty-table case is+ trivially safe and would not reproduce the production error.+ ``DROP TABLE`` wipes every pre-existing strategies row, so+ callers with several researches must pass all ids (the+ ``clear_history`` twin passes both ``r1`` and ``r2``)."""+ from sqlalchemy import text++ session.execute(text("DROP TABLE research_strategies"))+ session.execute(+ text(+ "CREATE TABLE research_strategies ("+ " id INTEGER PRIMARY KEY,"+ " research_id VARCHAR(36) NOT NULL,"+ " strategy_name VARCHAR(255) NOT NULL,"+ " created_at VARCHAR(50),"+ " FOREIGN KEY (research_id) "+ " REFERENCES research_history(id)"+ ")"+ )+ )+ for rid in research_ids:+ session.execute(+ text(+ "INSERT INTO research_strategies "+ "(research_id, strategy_name, created_at) "+ "VALUES (:rid, :name, :ts)"+ ),+ {+ "rid": rid,+ "name": "no-cascade-shape",+ "ts": _NOW.isoformat(),+ },+ )+ session.commit()+ # The ORM identity map still holds the pre-DROP ResearchStrategy+ # objects; expire so later ORM reads hit the rebuilt table.+ session.expire_all()++ # Guard the fixture shape itself: the rebuilt FK must really+ # lack CASCADE, otherwise the discriminator below is vacuous.+ fk_list = (+ session.execute(text("PRAGMA foreign_key_list(research_strategies)"))+ .mappings()+ .all()+ )+ assert fk_list, "rebuilt research_strategies lost its FK entirely"+ for fk_row in fk_list:+ # SQLite reports NO ACTION / RESTRICT when no explicit+ # ON DELETE CASCADE was declared; CASCADE would read 'CASCADE'.+ assert fk_row["on_delete"] != "CASCADE", (+ f"partial-schema fixture still carries CASCADE: {fk_row}"+ )+ # And FK enforcement must be ON, or the bulk delete below+ # succeeds silently (orphans) instead of raising.+ fk_state = session.execute(text("PRAGMA foreign_keys")).scalar()+ assert fk_state == 1, (+ "partial-schema discriminator requires PRAGMA foreign_keys = ON"+ )+++class TestRouteSurvivesPartialSchema:+ """Discriminator: rebuild a child table without ``ON DELETE+ CASCADE`` (with ``PRAGMA foreign_keys = ON``), then drive the+ real ``delete`` / ``clear_history`` routes.++ A fresh ``create_all`` schema carries every CASCADE rule, so the+ happy-path tests above return 200 both before and after the fix.+ Under this partial shape the old bulk-delete path raises+ ``IntegrityError`` (route returns 500) while the helper — which+ issues an explicit ``DELETE FROM research_strategies WHERE+ research_id = ?`` — returns 200 with rows gone. Reverting the+ route to the bulk delete therefore fails these tests on+ behavior (500 vs 200), not on an import error."""++ def test_delete_research_returns_500_on_main_with_partial_schema(+ self, db_with_fk+ ):+ """Control: the old bulk-delete statement raises+ ``IntegrityError`` under the partial-schema shape, so the+ route tests' 200 is the helper doing the work, not luck.+ Takes no ``authenticated_client`` fixture on purpose — it+ never touches HTTP, and skipping the register flow keeps+ this control from adding flake to the suite."""+ from sqlalchemy.exc import IntegrityError++ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")+ _rebuild_research_strategies_without_cascade(db_with_fk, "r1")++ # Reproduce the exact pre-PR statement (single bulk parent+ # DELETE, no explicit child cleanup). If this stops raising,+ # the fixture lost its no-CASCADE shape and the route tests+ # below are vacuous again.+ from local_deep_research.database.models import ResearchHistory++ with pytest.raises(IntegrityError) as excinfo:+ db_with_fk.query(ResearchHistory).filter(+ ResearchHistory.id == "r1"+ ).delete(synchronize_session=False)+ db_with_fk.commit()+ assert "FOREIGN KEY" in str(excinfo.value).upper()++ def test_delete_research_route_handles_partial_schema(+ self, authenticated_client, db_with_fk+ ):+ """``DELETE /api/delete/{id}`` under the partial shape must+ answer 200 with parent and strategies rows gone. Reverting+ the route to the bulk delete returns 500 here."""+ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")+ _rebuild_research_strategies_without_cascade(db_with_fk, "r1")++ with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):+ response = authenticated_client.delete("/api/delete/r1")++ assert response.status_code == 200, response.text[:300]+ assert response.get_json()["status"] == "success"++ from sqlalchemy import text++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchStrategy,+ )++ assert db_with_fk.get(ResearchHistory, "r1") is None+ assert db_with_fk.query(ResearchStrategy).count() == 0+ # Raw-table check: the rebuilt (non-ORM) table is empty too —+ # guards against the ORM query hitting a stale identity map+ # while the raw row survives.+ raw_strategies = db_with_fk.execute(+ text("SELECT COUNT(*) FROM research_strategies")+ ).scalar()+ assert raw_strategies == 0++ def test_clear_history_route_handles_partial_schema(+ self, authenticated_client, db_with_fk+ ):+ """``POST /api/clear_history`` twin: both researches carry a+ no-CASCADE strategies row (the rebuild wipes the table, so+ both ids are re-inserted). Reverting to the bulk delete+ returns 500 here."""+ from sqlalchemy import text++ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r2")+ _rebuild_research_strategies_without_cascade(db_with_fk, "r1", "r2")++ # Sanity: both partial rows exist before the route runs —+ # otherwise the twin only discriminates on one row.+ pre_count = db_with_fk.execute(+ text("SELECT COUNT(*) FROM research_strategies")+ ).scalar()+ assert pre_count == 2, (+ f"partial fixture must hold 2 strategies rows, found {pre_count}"+ )++ with (+ _patch_session(db_with_fk),+ patch(f"{_RR}.get_active_research_ids", return_value=[]),+ patch(_LIFECYCLE_CLEANUP),+ ):+ response = authenticated_client.post("/api/clear_history")++ assert response.status_code == 200, response.text[:300]+ assert response.get_json()["status"] == "success"++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchStrategy,+ )++ assert db_with_fk.query(ResearchHistory).count() == 0+ assert db_with_fk.query(ResearchStrategy).count() == 0+ raw_strategies = db_with_fk.execute(+ text("SELECT COUNT(*) FROM research_strategies")+ ).scalar()+ assert raw_strategies == 0+++# ---------------------------------------------------------------------------+# Atomic guard — the #5074 predicates must run INSIDE the parent+# DELETE so a concurrent claim between the route's pre-check and+# the helper's delete still wins the race.+# ---------------------------------------------------------------------------+++def _add_queued_claim(session, research_id):+ """Insert a ``QueuedResearch`` row that the helper must observe+ inside the parent DELETE's WHERE clause. Mirrors the worker+ claim that #5074 was written to handle."""+ from local_deep_research.database.models import QueuedResearch++ session.add(+ QueuedResearch(+ username="claim-user",+ research_id=research_id,+ query="q",+ mode="quick",+ position=1,+ is_processing=True,+ created_at=_NOW,+ )+ )+ session.commit()+++def _flip_status_in_progress(session, research_id):+ """Flip a row's status to ``IN_PROGRESS`` between the+ pre-check and the helper call. Matches what a worker thread+ would commit at the top of its run."""+ from sqlalchemy import text++ session.execute(+ text(+ "UPDATE research_history SET status = 'in_progress' WHERE id = :rid"+ ),+ {"rid": research_id},+ )+ session.commit()+++class TestHelperAtomicGuard:+ """The pre-check on the route is a fast-path only; the+ authoritative predicate is the ``WHERE`` clause of the helper's+ parent DELETE. These tests prove a worker that races the+ route — by flipping the row's status to ``IN_PROGRESS`` or by+ claiming its queue entry between the route's snapshot and the+ helper's call — still preserves the row.++ The implementation technique: we don't need a real thread.+ The helper's parent DELETE is a SQL statement; we just need+ the row to BE in the protected state at the moment the+ statement runs. For the "race" simulation we issue a+ ``session.execute(UPDATE research_history SET status =+ 'in_progress')`` *first* and then call the helper — that is+ exactly the DB-level observation the helper must respect.+ """++ def test_helper_preserves_row_already_in_progress(self, db_with_fk):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "live", status="in_progress")+ _populate_all_child_tables(db_with_fk, "live")++ assert _delete_research_with_dependents(db_with_fk, "live") is False, (+ "helper must refuse IN_PROGRESS rows; the route's "+ "pre-check is a fast-path, this is the authoritative guard"+ )+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "live") is not None++ def test_helper_preserves_row_with_claimed_queue_entry(self, db_with_fk):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "claimed")+ _populate_all_child_tables(db_with_fk, "claimed")+ _add_queued_claim(db_with_fk, "claimed")++ assert (+ _delete_research_with_dependents(db_with_fk, "claimed") is False+ ), "helper must refuse rows whose queue entry is claimed"+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "claimed") is not None++ def test_helper_opt_out_drops_status_guard_for_chat_service(+ self, db_with_fk+ ):+ """The chat service's per-attempt teardown calls the+ helper after a liveness check has already confirmed the+ worker thread is dead; an ``IN_PROGRESS`` row in the DB+ (stale, worker is gone) must still be deletable. The+ helper exposes a ``require_status_not_in_progress=False``+ opt-out for that call site."""+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "stale", status="in_progress")+ _populate_all_child_tables(db_with_fk, "stale")++ assert (+ _delete_research_with_dependents(+ db_with_fk,+ "stale",+ require_status_not_in_progress=False,+ require_no_claimed_queue_row=False,+ )+ is True+ )+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "stale") is None++ def test_helper_opt_out_drops_claim_guard(self, db_with_fk):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")+ _add_queued_claim(db_with_fk, "r1")++ assert (+ _delete_research_with_dependents(+ db_with_fk,+ "r1",+ require_no_claimed_queue_row=False,+ )+ is True+ )+ db_with_fk.commit()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "r1") is None+++class TestHelperGuardRollbackAtomicity:+ """Guard refusal must leave parent AND children untouched.++ Regression for the final-review finding at ``f8bf804``: the helper+ deleted children in Phases B/C *before* the Phase-D parent guard,+ so a ``False`` return still persisted child deletions when the+ caller committed (``clear_history`` commits siblings in the same+ transaction). The helper now wraps Phases B–D in a SAVEPOINT and+ rolls back on a zero-row parent DELETE.+ """++ def _assert_all_children_present(self, session, research_id):+ from local_deep_research.database.models import (+ ChatMessage,+ ChatProgressStep,+ Document,+ NoteReference,+ NoteResearch,+ ResearchLog,+ ResearchResource,+ ResearchStrategy,+ )++ assert (+ session.query(ResearchLog)+ .filter(ResearchLog.research_id == research_id)+ .count()+ == 1+ ), "ResearchLog child must survive guard refusal"+ assert (+ session.query(ResearchResource)+ .filter(ResearchResource.research_id == research_id)+ .count()+ == 1+ ), "ResearchResource child must survive guard refusal"+ assert (+ session.query(ResearchStrategy)+ .filter(ResearchStrategy.research_id == research_id)+ .count()+ == 1+ ), "ResearchStrategy child must survive guard refusal"+ assert (+ session.query(ChatProgressStep)+ .filter(ChatProgressStep.research_id == research_id)+ .count()+ == 1+ ), "ChatProgressStep child must survive guard refusal"+ assert (+ session.query(NoteResearch)+ .filter(NoteResearch.research_id == research_id)+ .count()+ == 1+ ), "NoteResearch child must survive guard refusal"+ assert (+ session.query(NoteReference)+ .filter(NoteReference.target_research_id == research_id)+ .count()+ == 1+ ), "NoteReference child must survive guard refusal"+ assert (+ session.query(Document)+ .filter(Document.research_id == research_id)+ .count()+ == 1+ ), "Document child must survive guard refusal"+ # SET NULL edge rolls back too: the link must still be set.+ msg = (+ session.query(ChatMessage)+ .filter(ChatMessage.id == "cm-" + research_id)+ .one_or_none()+ )+ assert msg is not None, "chat message row must survive guard refusal"+ assert msg.research_id == research_id, (+ "chat_messages.research_id must NOT be nulled when the guard "+ "refuses the delete"+ )++ def test_helper_rolls_back_children_when_status_guard_blocks(+ self, db_with_fk+ ):+ from local_deep_research.database.models import ResearchHistory+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "live", status="in_progress")+ _populate_all_child_tables(db_with_fk, "live")++ assert _delete_research_with_dependents(db_with_fk, "live") is False+ db_with_fk.commit()+ db_with_fk.expire_all()++ assert db_with_fk.get(ResearchHistory, "live") is not None+ self._assert_all_children_present(db_with_fk, "live")++ def test_helper_rolls_back_children_when_claim_guard_blocks(+ self, db_with_fk+ ):+ from local_deep_research.database.models import ResearchHistory+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "claimed")+ _populate_all_child_tables(db_with_fk, "claimed")+ _add_queued_claim(db_with_fk, "claimed")++ assert _delete_research_with_dependents(db_with_fk, "claimed") is False+ db_with_fk.commit()+ db_with_fk.expire_all()++ assert db_with_fk.get(ResearchHistory, "claimed") is not None+ self._assert_all_children_present(db_with_fk, "claimed")++ def test_clear_history_loop_preserves_protected_children(self, db_with_fk):+ """Mirror ``clear_history``'s multi-row loop in one transaction:+ one protected row must not lose children when a sibling commits."""+ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2", status="in_progress")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r2")++ deleted_ids = []+ for rid in ("r1", "r2"):+ if _delete_research_with_dependents(db_with_fk, rid):+ deleted_ids.append(rid)+ db_with_fk.commit()+ db_with_fk.expire_all()++ assert deleted_ids == ["r1"]+ assert db_with_fk.get(ResearchHistory, "r1") is None+ assert db_with_fk.get(ResearchHistory, "r2") is not None+ # Sibling delete lands ...+ assert (+ db_with_fk.query(ResearchLog)+ .filter(ResearchLog.research_id == "r1")+ .count()+ == 0+ )+ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "r1")+ .count()+ == 0+ )+ # ... while the protected row keeps parent AND children.+ self._assert_all_children_present(db_with_fk, "r2")+++class TestHelperCallerRollback:+ """Caller rollback must restore parent, children and preserved links.++ Regression for the standalone-savepoint finding: in legacy+ transaction mode (production SQLCipher/SQLite with+ ``isolation_level=""``) SELECTs do not open a database+ transaction, so ``begin_nested()`` issued after a SELECT-only+ Phase A opened a standalone SAVEPOINT whose RELEASE committed+ immediately. A later ``db_session.rollback()`` by the caller then+ restored nothing and ``clear_history`` ended up partly applied+ after a later failure. The helper now forces a DEFERRED ``BEGIN``+ first (see ``_ensure_outer_db_transaction``), so RELEASE stays+ nested and the caller still owns commit/rollback.+ """++ def _assert_restored_from_fresh_session(self, session, research_id):+ """Re-read parent, children and SET NULL links from a new+ Session on the same engine — guards against the caller's+ identity map masking a persisted delete."""+ from sqlalchemy.orm import sessionmaker++ engine = session.get_bind()+ fresh = sessionmaker(bind=engine)()+ try:+ from local_deep_research.database.models import (+ ChatMessage,+ ChatProgressStep,+ Document,+ NoteReference,+ NoteResearch,+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ ResearchStrategy,+ )++ assert fresh.get(ResearchHistory, research_id) is not None, (+ "parent must survive caller rollback"+ )+ for model, column in (+ (ResearchLog, "research_id"),+ (ResearchResource, "research_id"),+ (ResearchStrategy, "research_id"),+ (ChatProgressStep, "research_id"),+ (NoteResearch, "research_id"),+ (Document, "research_id"),+ ):+ assert (+ fresh.query(model)+ .filter(getattr(model, column) == research_id)+ .count()+ == 1+ ), f"{model.__name__} child must survive caller rollback"+ assert (+ fresh.query(NoteReference)+ .filter(NoteReference.target_research_id == research_id)+ .count()+ == 1+ ), "NoteReference child must survive caller rollback"+ msg = fresh.get(ChatMessage, "cm-" + research_id)+ assert msg is not None, "chat message row must survive rollback"+ assert msg.research_id == research_id, (+ "chat_messages.research_id must still be linked after "+ "caller rollback (SET NULL step must roll back too)"+ )+ finally:+ fresh.close()++ def test_helper_success_is_rollbackable_by_caller(self, db_with_fk):+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r1")++ # Caller pattern is SELECTs then helper (no prior writes) —+ # exactly the state that produced a standalone SAVEPOINT.+ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ db_with_fk.rollback()+ db_with_fk.expire_all()++ from local_deep_research.database.models import ResearchHistory++ assert db_with_fk.get(ResearchHistory, "r1") is not None+ self._assert_restored_from_fresh_session(db_with_fk, "r1")++ def test_multi_row_clear_rollback_on_later_failure(self, db_with_fk):+ """Mirror ``clear_history``: two successful helper deletes in+ one outer transaction, then a later failure before commit must+ restore both researches fully."""+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ _add_research(db_with_fk, "r1")+ _add_research(db_with_fk, "r2")+ _populate_all_child_tables(db_with_fk, "r1")+ _populate_all_child_tables(db_with_fk, "r2")++ assert _delete_research_with_dependents(db_with_fk, "r1") is True+ assert _delete_research_with_dependents(db_with_fk, "r2") is True+ # Later failure (queue cleanup, file unlink, second-loop+ # exception) before the route's commit — caller rolls back.+ db_with_fk.rollback()+ db_with_fk.expire_all()++ self._assert_restored_from_fresh_session(db_with_fk, "r1")+ self._assert_restored_from_fresh_session(db_with_fk, "r2")+++class TestRouteAtomicGuardUnderRace:+ """End-to-end version of the atomic-guard invariant: the route+ must observe a row the worker just claimed between the+ pre-check and the helper's call. The pre-check passes because+ the row is ``completed`` and unclaimed at the moment of the+ read; the race is a real DB write that lands before the+ helper's parent DELETE.++ The race is simulated by patching the helper with a wrapper+ that flips the row's status to ``in_progress`` (or claims its+ queue entry) before delegating to the real helper. This+ models exactly the worker that wins the race: it commits+ *between* the route's pre-check and the helper's parent+ DELETE, so the row exists in the protected state when the+ helper's ``WHERE`` clause runs.+ """++ def _wrap_helper_with_race(self, prep):+ """Wrap the real helper with a side-effect ``prep`` that+ mutates the DB between the route's pre-check and the+ helper's parent DELETE. ``prep`` takes the session and+ the research_id."""+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents as real_helper,+ )++ def helper_with_race(db_session, research_id, **kwargs):+ prep(db_session, research_id)+ return real_helper(db_session, research_id, **kwargs)++ return helper_with_race++ def test_delete_research_preserves_row_when_status_flips_mid_call(+ self, authenticated_client, db_with_fk+ ):+ _add_research(db_with_fk, "race")+ _populate_all_child_tables(db_with_fk, "race")++ wrapped = self._wrap_helper_with_race(_flip_status_in_progress)++ with (+ _patch_session(db_with_fk),+ patch(_LIFECYCLE_CLEANUP),+ patch(f"{_RR}._delete_research_with_dependents", wrapped),+ ):+ response = authenticated_client.delete("/api/delete/race")++ # The pre-check happened *before* the wrapper ran (the+ # row was ``completed`` then). The wrapper flipped the+ # status, the helper's parent DELETE observed the flip+ # via its WHERE clause, and the row survived. The+ # post-check (db_session.get(...) is not None) emits+ # the 400 — preserving the observable contract for+ # callers.+ assert response.status_code == 400, response.text[:300]+ assert "in progress" in response.get_json()["message"].lower()++ from local_deep_research.database.models import (+ ChatMessage,+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ assert db_with_fk.get(ResearchHistory, "race") is not None+ # The mid-call flip must not cost the row's children: the+ # helper's SAVEPOINT rolls back Phases B/C with the parent.+ db_with_fk.expire_all()+ assert (+ db_with_fk.query(ResearchLog)+ .filter(ResearchLog.research_id == "race")+ .count()+ == 1+ )+ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "race")+ .count()+ == 1+ )+ msg = db_with_fk.get(ChatMessage, "cm-race")+ assert msg is not None and msg.research_id == "race"++ def test_delete_research_preserves_row_when_queue_claim_lands_mid_call(+ self, authenticated_client, db_with_fk+ ):+ """Same shape as the status-flip test, except the+ race is a ``QueuedResearch`` claim landing between the+ pre-check and the helper's parent DELETE. The pre-check+ sees no claim (false), the wrapper inserts the claim+ before the helper runs, and the helper's+ ``~claimed_queue_row`` predicate preserves the row."""++ def claim_prep(session, research_id):+ _add_queued_claim(session, research_id)++ _add_research(db_with_fk, "race-claim")+ _populate_all_child_tables(db_with_fk, "race-claim")++ wrapped = self._wrap_helper_with_race(claim_prep)++ with (+ _patch_session(db_with_fk),+ patch(_LIFECYCLE_CLEANUP),+ patch(f"{_RR}._delete_research_with_dependents", wrapped),+ ):+ response = authenticated_client.delete("/api/delete/race-claim")++ assert response.status_code == 400, response.text[:300]+ assert "in progress" in response.get_json()["message"].lower()++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ assert db_with_fk.get(ResearchHistory, "race-claim") is not None+ db_with_fk.expire_all()+ assert (+ db_with_fk.query(ResearchLog)+ .filter(ResearchLog.research_id == "race-claim")+ .count()+ == 1+ )+ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "race-claim")+ .count()+ == 1+ )++ def test_clear_history_route_preserves_protected_children(+ self, authenticated_client, db_with_fk+ ):+ """``clear_history`` commits siblings in one transaction: a+ protected row skipped by the helper must keep its children."""+ _add_research(db_with_fk, "keep", status="in_progress")+ _add_research(db_with_fk, "gone")+ _populate_all_child_tables(db_with_fk, "keep")+ _populate_all_child_tables(db_with_fk, "gone")++ with (+ _patch_session(db_with_fk),+ patch(f"{_RR}.get_active_research_ids", return_value=[]),+ patch(_LIFECYCLE_CLEANUP),+ ):+ response = authenticated_client.post("/api/clear_history")++ assert response.status_code == 200, response.text[:300]++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ db_with_fk.expire_all()+ assert db_with_fk.get(ResearchHistory, "gone") is None+ assert db_with_fk.get(ResearchHistory, "keep") is not None+ assert (+ db_with_fk.query(ResearchLog)+ .filter(ResearchLog.research_id == "keep")+ .count()+ == 1+ )+ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "keep")+ .count()+ == 1+ )++ def test_clear_history_race_preserves_claimed_children(+ self, authenticated_client, db_with_fk+ ):+ """``clear_history`` race: a queue claim landing between the+ per-row ``is_claimed`` pre-check and the helper's parent DELETE+ must preserve parent AND children, even though the outer+ transaction commits a sibling delete."""+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents as real_helper,+ )++ _add_research(db_with_fk, "gone")+ _add_research(db_with_fk, "raced")+ _populate_all_child_tables(db_with_fk, "gone")+ _populate_all_child_tables(db_with_fk, "raced")++ def helper_with_claim_race(db_session, research_id, **kwargs):+ if research_id == "raced":+ _add_queued_claim(db_session, research_id)+ return real_helper(db_session, research_id, **kwargs)++ with (+ _patch_session(db_with_fk),+ patch(f"{_RR}.get_active_research_ids", return_value=[]),+ patch(_LIFECYCLE_CLEANUP),+ patch(+ f"{_RR}._delete_research_with_dependents",+ helper_with_claim_race,+ ),+ ):+ response = authenticated_client.post("/api/clear_history")++ assert response.status_code == 200, response.text[:300]++ from local_deep_research.database.models import (+ ResearchHistory,+ ResearchLog,+ ResearchResource,+ )++ db_with_fk.expire_all()+ assert db_with_fk.get(ResearchHistory, "gone") is None+ assert db_with_fk.get(ResearchHistory, "raced") is not None+ assert (+ db_with_fk.query(ResearchLog)+ .filter(ResearchLog.research_id == "raced")+ .count()+ == 1+ )+ assert (+ db_with_fk.query(ResearchResource)+ .filter(ResearchResource.research_id == "raced")+ .count()+ == 1+ )+++# ---------------------------------------------------------------------------+# FK-introspection coverage — walks ``Base.metadata`` and fails+# CI when a new FK on ``research_history.id`` is added without+# the helper knowing about it. Closes the+# "next child table orphans silently" class of regressions.+# ---------------------------------------------------------------------------+++class TestHelperCoverageBaseMetadata:+ """Every FK edge in the ``research_history`` descendant closure+ must be classified by the cascade planner — no silent orphans or+ runtime ``IntegrityError`` when the next child table lands.++ The planner derives its steps from ``Base.metadata`` (any depth),+ so plain data tables need no code change. This test pins the+ other direction: the survival policy (``_CASCADE_SET_NULL``) must+ stay in sync with the schema, every depth-1 FK must be planned,+ and the delete order must respect child-before-parent for every+ planned delete edge. Walking ``Base.metadata`` (not a+ hand-mirrored fixture) means a new FK, a renamed column, or a+ changed ``ondelete`` action fails CI instead of breaking deletes+ in production."""++ def _closure_edges(self):+ """All ``(child, column, parent, parent_column)`` FK edges+ transitively reachable from ``research_history``."""+ from local_deep_research.database.models import Base++ children_of: dict = {}+ for table in Base.metadata.tables.values():+ for fk in table.foreign_keys:+ children_of.setdefault(fk.column.table.name, []).append(+ (table.name, fk.parent.name, fk.column.name)+ )+ edges = []+ seen = set()+ queue = ["research_history"]+ while queue:+ parent = queue.pop(0)+ for child, column, parent_column in children_of.get(parent, []):+ key = (child, column, parent, parent_column)+ if key in seen:+ continue+ seen.add(key)+ edges.append(key)+ queue.append(child)+ return edges++ def test_every_fk_on_research_history_id_is_covered(self):+ from local_deep_research.web.routers.research import (+ _CASCADE_SET_NULL,+ _research_cascade_plan,+ )++ set_null_steps, delete_steps, _table_to_class = _research_cascade_plan()+ planned = {(s[0], s[1]) for s in set_null_steps + delete_steps}++ # 1. Every depth-1 FK on research_history.id is planned+ # (deleted or nulled) — the original PR #6716 contract.+ depth1 = {+ (child, column)+ for child, column, parent, _ref in self._closure_edges()+ if parent == "research_history"+ }+ assert depth1, (+ "test scaffold found no FKs on research_history.id — "+ "the schema may have drifted"+ )+ assert depth1 <= planned, (+ f"unplanned depth-1 edges: {sorted(depth1 - planned)} — "+ "extend _CASCADE_SET_NULL or fix the planner walk"+ )++ # 2. The full closure is planned, at any depth — with one+ # principled exception: an edge whose parent table is never+ # deleted needs no step, since a row that survives cannot+ # block anything (e.g. ``note_synthesis_sources.synthesis_id``+ # targets ``note_syntheses``, which outlive the research). A+ # new table (or a new FK on an existing one) lands here+ # automatically as a DELETE unless it needs survival+ # semantics, in which case it needs a _CASCADE_SET_NULL+ # entry.+ closure = self._closure_edges()+ all_edges = {(child, column) for child, column, _p, _r in closure}+ delete_tables = {s[0] for s in delete_steps}+ unplanned = all_edges - planned+ for child, column in sorted(unplanned):+ parents = {+ parent+ for c, cc, parent, _r in closure+ if (c, cc) == (child, column)+ }+ assert parents and all(+ parent not in delete_tables and parent != "research_history"+ for parent in parents+ ), (+ f"edge {(child, column)} targets a deleted table "+ f"{sorted(parents)} but has no planned step — the "+ "helper would hit FOREIGN KEY constraint failed. "+ "Extend _CASCADE_SET_NULL or fix the planner walk."+ )++ # 3. No stale _CASCADE_SET_NULL entries: each must match a+ # real edge in the closure (a removed/renamed column without+ # a policy update fails here).+ assert _CASCADE_SET_NULL <= all_edges, (+ f"stale survival policy: {sorted(_CASCADE_SET_NULL - all_edges)}"+ )++ # 4. The two historical special-column shapes still exist as+ # planned: ``note_references.target_research_id`` is deleted,+ # ``chat_messages.research_id`` is nulled (rows survive).+ by_pair = {(s[0], s[1]): s for s in set_null_steps + delete_steps}+ assert by_pair[("note_references", "target_research_id")] in set(+ delete_steps+ )+ assert by_pair[("chat_messages", "research_id")] in set(set_null_steps)++ # 5. Delete order respects child-before-parent for every+ # planned delete edge (re-check the planner's topological+ # order independently of its implementation).+ order = [s[0] for s in delete_steps]+ position = {table: i for i, table in enumerate(order)}+ for child, _column, parent, _ref in delete_steps:+ if parent in position:+ assert position[child] <= position[parent], (+ f"{child} must be deleted before {parent}"+ )++ # And the helper itself must still be the public+ # symbol used by the routes.+ from local_deep_research.web.routers.research import (+ _delete_research_with_dependents,+ )++ assert callable(_delete_research_with_dependents)