Background-task TTL cleanup: deletes terminal task
rows from mastra_background_tasks once their completedAt is older than the
TTL, mirroring core BackgroundTaskManager.cleanup() at the storage layer so
a purge cron can reap them WITHOUT a live manager — the same posture as
purgeExpiredWorkflowRuns/purgeExpiredThreads (raw D1 binding, failure-isolated
as a cron duty). Two windows, exactly as core: completed rows expire fast
(default 1h), failed/cancelled/timed_out slowly (default 24h) so a failure
stays inspectable.
completedAt is stored as ISO-8601 TEXT (toISOString()), so lexicographic
< against an ISO cutoff is a correct timestamp comparison — the same bet the
other purges take. completedAt IS NOT NULL is load-bearing: a row without a
completion stamp cannot be proven old, so it survives (fail safe). Live rows
(pending / running / suspended) are never matched — deleting a suspended task
mid-flight would strand its resume. A missing table reads as zero (background
tasks may never have run). Scheduling stays with the caller.
Background-task TTL cleanup: deletes terminal task rows from
mastra_background_tasksonce theircompletedAtis older than the TTL, mirroring coreBackgroundTaskManager.cleanup()at the storage layer so a purge cron can reap them WITHOUT a live manager — the same posture as purgeExpiredWorkflowRuns/purgeExpiredThreads (raw D1 binding, failure-isolated as a cron duty). Two windows, exactly as core: completed rows expire fast (default 1h), failed/cancelled/timed_out slowly (default 24h) so a failure stays inspectable.completedAtis stored as ISO-8601 TEXT (toISOString()), so lexicographic<against an ISO cutoff is a correct timestamp comparison — the same bet the other purges take.completedAt IS NOT NULLis load-bearing: a row without a completion stamp cannot be proven old, so it survives (fail safe). Live rows (pending / running / suspended) are never matched — deleting a suspended task mid-flight would strand its resume. A missing table reads as zero (background tasks may never have run). Scheduling stays with the caller.