diff options
| author | soryu <soryu@soryu.co> | 2026-01-21 16:22:56 +0000 |
|---|---|---|
| committer | soryu <soryu@soryu.co> | 2026-01-21 16:22:56 +0000 |
| commit | 0fe52039768b8f6bb60d15fb87b6d330de66d15f (patch) | |
| tree | 6e460d2fdcf3fb735d84d6ea170e5b6809b87e93 /makima/src/db | |
| parent | 9e724358438e7a6da69657260a218aedd76f1911 (diff) | |
| download | soryu-0fe52039768b8f6bb60d15fb87b6d330de66d15f.tar.gz soryu-0fe52039768b8f6bb60d15fb87b6d330de66d15f.zip | |
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 <noreply@anthropic.com>
Diffstat (limited to 'makima/src/db')
| -rw-r--r-- | makima/src/db/repository.rs | 28 |
1 files changed, 28 insertions, 0 deletions
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<Option<SupervisorState>, 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<i64, sqlx::Error> { + 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) +} |
