From 2b59a3082000842d980f9e19931949dbe5011229 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 30 Oct 2026 12:25:20 +0100 Subject: fix(sender): ignore inconclusive empty scans Treat scans with no successfully processed source pages as inconclusive so a temporary empty or partially rebuilt output directory cannot mark every source deleted or trigger removal Webmentions. Defer pending sends until at least one source is scanned successfully. --- tests/tasks/test_scanner.py | 134 +++++++++++++++++++++++++++++++++++---- webmentions_ssg/tasks/scanner.py | 15 ++++- 2 files changed, 137 insertions(+), 12 deletions(-) diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 4e6c818..3d138ac 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -858,6 +858,8 @@ def test_deleted_source_still_reserves_its_canonical_url( ) monkeypatch.setattr(scanner_module, "send_webmention", lambda identifier: None) + write_post(tmp_path, "unrelated", "
Other page remains published.
") + run_scan(app, scanner_module) path.unlink() run_scan(app, scanner_module) @@ -1235,6 +1237,116 @@ def test_removed_unsent_target_is_not_queued( assert queued == [] +def test_empty_output_directory_does_not_delete_sources_or_queue_removals( + app: Flask, + scanner_module: ModuleType, + tmp_path: Path, + monkeypatch: MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + configure_scanner(app, tmp_path) + + paths = [ + write_post(tmp_path, slug, f'Target') + for slug in ("first", "second") + ] + + queued: list[UUID] = [] + monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + run_scan(app, scanner_module) + + with app.app_context(): + for webmention in db.session.scalars(sa.select(SentWebmention)): + webmention.processed_revision = 1 + webmention.sent_revision = 1 + webmention.status = SentWebmentionStatus.SENT + db.session.commit() + + queued.clear() + for path in paths: + path.unlink() + + # Repeated empty scans must never turn a temporary rebuild into deletions. + run_scan(app, scanner_module) + run_scan(app, scanner_module) + + with app.app_context(): + sources = list(db.session.scalars(sa.select(Source))) + webmentions = list(db.session.scalars(sa.select(SentWebmention))) + + assert len(sources) == 2 + assert all(source.revision == 1 for source in sources) + assert all(source.deleted_at is None for source in sources) + assert len(webmentions) == 2 + assert all(webmention.active for webmention in webmentions) + assert all(webmention.desired_revision == 1 for webmention in webmentions) + + assert queued == [] + assert "No source documents successfully scanned" in caplog.text + + # Once the SSG rebuild completes, unchanged sources stay on revision 1. + write_post(tmp_path, "first", 'Target') + write_post(tmp_path, "second", 'Target') + run_scan(app, scanner_module) + + with app.app_context(): + sources = list(db.session.scalars(sa.select(Source))) + assert all(source.revision == 1 for source in sources) + assert all(source.deleted_at is None for source in sources) + + assert queued == [] + + +def test_no_successful_pages_defers_pending_webmentions( + app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch +) -> None: + configure_scanner(app, tmp_path) + path = write_post( + tmp_path, "example", 'Target' + ) + queued: list[UUID] = [] + monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + + run_scan(app, scanner_module) + assert len(queued) == 1 + identifier = queued.pop() + + # With no valid pages, even previously pending sends must wait for recovery. + path.unlink() + run_scan(app, scanner_module) + assert queued == [] + + with app.app_context(): + source = db.session.scalar(sa.select(Source)) + webmention = db.session.scalar(sa.select(SentWebmention)) + assert source is not None and source.deleted_at is None + assert webmention is not None and webmention.desired_revision == 1 + + write_post(tmp_path, "example", 'Target') + run_scan(app, scanner_module) + assert queued == [identifier] + + +def test_failed_scan_does_not_delete_missing_source( + app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch +) -> None: + configure_scanner(app, tmp_path) + missing = write_post(tmp_path, "missing", "Original page.
") + invalid = write_post(tmp_path, "invalid", "Original page.
") + monkeypatch.setattr(scanner_module, "send_webmention", lambda identifier: None) + run_scan(app, scanner_module) + + missing.unlink() + invalid.write_text("Incomplete rebuild", encoding="utf-8") + run_scan(app, scanner_module) + + with app.app_context(): + sources = list(db.session.scalars(sa.select(Source))) + assert len(sources) == 2 + assert all(source.revision == 1 for source in sources) + assert all(source.deleted_at is None for source in sources) + + def test_deleted_source_queues_previously_sent_webmention( app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: @@ -1254,6 +1366,8 @@ def test_deleted_source_queues_previously_sent_webmention( monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + write_post(tmp_path, "other", "Other page remains published.
") + run_scan(app, scanner_module) with app.app_context(): @@ -1274,7 +1388,7 @@ def test_deleted_source_queues_previously_sent_webmention( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -1310,6 +1424,8 @@ def test_deleted_source_does_not_queue_unsent_webmention( monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + write_post(tmp_path, "other", "Other page remains published.
") + run_scan(app, scanner_module) queued.clear() @@ -1318,7 +1434,7 @@ def test_deleted_source_does_not_queue_unsent_webmention( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -1354,6 +1470,8 @@ def test_restored_source_creates_new_revision( monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + write_post(tmp_path, "other", "Other page remains published.
") + run_scan(app, scanner_module) with app.app_context(): @@ -1385,7 +1503,7 @@ def test_restored_source_creates_new_revision( run_scan(app, scanner_module) with app.app_context(): - source = db.session.scalar(sa.select(Source)) + source = db.session.scalar(sa.select(Source).where(Source.url == SOURCE_URL)) webmention = db.session.scalar(sa.select(SentWebmention)) assert source is not None @@ -1547,10 +1665,7 @@ def test_unexpected_parser_error_does_not_abort_other_sources( def test_unexpected_parser_error_does_not_delete_known_source( - app: Flask, - scanner_module: ModuleType, - tmp_path: Path, - monkeypatch: MonkeyPatch, + app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: configure_scanner(app, tmp_path) write_post(tmp_path, "bad", "Original bad page.
") @@ -1591,10 +1706,7 @@ def test_unexpected_parser_error_does_not_delete_known_source( def test_unexpected_persistence_error_is_not_suppressed( - app: Flask, - scanner_module: ModuleType, - tmp_path: Path, - monkeypatch: MonkeyPatch, + app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: configure_scanner(app, tmp_path) write_post(tmp_path, "example", "Content
") diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index e8149ba..7e163f7 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -406,7 +406,9 @@ def scan_sources() -> None: Scan generated source documents and queue pending Webmentions. New and changed sources are persisted, missing sources are marked as deleted, - and Webmentions requiring processing are queued for sending. + and Webmentions requiring processing are queued for sending. A scan that + successfully processes no sources is treated as inconclusive: it neither + marks sources deleted nor queues Webmentions. :raises RuntimeError: If the configured source directory or base URL is invalid. @@ -498,6 +500,17 @@ def scan_sources() -> None: except SourceScanError as exc: current_app.logger.warning("Could not scan source %s: %s", index_path, exc) + # A static-site generator can temporarily empty its output directory while + # rebuilding. With no successfully scanned pages, absence is not evidence + # of deletion; do not generate removals or send pending Webmentions that + # might be verified while the site is unavailable. + if scanned_count == 0: + current_app.logger.warning( + "No source documents successfully scanned; " + "skipping deletion detection and Webmention sends" + ) + return + for path, source in sources_by_path.items(): if source.deleted_at is not None or path in seen_paths: continue -- cgit v1.3.1