Skip to content

The agent that grows with you

fix(recovery): skip rows the destination schema rejects during partial salvage

Gunwoo Lee1dcc0b06e29b
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..3e5c997d180
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1,2 @@
+leegunwoo98
+# PR #91413 salvage (skip destination-rejected rows in partial recovery; #102240)
diff --git a/hermes_cli/session_recovery.py b/hermes_cli/session_recovery.py
index 73cf4645b1d..28fd11f208e 100644
--- a/hermes_cli/session_recovery.py
+++ b/hermes_cli/session_recovery.py
@@ -511,8 +511,15 @@ class _RowidRangeSalvage:
if not self._keep([value]):
result["excluded_rows"] += 1
return True
- with _immediate_transaction(self.destination):
- self.destination.execute(self.insert_sql, value)
+ try:
+ with _immediate_transaction(self.destination):
+ self.destination.execute(self.insert_sql, value)
+ except sqlite3.IntegrityError as exc:
+ # A phantom row from a damaged page (NULL in a NOT NULL column, FK to nothing) is rejected
+ # by the destination schema; report it as a skipped singleton instead of aborting the table.
+ result["destination_rejected_rows"] += 1
+ self._skip(rowid, rowid, f"destination constraint rejected row: {exc}")
+ return True
result["copied_rows"] += 1
result["exact_lookup_recovered"] += 1
return True
@@ -568,7 +575,8 @@ def _copy_table_salvage(
"""Best-effort rowid-range copy that continues past damaged source pages."""
result: dict[str, Any] = {
"mode": "rowid_range_salvage", "source_rows": source_rows, "copied_rows": 0, "excluded_rows": 0,
- "columns": [], "range_queries": 0, "exact_lookup_recovered": 0, "skipped_rowid_ranges": [],
+ "columns": [], "range_queries": 0, "exact_lookup_recovered": 0, "destination_rejected_rows": 0,
+ "skipped_rowid_ranges": [],
}
columns = _compatible_columns(source, destination, table, result)
if columns is None:
diff --git a/tests/hermes_cli/test_session_recovery.py b/tests/hermes_cli/test_session_recovery.py
index c9549018d74..899906de2c1 100644
--- a/tests/hermes_cli/test_session_recovery.py
+++ b/tests/hermes_cli/test_session_recovery.py
@@ -834,3 +834,45 @@ def test_lost_and_found_direct_copy_creates_lazy_delivery_ledger(tmp_path: Path)
lf_conn.close()
dest.close()
assert rows == [("ob-1", "pending", None), ("ob-2", "failed", "boom")]
+
+
+
+def test_partial_recovery_skips_phantom_row_rejected_by_destination_schema(
+ tmp_path: Path,
+) -> None:
+ """#102240: a phantom ``sessions`` row with NULL ``started_at`` must be reported as a skipped
+ singleton, not abort the whole ``--allow-partial`` run at the exact-lookup boundary."""
+ source = tmp_path / "phantom-state.db"
+ output = tmp_path / "phantom-recovered.db"
+ _make_source(source)
+
+ # Relax the source's NOT NULL in place (schema text only, the pages stay identical) so the
+ # source can hold a row the destination's canonical schema rejects.
+ with sqlite3.connect(str(source), isolation_level=None) as conn:
+ conn.execute("PRAGMA writable_schema=ON")
+ conn.execute(
+ "UPDATE sqlite_master SET sql = replace(sql, 'started_at REAL NOT NULL', 'started_at REAL') "
+ "WHERE type = 'table' AND name = 'sessions'"
+ )
+ version = conn.execute("PRAGMA schema_version").fetchone()[0]
+ conn.execute(f"PRAGMA schema_version={version + 1}")
+ conn.execute("PRAGMA writable_schema=OFF")
+ with sqlite3.connect(str(source), isolation_level=None) as conn:
+ conn.execute(
+ "INSERT INTO sessions (id, source, started_at, title) VALUES ('phantom', 'cli', NULL, 'Phantom')"
+ )
+ assert conn.execute("SELECT count(*) FROM sessions").fetchone()[0] == 4
+
+ report = recover_session_database(source, output, work_dir=tmp_path, chunk_size=16, allow_partial=True)
+
+ copied = report["copy"]["sessions"]
+ assert copied["status"] == "partial"
+ assert copied["copied_rows"] == 3
+ assert copied["destination_rejected_rows"] == 1
+ assert [item["error"] for item in copied["skipped_rowid_ranges"]] == [
+ "destination constraint rejected row: NOT NULL constraint failed: sessions.started_at",
+ ]
+ assert report["verified"] is True
+ with sqlite3.connect(str(output)) as conn:
+ assert conn.execute("SELECT count(*) FROM sessions WHERE id = 'phantom'").fetchone()[0] == 0
+ assert conn.execute("SELECT count(*) FROM messages").fetchone()[0] == 21