From 0fe52039768b8f6bb60d15fb87b6d330de66d15f Mon Sep 17 00:00:00 2001 From: soryu Date: Wed, 21 Jan 2026 16:22:56 +0000 Subject: feat(db): Add cleanup_stale_anonymous_tasks function Add a database function to delete anonymous tasks (tasks with contract_id = NULL) that are in a terminal state (done, failed, merged) and older than a specified number of days. This helps prevent accumulation of orphaned tasks that were created for ad-hoc operations like task branching. Co-Authored-By: Claude Opus 4.5 --- makima/src/db/repository.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 33e48a4..7387735 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -3684,3 +3684,31 @@ pub async fn get_supervisor_conversation_full( ) -> Result, sqlx::Error> { get_supervisor_state(pool, contract_id).await } + +// ============================================================================= +// Anonymous Task Cleanup Functions +// ============================================================================= + +/// Delete stale anonymous tasks (tasks with contract_id = NULL) that: +/// - Are in a terminal state (done, failed, merged) +/// - Are older than the specified number of days +/// +/// Returns the number of deleted tasks. +pub async fn cleanup_stale_anonymous_tasks( + pool: &PgPool, + max_age_days: i32, +) -> Result { + let result = sqlx::query( + r#" + DELETE FROM tasks + WHERE contract_id IS NULL + AND status IN ('done', 'failed', 'merged') + AND created_at < NOW() - INTERVAL '1 day' * $1 + "#, + ) + .bind(max_age_days) + .execute(pool) + .await?; + + Ok(result.rows_affected() as i64) +} -- cgit v1.2.3