fd79d6170f
* feat(tasks): multi-branch tasks — N (repo, branch) pairs per task Extends multi-repo into multi-branch so a single task can hold multiple worktrees on the same repo (one per branch). Sibling worktrees layout at <task>/<repo>/ + <task>/<repo>-<slug>/, dedicated PRWatch per branch, live agentctl tracker rescan + frontend store updates so the UI splits PRs / files / commits per branch without a session restart. MCP add_branch_to_task_kandev tool is shielded to the worktree executor. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): unify multi-branch repo label as <repo> · <branch> across panels PR Changes and Commits panels rendered repo labels differently for multi-branch tasks — Commits showed "kandev · feature/x" while PR Changes leaked the raw "kandev-feature-x" agentctl tracker tag. Reformat tracker-tagged repo names via formatRepoLabel using the longest known-repo prefix so kandev-cli-* doesn't collapse to kandev · cli-*. * fix(tasks): address multi-branch PR review — data races, slug collisions, MCP error codes Backend - agentctl process.Manager: add repoTrackersMu RWMutex guarding repoTrackers + workspaceTracker. Reads snapshot under RLock; RescanRepositories builds trackers first, takes Lock for swap/append. Validates newWorkDir via os.Stat before publishing cfg.WorkDir. - worktree.Manager: reject invalid explicit BranchSlug up-front; cache key uses sanitized wt.BranchSlug so read/write paths are symmetric. - worktree.store: narrow isDuplicateColumnError to "duplicate column name". - task service AddBranchToTask: slug-collision guard rejects feat/a vs feat-a. - orchestrator resolvePushRepo: two-pass exact-name match before subdir prefix fallback so "backend" doesn't swallow pushes for "backend-admin". - executor buildRepoSpecs: duplicate repos always get a non-empty BranchSlug (falls back to branch-N when both branch fields sanitize to empty). - MCP handler: classifyAddBranchError maps service errors to NOT_FOUND / CONFLICT / VALIDATION_ERROR; add ErrorCodeConflict constant. - MCP server: extract registerAddBranchToTaskTool, register in task mode only (external mode contract: config tools + create_task_kandev). - workspace_rescan handler: reject malformed JSON with 400 instead of silently rescanning the current root. - branch_materializer: split MaterializeBranch into prepare + finalize helpers to satisfy the 80-line cap. Frontend - existingPrUrl returns undefined when a task has >1 PR so per-repo View PR buttons take over (no silent linking to a sibling). - formatRepoLabel checks exact known-repo match before prefix splitting so "kandev-cli" isn't mislabeled as "kandev · cli". - Repo chip key now includes base_branch so siblings differing only on base render as distinct chips. Docs - multi-branch spec uniqueness wording aligned with the canonical 4-column key (task_id, repository_id, base_branch, checkout_branch). - worktree.CreateRequest.BranchSlug doc describes the sibling layout (<repo>-<slug>) instead of the obsolete nested form. * fix(tasks): scope slug-collision check to repo only + use errors.Is for ErrTaskNotFound - service_branches: drop base_branch from the slug-collision check. Worktree path is (repo, slug); two rows on the same repo cut from different bases still collide on disk. - service_branches: prepareBranchAdd wraps taskrepo.ErrTaskNotFound via fmt.Errorf with %w so classifiers don't have to substring-match the formatted task UUID. - mcp handlers: classifyAddBranchError uses errors.Is(err, ErrTaskNotFound). * fix(agentctl): sanitize rescan work_dir before os.Stat to clear CodeQL path-injection CodeQL flagged the rescan endpoint as a path-injection sink (the HTTP-supplied work_dir flowed straight into os.Stat and downstream scanRepositorySubdirs). Add validateRescanPath which: - requires the input to be a clean absolute path with no ".." segments - rejects anything that isn't equal to or an ancestor of the current cfg.WorkDir — the only legitimate use case is the branch materializer promoting <task>/<repo>/ to <task>/ Defense in depth: agentctl binds to localhost, but any colocated process could otherwise direct it at /etc or the user's home by POSTing /api/v1/workspace/rescan. * fix(agentctl): tighten validateRescanPath, close TOCTOU on existingTrackers, add tests - Drop the strings.Contains(clean, "..") guard. filepath.Clean already resolves traversal; the substring check rejects legitimate names like /home/u/..hidden without adding defense (the ancestor check via filepath.Rel is the real boundary). - Drop the rel == "." branch in the ancestor check — early return for clean == currentClean already covers identity. - Read existingTrackers inside the write-lock section (not under RLock then re-Lock) so two concurrent rescans can't both see 0 trackers and double-bootstrap the multi-repo set. - Add table-driven TestValidateRescanPath covering rejection paths (relative, descendant, sibling, traversal-cleaned, empty input), legitimate ancestor promotion, exact match, no-anchor first-launch, and a /home/u/..hidden dotted-segment case. * fix(agentctl): break taint flow into os.Stat — derive rescan path from cfg, not HTTP body CodeQL's go/path-injection trace still flagged manager_rescan.go after the prior sanitizer because the user-supplied newWorkDir flowed (after validation) into os.Stat and then into cfg.WorkDir → tracker paths → workspace_monitor os.Stat sinks. Replace validateRescanPath with resolveRescanPath: it returns a path DERIVED from currentWorkDir (no-op = currentClean, promotion = filepath.Dir(currentClean)) and uses newPath only for comparison, never as a source for the returned string. os.Stat now receives a path whose provenance is trusted manager config. Functionally the legitimate caller (branch materializer promoting <task>/<repo>/ to <task>/) is unchanged. * test(agentctl): make TestResolveRescanPath windows-portable The Linux Backend Tests job passed but the Backend (windows) job rejected every "absolute" case because filepath.IsAbs("/task/repo") is false on Windows (no volume letter). Build inputs via filepath.Join against absRoot() (filepath.Abs of "/" / "\\") so the same table covers POSIX (`/...`) and Windows (`C:\...`) without per-OS branches. Keep the traversal-after-Clean case POSIX-only since filepath.Clean is the shared primitive and that input form needs a volume to be considered absolute on Windows anyway. * fix(agentctl): serialize RescanRepositories + re-check trackers under write lock Two concurrent RescanRepositories calls could both observe an empty tracker set (between the snapshot read and the bootstrap branch) and both enter transitionToMultiRepoMode, leaking a duplicate bare-root tracker and duplicate per-repo trackers. Same race in appendNewRepoTrackers would double-append the same new child across both callers. Fix: - Add rescanMu sync.Mutex on Manager. Acquired for the full body of RescanRepositories so the snapshot → branch → mutate sequence is serialized. repoTrackersMu still guards individual field reads from unrelated goroutines (subscribe/unsubscribe/poll-mode). - appendNewRepoTrackers re-checks membership inside the write-lock before appending and drops already-present trackers (Stop + detach outside the lock). Defense in depth. - Test: add `dotted segment is legal as promotion` case so the ..hidden-segment promotion path stays exercised. --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
201 lines
7.0 KiB
Go
201 lines
7.0 KiB
Go
package worktree
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
)
|
|
|
|
const (
|
|
defaultGitFetchTimeout = 90 * time.Second
|
|
defaultGitPullTimeout = 60 * time.Second
|
|
defaultGitInspectTimeout = 10 * time.Second
|
|
gitNoTags = "--no-tags"
|
|
)
|
|
|
|
// repoLockEntry tracks a repository lock and its reference count.
|
|
type repoLockEntry struct {
|
|
mu *sync.Mutex
|
|
refCount int
|
|
}
|
|
|
|
// Manager handles Git worktree operations for concurrent agent execution.
|
|
type Manager struct {
|
|
config Config
|
|
logger *logger.Logger
|
|
store Store
|
|
// worktrees is the in-memory cache keyed by cacheKey(sessionID, repositoryID).
|
|
// For legacy single-repo writes the repositoryID may be empty, in which
|
|
// case the cache key collapses to "{sessionID}|" — still distinct from
|
|
// any per-repo entry the same session might gain later.
|
|
worktrees map[string]*Worktree
|
|
mu sync.RWMutex // Protects worktrees map
|
|
repoLocks map[string]*repoLockEntry
|
|
repoLockMu sync.Mutex
|
|
|
|
// Optional dependencies for script execution
|
|
repoProvider RepositoryProvider
|
|
scriptMsgHandler ScriptMessageHandler
|
|
|
|
// Timeouts for best-effort remote sync before creating a worktree.
|
|
fetchTimeout time.Duration
|
|
pullTimeout time.Duration
|
|
// Bound for cheap git ref-inspection commands (branchExists, currentBranch).
|
|
inspectTimeout time.Duration
|
|
}
|
|
|
|
// ScriptMessageHandler provides script execution and message streaming.
|
|
type ScriptMessageHandler interface {
|
|
ExecuteSetupScript(ctx context.Context, req ScriptExecutionRequest) error
|
|
ExecuteCleanupScript(ctx context.Context, req ScriptExecutionRequest) error
|
|
}
|
|
|
|
// Store is the interface for worktree persistence.
|
|
type Store interface {
|
|
// CreateWorktree persists a new worktree record.
|
|
CreateWorktree(ctx context.Context, wt *Worktree) error
|
|
// GetWorktreeByID retrieves a worktree by its unique ID.
|
|
GetWorktreeByID(ctx context.Context, id string) (*Worktree, error)
|
|
// GetWorktreeBySessionID retrieves a single active worktree by session ID.
|
|
// For multi-repo sessions, returns the first one found (typically the
|
|
// primary/first-created). Use GetWorktreesBySessionID when the caller
|
|
// needs all of them.
|
|
GetWorktreeBySessionID(ctx context.Context, sessionID string) (*Worktree, error)
|
|
// GetWorktreesByTaskID retrieves all worktrees for a task (used for cleanup on task deletion).
|
|
GetWorktreesByTaskID(ctx context.Context, taskID string) ([]*Worktree, error)
|
|
// GetWorktreesByRepositoryID retrieves all worktrees for a repository.
|
|
GetWorktreesByRepositoryID(ctx context.Context, repoID string) ([]*Worktree, error)
|
|
// UpdateWorktree updates an existing worktree record.
|
|
UpdateWorktree(ctx context.Context, wt *Worktree) error
|
|
// DeleteWorktree removes a worktree record.
|
|
DeleteWorktree(ctx context.Context, id string) error
|
|
// ListActiveWorktrees returns all active worktrees.
|
|
ListActiveWorktrees(ctx context.Context) ([]*Worktree, error)
|
|
// ListActiveWorktreePaths returns the worktree_path of every active,
|
|
// non-deleted worktree row that has a non-empty path. Used by the
|
|
// office GC to identify live worktrees that must not be swept.
|
|
ListActiveWorktreePaths(ctx context.Context) ([]string, error)
|
|
}
|
|
|
|
// MultiRepoStore is an optional capability some stores implement to support
|
|
// multi-repo task sessions (one worktree per repository per session). The
|
|
// Manager checks at runtime whether its Store satisfies this interface and
|
|
// uses the multi-repo lookups when available.
|
|
type MultiRepoStore interface {
|
|
// GetWorktreesBySessionID returns all active worktrees for the session.
|
|
GetWorktreesBySessionID(ctx context.Context, sessionID string) ([]*Worktree, error)
|
|
// GetWorktreeBySessionAndRepository returns the active worktree for the
|
|
// given (session, repository, branchSlug) triple, or nil if none exists.
|
|
// branchSlug scopes the lookup for multi-branch tasks; empty matches the
|
|
// legacy single-branch persistence shape.
|
|
GetWorktreeBySessionAndRepository(ctx context.Context, sessionID, repositoryID, branchSlug string) (*Worktree, error)
|
|
}
|
|
|
|
// NewManager creates a new worktree manager.
|
|
func NewManager(cfg Config, store Store, log *logger.Logger) (*Manager, error) {
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, fmt.Errorf("invalid config: %w", err)
|
|
}
|
|
|
|
if log == nil {
|
|
log = logger.Default()
|
|
}
|
|
|
|
// Ensure tasks base directory exists (if configured)
|
|
if cfg.TasksBasePath != "" {
|
|
tasksBase, err := cfg.ExpandedTasksBasePath()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to expand tasks base path: %w", err)
|
|
}
|
|
if err := os.MkdirAll(tasksBase, 0755); err != nil {
|
|
return nil, fmt.Errorf("failed to create tasks base directory: %w", err)
|
|
}
|
|
}
|
|
|
|
fetchTimeout := defaultGitFetchTimeout
|
|
if cfg.FetchTimeoutSeconds > 0 {
|
|
fetchTimeout = time.Duration(cfg.FetchTimeoutSeconds) * time.Second
|
|
}
|
|
|
|
pullTimeout := defaultGitPullTimeout
|
|
if cfg.PullTimeoutSeconds > 0 {
|
|
pullTimeout = time.Duration(cfg.PullTimeoutSeconds) * time.Second
|
|
}
|
|
|
|
return &Manager{
|
|
config: cfg,
|
|
logger: log.WithFields(zap.String("component", "worktree-manager")),
|
|
store: store,
|
|
worktrees: make(map[string]*Worktree),
|
|
repoLocks: make(map[string]*repoLockEntry),
|
|
fetchTimeout: fetchTimeout,
|
|
pullTimeout: pullTimeout,
|
|
inspectTimeout: defaultGitInspectTimeout,
|
|
}, nil
|
|
}
|
|
|
|
// SetRepositoryProvider sets the repository provider for fetching repository information.
|
|
func (m *Manager) SetRepositoryProvider(provider RepositoryProvider) {
|
|
m.repoProvider = provider
|
|
}
|
|
|
|
// SetScriptMessageHandler sets the script message handler for executing setup/cleanup scripts.
|
|
func (m *Manager) SetScriptMessageHandler(handler ScriptMessageHandler) {
|
|
m.scriptMsgHandler = handler
|
|
}
|
|
|
|
// ListActiveWorktreePaths returns the absolute on-disk paths of all
|
|
// currently active, non-deleted worktrees. Used by the office GC as the
|
|
// authoritative inventory of paths that must not be swept.
|
|
func (m *Manager) ListActiveWorktreePaths(ctx context.Context) ([]string, error) {
|
|
return m.store.ListActiveWorktreePaths(ctx)
|
|
}
|
|
|
|
// IsEnabled returns whether worktree mode is enabled.
|
|
func (m *Manager) IsEnabled() bool {
|
|
return m.config.Enabled
|
|
}
|
|
|
|
// getRepoLock returns a mutex for the given repository path and increments its reference count.
|
|
func (m *Manager) getRepoLock(repoPath string) *sync.Mutex {
|
|
m.repoLockMu.Lock()
|
|
defer m.repoLockMu.Unlock()
|
|
|
|
if entry, exists := m.repoLocks[repoPath]; exists {
|
|
entry.refCount++
|
|
return entry.mu
|
|
}
|
|
|
|
entry := &repoLockEntry{
|
|
mu: &sync.Mutex{},
|
|
refCount: 1,
|
|
}
|
|
m.repoLocks[repoPath] = entry
|
|
return entry.mu
|
|
}
|
|
|
|
// releaseRepoLock decrements the reference count for a repository lock.
|
|
// If the count reaches zero, the lock is removed from the map to prevent memory leaks.
|
|
func (m *Manager) releaseRepoLock(repoPath string) {
|
|
m.repoLockMu.Lock()
|
|
defer m.repoLockMu.Unlock()
|
|
|
|
entry, exists := m.repoLocks[repoPath]
|
|
if !exists {
|
|
return
|
|
}
|
|
|
|
entry.refCount--
|
|
if entry.refCount <= 0 {
|
|
delete(m.repoLocks, repoPath)
|
|
m.logger.Debug("released repository lock",
|
|
zap.String("repository_path", repoPath))
|
|
}
|
|
}
|