From 0627cd031c3087e4efb63bcec2188db3903e4779 Mon Sep 17 00:00:00 2001 From: Alex Netsch Date: Wed, 9 Jul 2025 17:40:28 +0100 Subject: [PATCH] Perfect! I have successfully implemented the requested feature. Here's a summary of what I accomplished: (#113) ## Summary I added an environment variable `DISABLE_WORKTREE_ORPHAN_CLEANUP` to control worktree orphan cleanup behavior: 1. **Modified `execution_monitor.rs:252`** - Added a check at the beginning of the `cleanup_orphaned_worktrees` function that returns early if the environment variable is set, with a debug log message. 2. **Updated `package.json`** - Modified the `npm run dev` script to export `DISABLE_WORKTREE_ORPHAN_CLEANUP=1`, which disables orphan cleanup when running in development mode. 3. **Verified the implementation** - The backend builds successfully and the code correctly checks for the environment variable. The implementation defaults to cleanup being enabled (as requested), but when `npm run dev` is run, it automatically disables the orphan cleanup to prevent worktrees from being cleaned up during development. This allows developers to work with persistent worktrees while still maintaining the cleanup functionality in production environments. --- backend/src/execution_monitor.rs | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/src/execution_monitor.rs b/backend/src/execution_monitor.rs index 4e2e0a45..add1bd30 100644 --- a/backend/src/execution_monitor.rs +++ b/backend/src/execution_monitor.rs @@ -250,6 +250,11 @@ async fn check_externally_deleted_worktrees(pool: &sqlx::SqlitePool) { /// Find and delete orphaned worktrees that don't correspond to any task attempts async fn cleanup_orphaned_worktrees(pool: &sqlx::SqlitePool) { + // Check if orphan cleanup is disabled via environment variable + if std::env::var("DISABLE_WORKTREE_ORPHAN_CLEANUP").is_ok() { + tracing::debug!("Orphan worktree cleanup is disabled via DISABLE_WORKTREE_ORPHAN_CLEANUP environment variable"); + return; + } let worktree_base_dir = crate::models::task_attempt::TaskAttempt::get_worktree_base_dir(); // Check if base directory exists diff --git a/package.json b/package.json index ae4c3b8c..61a936eb 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.41", "private": true, "scripts": { - "dev": "export FRONTEND_PORT=$(node scripts/setup-dev-environment.js frontend) && export BACKEND_PORT=$(node scripts/setup-dev-environment.js backend) && concurrently \"npm run backend:dev:watch\" \"npm run frontend:dev\"", + "dev": "export FRONTEND_PORT=$(node scripts/setup-dev-environment.js frontend) && export BACKEND_PORT=$(node scripts/setup-dev-environment.js backend) && export DISABLE_WORKTREE_ORPHAN_CLEANUP=1 && concurrently \"npm run backend:dev:watch\" \"npm run frontend:dev\"", "build": "npm run frontend:build && npm run backend:build", "build:single": "npm run frontend:build && npm run backend:build:single", "build:npm": "./build-npm-package.sh",