From 94e5604e770d6589f786ea71e51738e21492f301 Mon Sep 17 00:00:00 2001 From: soryu Date: Wed, 21 Jan 2026 17:31:46 +0000 Subject: Add task branching feature (#15) --- makima/src/db/repository.rs | 54 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'makima/src/db/repository.rs') diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 536bc9b..7387735 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -654,8 +654,8 @@ pub async fn create_task(pool: &PgPool, req: CreateTaskRequest) -> Result Result Result Result 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