Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: keep planning worktree state until remove succeeds (#4017)

Junghwanba6890236965
diff --git a/camel/toolkits/planning_worktree_toolkit.py b/camel/toolkits/planning_worktree_toolkit.py
index 96ddfd720..47122cbda 100644
--- a/camel/toolkits/planning_worktree_toolkit.py
+++ b/camel/toolkits/planning_worktree_toolkit.py
@@ -115,10 +115,7 @@ class PlanningWorktreeToolkit(BaseToolkit):
try:
if not plan_file.exists():
plan_file.write_text(
- "# Plan\n\n"
- "- Objective:\n"
- "- Constraints:\n"
- "- Steps:\n",
+ "# Plan\n\n- Objective:\n- Constraints:\n- Steps:\n",
encoding="utf-8",
)
except OSError as exc:
@@ -340,11 +337,6 @@ class PlanningWorktreeToolkit(BaseToolkit):
working_directory=str(self.working_directory),
)
- self.working_directory = main_repo_path
- self.current_worktree_path = None
- if self.switch_process_cwd:
- os.chdir(main_repo_path)
-
remove_result = self._run_git(
"worktree",
"remove",
@@ -359,6 +351,11 @@ class PlanningWorktreeToolkit(BaseToolkit):
working_directory=str(self.working_directory),
)
+ self.working_directory = main_repo_path
+ self.current_worktree_path = None
+ if self.switch_process_cwd:
+ os.chdir(main_repo_path)
+
if branch_name:
self._run_git(
"branch",
diff --git a/test/toolkits/test_planning_worktree_toolkit.py b/test/toolkits/test_planning_worktree_toolkit.py
index 3381ddd10..8f33aa6c8 100644
--- a/test/toolkits/test_planning_worktree_toolkit.py
+++ b/test/toolkits/test_planning_worktree_toolkit.py
@@ -177,6 +177,39 @@ def test_worktree_reentry_returns_error():
toolkit.worktree_remove_worktree()
+def test_worktree_remove_failure_preserves_state(monkeypatch, tmp_path):
+ repo = tmp_path / "repo"
+ worktree = tmp_path / "repo-worktree"
+ repo.mkdir()
+ worktree.mkdir()
+
+ toolkit = PlanningWorktreeToolkit(working_directory=str(worktree))
+ toolkit.current_worktree_path = worktree
+
+ def fake_run_git(*args, cwd=None):
+ if args == ("rev-parse", "--show-toplevel"):
+ return subprocess.CompletedProcess(args, 0, f"{repo}\n", "")
+ if args == ("rev-parse", "--abbrev-ref", "HEAD"):
+ return subprocess.CompletedProcess(args, 0, "camel/test\n", "")
+ if args == ("worktree", "list", "--porcelain"):
+ return subprocess.CompletedProcess(
+ args, 0, f"worktree {repo}\nworktree {worktree}\n", ""
+ )
+ if args[:2] == ("worktree", "remove"):
+ return subprocess.CompletedProcess(args, 1, "", "remove failed")
+ if args[:2] == ("branch", "-d"):
+ return subprocess.CompletedProcess(args, 0, "", "")
+ raise AssertionError(f"unexpected git call: {args} cwd={cwd}")
+
+ monkeypatch.setattr(toolkit, "_run_git", fake_run_git)
+
+ result = toolkit.worktree_remove_worktree()
+
+ assert "error" in result
+ assert toolkit.current_worktree_path == worktree
+ assert toolkit.working_directory == worktree.resolve()
+
+
def test_concurrent_plan_mode_enter_exit():
"""Multiple agents can independently enter and exit plan mode."""
with tempfile.TemporaryDirectory() as temp_dir: