This commit is contained in:
2026-06-22 08:55:57 +08:00
parent dbb87823e8
commit 6ade6e8fa9
325 changed files with 41131 additions and 855 deletions
+23 -5
View File
@@ -12,12 +12,18 @@ import (
"github.com/yan1h/agent-coding-workflow/internal/infra/git"
)
// IndexBuildHook is a best-effort callback fired after a worktree is created (or
// a workspace is synced) to enqueue a code-index build. Injected from app.go via
// SetIndexBuildHook to avoid a workspace→codeindex import cycle. May be nil.
type IndexBuildHook func(ctx context.Context, wsID uuid.UUID, worktreeID *uuid.UUID, commitSHA string) error
type worktreeService struct {
repo Repository
rec audit.Recorder
gitr git.Runner
pa ProjectAccess
dataDir string
repo Repository
rec audit.Recorder
gitr git.Runner
pa ProjectAccess
dataDir string
indexHook IndexBuildHook
}
// NewWorktreeService 构造 WorktreeService。
@@ -25,6 +31,10 @@ func NewWorktreeService(repo Repository, rec audit.Recorder, gitr git.Runner, pa
return &worktreeService{repo: repo, rec: rec, gitr: gitr, pa: pa, dataDir: dataDir}
}
// SetIndexBuildHook injects the code-index enqueue callback (app wiring). Safe to
// call once at construction; nil disables index builds on worktree create.
func (s *worktreeService) SetIndexBuildHook(h IndexBuildHook) { s.indexHook = h }
func (s *worktreeService) Create(ctx context.Context, c Caller, wsID uuid.UUID, in CreateWorktreeInput) (*Worktree, error) {
ws, err := s.repo.GetWorkspaceByID(ctx, wsID)
if err != nil {
@@ -71,6 +81,14 @@ func (s *worktreeService) Create(ctx context.Context, c Caller, wsID uuid.UUID,
return nil, err
}
s.audit(ctx, &c.UserID, "worktree.create", "worktree", saved.ID.String(), map[string]any{"branch": in.Branch})
// Best-effort: enqueue a code-index build for the new worktree at its HEAD.
// Failures are logged via audit but never block worktree creation.
if s.indexHook != nil {
wtID := saved.ID
if herr := s.indexHook(ctx, wsID, &wtID, ""); herr != nil {
s.audit(ctx, &c.UserID, "worktree.index_enqueue_failed", "worktree", saved.ID.String(), map[string]any{"err": herr.Error()})
}
}
return saved, nil
}