fix(scheduler): fail closed on malformed runtime event preferences (#7440)

* 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>
This commit is contained in:
NanPan 2026-11-17 07:29:41 +08:00 committed by GitHub
parent dce29565ca
commit 4167e2b551
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 1 deletions

View File

@ -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.

View File

@ -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:

View File

@ -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 == []