Skip to content

armgit / agtx

Public Imported from GitHub
0

🏄🏼‍♂️ The blackboard for coding agents - agentic development environment for claude code, codex, cursor, opencode, grok and more.

refactor: task cleanup (#70)

Fynn Flügged3903b54ed4d
diff --git a/README.md b/README.md
index baf8f9d..73ccc4c 100644
--- a/README.md
+++ b/README.md
@@ -177,19 +177,6 @@ cleanup_script = "scripts/cleanup_worktree.sh"
`base_branch` controls which branch new task worktrees are created from. If omitted or empty, agtx
auto-detects `main`, `master`, or falls back to the current branch.
-These options run during the Backlog → Research/Planning/Running transition, after worktree creation
-and before the agent session starts.
-
-When a task worktree is removed, `cleanup_script` runs first (with cwd set to the worktree root).
-If it exits non-zero, worktree removal is aborted and the error is logged (forced cleanup flows
-continue after logging). The script receives:
-
-- `AGTX_PROJECT_PATH`
-- `AGTX_WORKTREE_PATH`
-- `AGTX_TASK_ID`
-- `AGTX_TASK_SLUG`
-- `AGTX_TASK_BRANCH` (when available)
-
### Per-Phase Agent Configuration
By default, all phases use `default_agent`. You can override the agent for specific phases globally or per project:
diff --git a/src/git/worktree.rs b/src/git/worktree.rs
index 74f3138..4e85b9c 100644
--- a/src/git/worktree.rs
+++ b/src/git/worktree.rs
@@ -96,14 +96,14 @@ pub const AGENT_CONFIG_DIRS: &[&str] = &[
/// Output from a shell script run inside a worktree.
#[derive(Debug)]
-pub struct ScriptOutput {
+pub(crate) struct ScriptOutput {
pub status: std::process::ExitStatus,
pub stdout: String,
pub stderr: String,
}
/// Run a shell script inside a worktree, capturing stdout/stderr.
-fn run_worktree_script(
+pub(crate) fn run_worktree_script(
script: &str,
worktree_path: &Path,
envs: &[(String, String)],
@@ -123,15 +123,6 @@ fn run_worktree_script(
})
}
-/// Run a cleanup script inside the worktree, returning the captured output.
-pub fn run_cleanup_script(
- script: &str,
- worktree_path: &Path,
- envs: &[(String, String)],
-) -> Result<ScriptOutput> {
- run_worktree_script(script, worktree_path, envs)
-}
-
/// Initialize a worktree by copying agent config dirs, user-specified files, and running an init script.
///
/// Returns a Vec of warning messages for any issues encountered.
@@ -321,3 +312,30 @@ pub fn worktree_path(project_path: &Path, task_id: &str) -> PathBuf {
pub fn worktree_exists(project_path: &Path, task_id: &str) -> bool {
worktree_path(project_path, task_id).exists()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use tempfile::TempDir;
+
+ #[test]
+ fn test_run_worktree_script_captures_output_and_env() {
+ let temp_dir = TempDir::new().unwrap();
+ let envs = vec![("AGTX_TASK_ID".to_string(), "task-123".to_string())];
+
+ let output =
+ run_worktree_script("echo $AGTX_TASK_ID", temp_dir.path(), &envs).unwrap();
+
+ assert!(output.status.success());
+ assert_eq!(output.stdout.trim(), "task-123");
+ }
+
+ #[test]
+ fn test_run_worktree_script_nonzero_exit() {
+ let temp_dir = TempDir::new().unwrap();
+
+ let output = run_worktree_script("exit 42", temp_dir.path(), &[]).unwrap();
+
+ assert!(!output.status.success());
+ }
+}
diff --git a/src/tui/app.rs b/src/tui/app.rs
index b70f264..264b99b 100644
--- a/src/tui/app.rs
+++ b/src/tui/app.rs
@@ -2641,7 +2641,6 @@ impl App {
&project_path,
tmux_ops.as_ref(),
git_ops.as_ref(),
- true,
);
});
}
@@ -4041,8 +4040,7 @@ impl App {
project_path,
self.state.tmux_ops.as_ref(),
self.state.git_ops.as_ref(),
- false,
- )?;
+ );
db.delete_task(&task.id)?;
self.refresh_tasks()?;
}
@@ -4562,7 +4560,6 @@ impl App {
&project_path_clone,
tmux_ops.as_ref(),
git_ops.as_ref(),
- false,
);
});
Ok(false)
@@ -6104,81 +6101,27 @@ fn generate_task_slug(task_id: &str, title: &str) -> String {
format!("{}-{}", id_prefix, title_slug)
}
-fn build_cleanup_env(
- project_path: &Path,
- worktree_path: &Path,
- task_id: &str,
- branch_name: Option<&str>,
-) -> Vec<(String, String)> {
- let slug = branch_name
- .and_then(|b| b.strip_prefix("task/"))
- .unwrap_or(task_id);
-
- let mut envs = vec![
- (
- "AGTX_PROJECT_PATH".to_string(),
- project_path.to_string_lossy().to_string(),
- ),
- (
- "AGTX_WORKTREE_PATH".to_string(),
- worktree_path.to_string_lossy().to_string(),
- ),
- ("AGTX_TASK_ID".to_string(), task_id.to_string()),
- ("AGTX_TASK_SLUG".to_string(), slug.to_string()),
- ];
-
- if let Some(branch) = branch_name {
- envs.push(("AGTX_TASK_BRANCH".to_string(), branch.to_string()));
- }
-
- envs
-}
-
-fn run_cleanup_script_for_worktree(
- cleanup_script: Option<&str>,
- project_path: &Path,
- worktree_path: &Path,
- task_id: &str,
- branch_name: Option<&str>,
- force_cleanup: bool,
-) -> Result<()> {
+fn run_cleanup_script_for_worktree(cleanup_script: Option<&str>, worktree_path: &Path) {
let Some(script) = cleanup_script else {
- return Ok(());
+ return;
};
let script = script.trim();
if script.is_empty() {
- return Ok(());
+ return;
}
- let envs = build_cleanup_env(project_path, worktree_path, task_id, branch_name);
- let output = match git::run_cleanup_script(script, worktree_path, &envs) {
- Ok(result) => result,
- Err(e) => {
- if force_cleanup {
- eprintln!("cleanup_script failed to run (forced): {}", e);
- return Ok(());
+ match git::run_worktree_script(script, worktree_path, &[]) {
+ Err(e) => eprintln!("cleanup_script failed to run: {}", e),
+ Ok(output) => {
+ if !output.status.success() {
+ eprintln!(
+ "cleanup_script exited with {}: {}",
+ output.status,
+ output.stderr.trim()
+ );
}
- return Err(e);
- }
- };
-
- if !output.stdout.trim().is_empty() {
- eprintln!("cleanup_script stdout:\n{}", output.stdout.trim_end());
- }
- if !output.stderr.trim().is_empty() {
- eprintln!("cleanup_script stderr:\n{}", output.stderr.trim_end());
- }
-
- if !output.status.success() {
- let message = format!("cleanup_script exited with {}: {}", output.status, script);
- if force_cleanup {
- eprintln!("{}", message);
- return Ok(());
}
- anyhow::bail!(message);
}
-
- Ok(())
}
/// Cleanup task resources (tmux window, cleanup script, git worktree) and mark as done
@@ -6189,7 +6132,6 @@ fn cleanup_task_for_done(
project_path: &Path,
tmux_ops: &dyn TmuxOperations,
git_ops: &dyn GitOperations,
- force_cleanup: bool,
) {
// Archive artifacts before removing worktree
if let Some(worktree) = &task.worktree_path {
@@ -6218,18 +6160,8 @@ fn cleanup_task_for_done(
let _ = tmux_ops.kill_window(session_name);
}
if let Some(worktree) = &task.worktree_path {
- if let Err(e) = run_cleanup_script_for_worktree(
- cleanup_script,
- project_path,
- Path::new(worktree),
- &task.id,
- task.branch_name.as_deref(),
- force_cleanup,
- ) {
- eprintln!("cleanup_script failed: {}", e);
- } else {
- let _ = git_ops.remove_worktree(project_path, worktree);
- }
+ run_cleanup_script_for_worktree(cleanup_script, Path::new(worktree));
+ let _ = git_ops.remove_worktree(project_path, worktree);
}
// Keep the branch so task can be reopened later
task.session_name = None;
@@ -6249,7 +6181,6 @@ fn cleanup_task_resources(
project_path: &Path,
tmux_ops: &dyn TmuxOperations,
git_ops: &dyn GitOperations,
- force_cleanup: bool,
) {
// Archive artifacts before removing worktree
if let Some(worktree) = worktree_path {
@@ -6277,17 +6208,7 @@ fn cleanup_task_resources(
let _ = tmux_ops.kill_window(session_name);
}
if let Some(worktree) = worktree_path {
- if let Err(e) = run_cleanup_script_for_worktree(
- cleanup_script,
- project_path,
- Path::new(worktree),
- task_id,
- branch_name.as_deref(),
- force_cleanup,
- ) {
- eprintln!("cleanup_script failed: {}", e);
- return;
- }
+ run_cleanup_script_for_worktree(cleanup_script, Path::new(worktree));
let _ = git_ops.remove_worktree(project_path, worktree);
}
}
@@ -6472,8 +6393,7 @@ fn delete_task_resources(
project_path: &Path,
tmux_ops: &dyn TmuxOperations,
git_ops: &dyn GitOperations,
- force_cleanup: bool,
-) -> Result<()> {
+) {
// Kill tmux window if exists
if let Some(ref session_name) = task.session_name {
let _ = tmux_ops.kill_window(session_name);
@@ -6482,19 +6402,11 @@ fn delete_task_resources(
// Remove worktree and delete branch if exists
if let Some(ref worktree) = task.worktree_path {
if let Some(ref branch_name) = task.branch_name {
- run_cleanup_script_for_worktree(
- cleanup_script,
- project_path,
- Path::new(worktree),
- &task.id,
- task.branch_name.as_deref(),
- force_cleanup,
- )?;
+ run_cleanup_script_for_worktree(cleanup_script, Path::new(worktree));
let _ = git_ops.remove_worktree(project_path, worktree);
let _ = git_ops.delete_branch(project_path, branch_name);
}
}
- Ok(())
}
/// Collect git diff content from a worktree
diff --git a/src/tui/app_tests.rs b/src/tui/app_tests.rs
index cfb9059..1faf2a5 100644
--- a/src/tui/app_tests.rs
+++ b/src/tui/app_tests.rs
@@ -930,7 +930,6 @@ fn test_cleanup_task_for_done_with_resources() {
Path::new("/project"),
&mock_tmux,
&mock_git,
- false,
);
assert!(task.session_name.is_none());
@@ -957,7 +956,6 @@ fn test_cleanup_task_for_done_no_resources() {
Path::new("/project"),
&mock_tmux,
&mock_git,
- false,
);
assert_eq!(task.status, TaskStatus::Done);
@@ -1007,9 +1005,7 @@ fn test_delete_task_resources_full_cleanup() {
Path::new("/project"),
&mock_tmux,
&mock_git,
- false,
- )
- .unwrap();
+ );
}
/// Test delete_task_resources handles task without resources
@@ -1031,9 +1027,7 @@ fn test_delete_task_resources_no_resources() {
Path::new("/project"),
&mock_tmux,
&mock_git,
- false,
- )
- .unwrap();
+ );
}
// =============================================================================
@@ -7415,7 +7409,6 @@ fn test_cleanup_task_for_done_clears_session_and_worktree() {
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
);
assert_eq!(task.status, TaskStatus::Done);
@@ -7440,7 +7433,6 @@ fn test_cleanup_task_for_done_no_ops_when_no_session_or_worktree() {
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
);
assert_eq!(task.status, TaskStatus::Done);
@@ -7475,7 +7467,6 @@ fn test_cleanup_task_for_done_archives_md_files() {
project_dir.path(),
&mock_tmux,
&mock_git,
- false,
);
// Archived file should exist under .agtx/archive/my-slug/plan.md
@@ -7514,7 +7505,6 @@ fn test_cleanup_task_resources_kills_window_and_removes_worktree() {
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
);
}
@@ -7533,7 +7523,6 @@ fn test_cleanup_task_resources_noop_when_no_session_or_worktree() {
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
);
// No panic = correct (no mock calls made)
}
@@ -7570,9 +7559,7 @@ fn test_delete_task_resources_kills_window_removes_worktree_and_deletes_branch()
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
- )
- .unwrap();
+ );
}
#[test]
@@ -7589,9 +7576,7 @@ fn test_delete_task_resources_noop_when_no_session_or_worktree() {
Path::new("/tmp/proj"),
&mock_tmux,
&mock_git,
- false,
- )
- .unwrap();
+ );
}
// --- save_task ---
diff --git a/tests/git_tests.rs b/tests/git_tests.rs
index 67ad515..2290115 100644
--- a/tests/git_tests.rs
+++ b/tests/git_tests.rs
@@ -43,26 +43,6 @@ fn test_worktree_exists_false_for_nonexistent() {
assert!(!git::worktree_exists(temp_dir.path(), "nonexistent-task"));
}
-#[test]
-fn test_run_cleanup_script_captures_output_and_env() {
- let temp_dir = TempDir::new().unwrap();
- let envs = vec![("AGTX_TASK_ID".to_string(), "task-123".to_string())];
-
- let output = git::run_cleanup_script("echo $AGTX_TASK_ID", temp_dir.path(), &envs).unwrap();
-
- assert!(output.status.success());
- assert_eq!(output.stdout.trim(), "task-123");
-}
-
-#[test]
-fn test_run_cleanup_script_nonzero_exit() {
- let temp_dir = TempDir::new().unwrap();
-
- let output = git::run_cleanup_script("exit 42", temp_dir.path(), &[]).unwrap();
-
- assert!(!output.status.success());
-}
-
// =============================================================================
// Integration tests (require git)
// =============================================================================