From 4167e2b5511692bb17f9ed523c34c6f7b07332fd Mon Sep 17 00:00:00 2001 From: NanPan <111261006+poijygfdyy@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:29:41 +0800 Subject: [PATCH] fix(scheduler): fail closed on malformed runtime event preferences (#7440) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rebase notification preference fix onto upstream main * fix(runtime): publish a failed artifact delivery on the run stream (#8252) * fix(runtime): publish the delivery failure on the run stream * docs(api): note the delivery-failure stream event * test(runtime): cover the deferred delivery-failure frame * docs(runtime): log the delivery fix and explain the fallback exemption * test(e2e): present the recorded output in the replay scenario The write/read-file fixture predates the delivery check, so the replayed run has been ending as an error without presenting note.txt. This PR makes that failure visible on the stream, which broke the replay golden and the render e2e. Add the present_files step the current scenario produces and regenerate the golden. * docs: drop the CHANGELOG entry CONTRIBUTING.md asks contributors to leave CHANGELOG.md and CHANGELOG_zh.md to the maintainers at merge time, so the Unreleased ordering and the English/Chinese pair stay in sync. --------- Co-authored-by: 一片一片 <3037678834@qq.com> --- backend/AGENTS.md | 2 ++ .../app/scheduler/notification_delivery.py | 7 +++- .../test_notification_delivery_worker.py | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/backend/AGENTS.md b/backend/AGENTS.md index 1ec6e43a93..c3fbeeb4e7 100644 --- a/backend/AGENTS.md +++ b/backend/AGENTS.md @@ -61,3 +61,5 @@ default suite needs no network and no API key. [run interaction policy](docs/RUN_INTERACTION_POLICY.md#hitl-tool-approval). - **Add a schema change with `make migrate-rev MSG="..."`** and follow the migrations guide. + +- **Notification preferences are a send-time trust boundary.** Malformed provider or per-event resolver output must fail closed and remain retryable without consuming an attempt; never treat malformed data as opt-in. diff --git a/backend/app/scheduler/notification_delivery.py b/backend/app/scheduler/notification_delivery.py index 58e3a61de6..b9f2e11a81 100644 --- a/backend/app/scheduler/notification_delivery.py +++ b/backend/app/scheduler/notification_delivery.py @@ -289,11 +289,16 @@ class NotificationDeliveryWorker: event_prefs = self._resolve_notification_event_preferences(delivery.get("owner_user_id") or "") if inspect.isawaitable(event_prefs): event_prefs = await event_prefs + if not isinstance(event_prefs, dict): + raise ValueError("Invalid runtime event preference response") + event_enabled = event_prefs.get(event, False) + if type(event_enabled) is not bool: + raise ValueError("Invalid runtime event preference value") except Exception: logger.warning("Could not verify runtime event preference for delivery %s; parking it", delivery["id"], exc_info=True) await self._delivery_repo.mark_failed(delivery["id"], claim_token=delivery.get("claim_token"), error="could not verify notification preference", count_attempt=False) return False - if isinstance(event_prefs, dict) and not event_prefs.get(event, False): + if not event_enabled: await self._delivery_repo.mark_failed(delivery["id"], claim_token=delivery.get("claim_token"), error=DISABLED_BY_OWNER, count_attempt=False, terminal=True) return False elif preferences.get(event) is False: diff --git a/backend/tests/test_notification_delivery_worker.py b/backend/tests/test_notification_delivery_worker.py index d900bccdbe..93b6f91cc0 100644 --- a/backend/tests/test_notification_delivery_worker.py +++ b/backend/tests/test_notification_delivery_worker.py @@ -1187,3 +1187,36 @@ async def test_wecom_group_notification_for_owner_and_multi_group_binding_isolat assert len(ws_client.calls) == 2 finally: await close_engine() + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "event_preferences", + [ + pytest.param(None, id="none"), + pytest.param([], id="list"), + pytest.param("malformed", id="string"), + pytest.param({"run_completed": "false"}, id="string-value"), + pytest.param({"run_completed": 1}, id="integer-value"), + ], +) +async def test_runtime_event_preference_lookup_fails_closed_on_malformed_shape(event_preferences): + row = _delivery_row(event="run_completed") + row["source_kind"] = "run" + repo = FakeDeliveryRepo([row]) + channel = FakeChannel() + worker = NotificationDeliveryWorker( + delivery_repo=repo, + resolve_channel=lambda provider: channel, + resolve_notification_preferences=lambda owner: {"wecom": True}, + resolve_notification_event_preferences=lambda owner: event_preferences, + ) + + await worker.run_once(now=datetime.now(UTC)) + + # An invalid preference lookup must be retryable, never interpreted as opt-in. + assert repo.sent == [] + assert repo.failed == [("delivery-1", "could not verify notification preference", False)] + assert repo.terminal_failures == [] + assert channel.sent == [] + assert channel.owners == []