feat: worktree 隔离方案 - 多 agent 独立工作目录 - Closes #102
CI / test (pull_request) Successful in 19s

启动 agent 后自动创建 ~/.gitea/worktrees/<user>/ 隔离目录,
多个 agent 可同时修改不同文件、不同分支互不干扰。

- _common.sh: 新增 setup_worktree/cleanup_worktree 函数
- start_dev_agent.sh: 启动时自动切 worktree
- start_qe_agent.sh: 同上
- DEV_AGENT.md/QE_AGENT.md: 启动行为增加 worktree 检查步骤

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 17:33:15 +08:00
parent 9dff1617ea
commit 5175fbaf14
5 changed files with 53 additions and 3 deletions
+37 -1
View File
@@ -6,7 +6,8 @@ set -eu
# ── Resolve paths ──────────────────────────────────────────────────────────────
_COMMON_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="${PROJECT_DIR:-$(cd "$_COMMON_DIR/.." && pwd)}"
_MAIN_REPO_DIR="$(cd "$_COMMON_DIR/.." && pwd)"
PROJECT_DIR="${PROJECT_DIR:-$_MAIN_REPO_DIR}"
# ── Load Gitea configuration ────────────────────────────────────────────────────
# Primary: ~/.gitea/config.yaml (requires GITEA_USER)
@@ -18,6 +19,41 @@ if ! eval "$(python "$_COMMON_DIR/_get_gitea_config.py" 2>/dev/null)"; then
fi
fi
# ── Worktree isolation ─────────────────────────────────────────────────────────
GITEA_WORKTREE_DIR="${GITEA_WORKTREE_DIR:-$HOME/.gitea/worktrees}"
setup_worktree() {
local user="$1"
local worktree="$GITEA_WORKTREE_DIR/$user"
# Already inside a worktree we created — reuse it.
if [ -f "$worktree/.gitea-worktree" ]; then
echo "Using existing worktree: $worktree"
PROJECT_DIR="$worktree"
cd "$PROJECT_DIR"
return 0
fi
local branch="agent/${user}/$(date +%Y%m%d-%H%M%S)"
echo "Creating worktree: $worktree (branch: $branch)"
mkdir -p "$GITEA_WORKTREE_DIR"
git -C "$_MAIN_REPO_DIR" worktree add -b "$branch" "$worktree" origin/main
touch "$worktree/.gitea-worktree"
PROJECT_DIR="$worktree"
cd "$PROJECT_DIR"
}
cleanup_worktree() {
local user="$1"
local worktree="$GITEA_WORKTREE_DIR/$user"
if [ -d "$worktree" ]; then
rm -f "$worktree/.gitea-worktree"
echo "Cleaning up worktree: $worktree"
git -C "$_MAIN_REPO_DIR" worktree remove "$worktree" 2>/dev/null || true
rm -rf "$worktree" 2>/dev/null || true
fi
}
# ── Validate required environment ──────────────────────────────────────────────
require_token() {
if [ -z "${GITEA_API_TOKEN:-}" ]; then
+6
View File
@@ -15,6 +15,12 @@ export GITEA_USER="$1"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/_common.sh"
# Switch to isolated worktree so multiple agents don't conflict
setup_worktree "$GITEA_USER"
# Cleanup worktree on exit (optional, comment out to keep for debugging)
trap 'cleanup_worktree "$GITEA_USER"' EXIT
banner "Dev"
require_token
+6
View File
@@ -15,6 +15,12 @@ export GITEA_USER="$1"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/_common.sh"
# Switch to isolated worktree so multiple agents don't conflict
setup_worktree "$GITEA_USER"
# Cleanup worktree on exit (optional, comment out to keep for debugging)
trap 'cleanup_worktree "$GITEA_USER"' EXIT
banner "QE"
require_token