#18972: Fix #18880: Drop pending notifications when cron job is deleted
size: S
Cluster:
Cron Job Enhancements
## Problem
When a cron job is deleted, pending system-event notifications (enqueued with `contextKey: 'cron:<jobId>'`) remained in the in-memory queue and continued to be delivered on subsequent heartbeats. This caused 20+ duplicate completion notifications after job deletion.
## Solution
- Added `dropSystemEventsByContextKey()` to `system-events.ts` that removes all queued events matching a given contextKey across all session queues
- Called it from `remove()` in `cron/service/ops.ts` to purge stale notifications immediately when a job is deleted
- Added tests for the new function
Fixes #18880
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes duplicate notification issue by clearing pending system events when cron jobs are deleted. The implementation adds `dropSystemEventsByContextKey()` that removes all queued events matching a specific contextKey across all session queues, then calls it from the cron job removal function with the job's contextKey pattern (`cron:${id}`). Tests cover single-session and multi-session cleanup scenarios.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The implementation is straightforward and correct - it properly normalizes contextKeys for case-insensitive matching, handles edge cases (empty queues, no matches), and includes comprehensive tests. The fix is well-scoped to the specific problem without affecting other functionality.
- No files require special attention
<sub>Last reviewed commit: 149b474</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#3693: fix(cron): delete deleteAfterRun jobs regardless of execution status
by HirokiKobayashi-R · 2026-01-29
78.1%
#18960: fix: don't disable one-shot cron jobs on skipped status
by jwchmodx · 2026-02-17
75.9%
#15422: fix(auto-reply): keep cron systemEvent payloads that start with 'Re...
by liuxiaopai-ai · 2026-02-13
75.8%
#11657: fix(cron): treat skipped heartbeat as ok for one-shot jobs
by DukeDeSouth · 2026-02-08
75.5%
#3335: Fixes cron jobs
by hkirat · 2026-01-28
75.3%
#17823: fix: memory leak in cron isolated runs — agent-events Maps never cl...
by techboss · 2026-02-16
75.2%
#8698: fix(cron): default enabled to true for new jobs
by emmick4 · 2026-02-04
75.1%
#12303: fix(cron): correct nextRunAtMs calculation and prevent timer stall
by colddonkey · 2026-02-09
74.6%
#14667: fix: preserve missed cron runs when updating job schedule
by WalterSumbon · 2026-02-12
74.5%
#18144: fix(cron): clear stuck runningAtMs after timeout and add maintenanc...
by taw0002 · 2026-02-16
74.3%