cab7b28ed9
* chore(agentctl): delete duplicate procattr files in adapter package setProcGroup and killProcessGroup are defined in both agentctl/server/adapter/ and agentctl/server/process/. The process/ copies are the live ones (called by process/runner.go, manager.go, vscode.go); the adapter/ copies have zero callers and are removed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(agentctl): remove orphan helpers from transport/shared Left over from the ACP refactor. All had zero callers: - DebugEnabled(): internal debugMode var is checked directly by LogRawEvent/LogNormalizedEvent — the exported wrapper was unused. - GetBool, GetMap, GetSlice: only GetString/GetInt are called in acp/normalize.go. - NormalizeShellResult, NormalizeReadResult, NormalizeCodeSearchResult, NormalizeModifyResult: normalization moved to acp/normalize.go. - TruncateIfNeeded, MaxContentLength, MaxFileCount: only used by the four Normalize* helpers above; removed transitively. - TraceControlMessage: zero callers in the adapter transport layer. GenerateUnifiedDiff, SplitLines, DetectLanguage, Tracer, ShutdownTracing, TraceProtocolEvent, TraceProtocolRequest are alive and kept. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(agentctl): remove unused tool_payload constructors NewHttpRequest, NewCreateTask, NewSubagentTask, NewShowPlan, NewManageTodos, and NewMisc have zero callers — ACP normalization builds NormalizedPayload via struct literals and JSON unmarshaling, not these factory functions. NewReadFile, NewModifyFile, NewShellExec, NewCodeSearch, and NewGeneric are kept (live callers in acp/normalize.go). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(agents/detect): remove dead fluent options and OSPaths type WithFileExists, WithCommandOutput, WithEnvVar, expandHomePath (their transitive helper), and OSPaths (struct + Resolve + Expanded) have zero callers. Only WithCommand and WithNpxRunnable are used by agent IsInstalled implementations. Imports trimmed to context and os/exec. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(office): remove dead channel methods superseded by service/ ListComments, DeleteComment, and UpdateChannelStatus on ChannelService have zero callers — the live implementations live in internal/office/service/comments.go and service/channels.go. CreateComment is kept: it is called by HandleChannelInbound. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore: remove scattered orphan helpers Remove dead single-symbol exports verified to have zero callers: - ParseCronExpression (office/shared/cron.go): NextCronTime covers all live usages; the validator wrapper was unused. - IsValidPathComponent, MustJSON (office/shared/helpers.go): zero callers; GenerateSlug is kept. - DiscoverUserSkillsAt (office/skills/userhome_discovery.go): exported wrapper over the private discoverUserSkillsAt; only the internal DiscoverUserSkills method is wired up. - Manager.Get (agent/loginpty/manager.go): zero callers; GetByID and Stop are kept (Stop is tested in manager_test.go). - DetachedWithValues (common/appctx/appctx.go): zero callers; Detached is kept. - Repository.Close no-ops (task/share, terminal/repository): both are documented no-ops with zero callers. - TaskBranchName (worktree/config.go): zero callers; the deterministic TaskBranchNameWithSuffix is kept. Note: dialect.Now was initially targeted but is tested in dialect_test.go (same-package call — missed by package-qualified grep). It remains unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(share): remove stale Close reference from provider.go comment Repository.Close() was removed as a no-op in the round-4 cleanup. The comment in provider.go still referenced it by name; updated to reflect that the repo simply doesn't own its DB connections. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(office/shared): delete helpers.go — GenerateSlug had zero callers The triage step mistakenly identified skills/service.go as a caller, but that package defines its own local GenerateSlug (different regex, adds TrimSpace and a 'skill' fallback default). shared.GenerateSlug was never called anywhere. helpers.go is now empty; delete the file. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * chore(worktree): fix stale TaskBranchName reference in TaskBranchNameWithSuffix doc Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
252 lines
8.0 KiB
Go
252 lines
8.0 KiB
Go
package worktree
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// Config holds configuration for the worktree manager.
|
|
type Config struct {
|
|
// Enabled controls whether worktree mode is active.
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
// TasksBasePath is the base directory for per-task worktree storage.
|
|
// Each task gets a subdirectory containing one repo worktree (future: multiple).
|
|
// Supports ~ expansion for home directory.
|
|
// Default: ~/.kandev/tasks
|
|
TasksBasePath string `mapstructure:"tasks_base_path"`
|
|
|
|
// BranchPrefix is the prefix used for worktree branch names.
|
|
// Default: feature/
|
|
BranchPrefix string `mapstructure:"branch_prefix"`
|
|
|
|
// FetchTimeoutSeconds is the timeout for pre-worktree git fetch.
|
|
// If <= 0, manager default is used.
|
|
FetchTimeoutSeconds int `mapstructure:"fetch_timeout_seconds"`
|
|
|
|
// PullTimeoutSeconds is the timeout for pre-worktree git pull.
|
|
// If <= 0, manager default is used.
|
|
PullTimeoutSeconds int `mapstructure:"pull_timeout_seconds"`
|
|
}
|
|
|
|
// DefaultBranchPrefix is used when no repository-specific prefix is provided.
|
|
const DefaultBranchPrefix = "feature/"
|
|
|
|
// Validate validates the configuration and returns an error if invalid.
|
|
func (c *Config) Validate() error {
|
|
if c.BranchPrefix == "" {
|
|
c.BranchPrefix = DefaultBranchPrefix
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetTasksBasePathFallback sets the TasksBasePath from the data directory if not already configured.
|
|
func (c *Config) SetTasksBasePathFallback(dataDir string) {
|
|
if c.TasksBasePath == "" && dataDir != "" {
|
|
c.TasksBasePath = filepath.Join(dataDir, "tasks")
|
|
}
|
|
}
|
|
|
|
// ExpandedTasksBasePath returns the tasks base path with ~ expanded to the user's home directory.
|
|
func (c *Config) ExpandedTasksBasePath() (string, error) {
|
|
path := c.TasksBasePath
|
|
if strings.HasPrefix(path, "~/") {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
path = filepath.Join(home, path[2:])
|
|
}
|
|
return path, nil
|
|
}
|
|
|
|
// TaskWorktreePath returns the full path for a worktree inside a task directory.
|
|
// Format: {tasksBase}/{taskDirName}/{repoName}
|
|
func (c *Config) TaskWorktreePath(taskDirName, repoName string) (string, error) {
|
|
basePath, err := c.ExpandedTasksBasePath()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(basePath, taskDirName, repoName), nil
|
|
}
|
|
|
|
// BranchName returns the branch name for a given task ID and suffix.
|
|
// Format: {prefix}{taskID}-{suffix} e.g. feature/abc123-xyz
|
|
func (c *Config) BranchName(taskID, suffix string) string {
|
|
return c.BranchPrefix + taskID + "-" + suffix
|
|
}
|
|
|
|
// SemanticBranchName returns a branch name using a semantic name derived from task title.
|
|
// Format: {prefix}{semanticName}-{suffix} e.g. feature/fix-login-bug-abc
|
|
func (c *Config) SemanticBranchName(semanticName, suffix string) string {
|
|
return c.BranchPrefix + semanticName + "-" + suffix
|
|
}
|
|
|
|
// isASCIIAlphaNum reports whether r is an ASCII letter or digit. Restricting
|
|
// to ASCII (rather than the broader unicode.IsLetter/IsDigit) keeps branch
|
|
// and worktree-directory names usable across tools and filesystems — Unicode
|
|
// letters such as CJK ideographs, Cyrillic, or Arabic characters are valid
|
|
// git refs but break many downstream consumers (see issue #1081).
|
|
func isASCIIAlphaNum(r rune) bool {
|
|
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9')
|
|
}
|
|
|
|
// SanitizeForBranch converts a task title into a valid git branch name component.
|
|
// It:
|
|
// - Converts to lowercase
|
|
// - Replaces spaces and non-ASCII-alphanumeric characters with hyphens
|
|
// - Removes consecutive hyphens
|
|
// - Truncates to maxLen characters
|
|
// - Removes leading/trailing hyphens
|
|
func SanitizeForBranch(title string, maxLen int) string {
|
|
if title == "" {
|
|
return ""
|
|
}
|
|
|
|
// Convert to lowercase
|
|
result := strings.ToLower(title)
|
|
|
|
// Replace any character that's not ASCII-alphanumeric with a hyphen.
|
|
// Git branch names allow a broader character set, but we restrict to ASCII
|
|
// alphanumerics + hyphens for cleaner, portable names.
|
|
var sb strings.Builder
|
|
for _, r := range result {
|
|
if isASCIIAlphaNum(r) {
|
|
sb.WriteRune(r)
|
|
} else {
|
|
sb.WriteRune('-')
|
|
}
|
|
}
|
|
result = sb.String()
|
|
|
|
// Remove consecutive hyphens
|
|
result = repoDirHyphenRun.ReplaceAllString(result, "-")
|
|
|
|
// Remove leading and trailing hyphens
|
|
result = strings.Trim(result, "-")
|
|
|
|
// Truncate to maxLen
|
|
if len(result) > maxLen {
|
|
result = result[:maxLen]
|
|
// Remove trailing hyphen after truncation
|
|
result = strings.TrimRight(result, "-")
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// NormalizeBranchPrefix trims and falls back to the default prefix.
|
|
func NormalizeBranchPrefix(prefix string) string {
|
|
trimmed := strings.TrimSpace(prefix)
|
|
if trimmed == "" {
|
|
return DefaultBranchPrefix
|
|
}
|
|
return trimmed
|
|
}
|
|
|
|
// TaskBranchNameWithSuffix builds the per-task branch name from a caller-supplied suffix.
|
|
// The caller is responsible for generating the suffix (e.g. SmallSuffix(3)) so that
|
|
// naming is deterministic when the suffix is derived externally.
|
|
func TaskBranchNameWithSuffix(taskTitle, taskID, prefix, suffix string) string {
|
|
sanitized := SanitizeForBranch(taskTitle, 20)
|
|
if sanitized == "" {
|
|
sanitized = SanitizeForBranch(taskID, 20)
|
|
}
|
|
if sanitized == "" {
|
|
sanitized = "task"
|
|
}
|
|
return NormalizeBranchPrefix(prefix) + sanitized + "-" + suffix
|
|
}
|
|
|
|
// ValidateBranchPrefix ensures a prefix contains only safe branch characters.
|
|
func ValidateBranchPrefix(prefix string) error {
|
|
trimmed := strings.TrimSpace(prefix)
|
|
if trimmed == "" {
|
|
return nil
|
|
}
|
|
for _, r := range trimmed {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '/' || r == '-' || r == '_' || r == '.' {
|
|
continue
|
|
}
|
|
return fmt.Errorf("invalid branch prefix")
|
|
}
|
|
if strings.Contains(trimmed, "..") || strings.Contains(trimmed, "@{") {
|
|
return fmt.Errorf("invalid branch prefix")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const branchSuffixAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
// SmallSuffix returns a random suffix capped at 3 characters.
|
|
func SmallSuffix(maxLen int) string {
|
|
if maxLen <= 0 {
|
|
return ""
|
|
}
|
|
if maxLen > 3 {
|
|
maxLen = 3
|
|
}
|
|
buf := make([]byte, maxLen)
|
|
if _, err := rand.Read(buf); err != nil {
|
|
return strings.Repeat("x", maxLen)
|
|
}
|
|
for i := range buf {
|
|
buf[i] = branchSuffixAlphabet[int(buf[i])%len(branchSuffixAlphabet)]
|
|
}
|
|
return string(buf)
|
|
}
|
|
|
|
// SanitizeRepoDirName converts a repository display name into a single,
|
|
// filesystem-safe path segment. Path separators and other unsafe characters
|
|
// are replaced with hyphens, runs of hyphens are collapsed, and surrounding
|
|
// hyphens/dots are trimmed. Returns an empty string when the input has no
|
|
// usable characters.
|
|
//
|
|
// This guards against names like "owner/repo" producing nested subdirectories
|
|
// when used as the worktree directory under a multi-repo task root — the
|
|
// extra path level breaks sibling-repo detection in agentctl.
|
|
//
|
|
// Limitation: distinct names that differ only in unsafe characters (e.g.
|
|
// "acme/widget-config" and "acme-widget-config") collapse to the same
|
|
// segment. Two such repos in one task would collide on `git worktree add`.
|
|
// Acceptable in practice — provider repos (the common case) are uniquely
|
|
// identified by owner/name and won't collide with each other.
|
|
func SanitizeRepoDirName(name string) string {
|
|
if name == "" {
|
|
return ""
|
|
}
|
|
var sb strings.Builder
|
|
sb.Grow(len(name))
|
|
for _, r := range name {
|
|
switch {
|
|
case isASCIIAlphaNum(r):
|
|
sb.WriteRune(r)
|
|
case r == '_', r == '.', r == '-':
|
|
sb.WriteRune(r)
|
|
default:
|
|
sb.WriteRune('-')
|
|
}
|
|
}
|
|
result := repoDirHyphenRun.ReplaceAllString(sb.String(), "-")
|
|
return strings.Trim(result, "-.")
|
|
}
|
|
|
|
var repoDirHyphenRun = regexp.MustCompile(`-+`)
|
|
|
|
// SemanticWorktreeName generates a semantic worktree directory name from a task title.
|
|
// Format: {sanitizedTitle}_{suffix} e.g. fix-login-bug_ab12cd34
|
|
// The title is truncated to 20 characters before adding the suffix.
|
|
func SemanticWorktreeName(taskTitle, suffix string) string {
|
|
semanticName := SanitizeForBranch(taskTitle, 20)
|
|
if semanticName == "" {
|
|
// Fallback to just suffix if title is empty or all special chars
|
|
return suffix
|
|
}
|
|
return semanticName + "_" + suffix
|
|
}
|