dispatchWorktreeCreate fires the worktree_create hooks of the agent the run targets, just after the worktree is created and before the session exists. Unlike every other event, this is dispatched from the CLI rather than the run loop: the worktree (and the working directory the runtime, session, too
(ctx context.Context, out *cli.Printer, t *team.Team, wt *worktree.Worktree)
| 683 | // Hooks run inside the new worktree so setup commands (copy .env, install |
| 684 | // deps) operate on the fresh checkout. A blocking verdict aborts the run. |
| 685 | func (f *runExecFlags) dispatchWorktreeCreate(ctx context.Context, out *cli.Printer, t *team.Team, wt *worktree.Worktree) error { |
| 686 | agt, err := t.AgentOrDefault(f.agentName) |
| 687 | if err != nil { |
| 688 | return err |
| 689 | } |
| 690 | hooksCfg := agt.Hooks() |
| 691 | if hooksCfg == nil { |
| 692 | return nil |
| 693 | } |
| 694 | |
| 695 | executor := hooks.NewExecutor(hooksCfg, wt.Dir, os.Environ()) |
| 696 | if !executor.Has(hooks.EventWorktreeCreate) { |
| 697 | return nil |
| 698 | } |
| 699 | |
| 700 | result, err := executor.Dispatch(ctx, hooks.EventWorktreeCreate, &hooks.Input{ |
| 701 | AgentName: agt.Name(), |
| 702 | Cwd: wt.Dir, |
| 703 | WorktreePath: wt.Dir, |
| 704 | WorktreeBranch: wt.Branch, |
| 705 | WorktreeSourceDir: wt.SourceDir, |
| 706 | }) |
| 707 | if err != nil { |
| 708 | return fmt.Errorf("running worktree_create hooks: %w", err) |
| 709 | } |
| 710 | if result.SystemMessage != "" { |
| 711 | out.Println(result.SystemMessage) |
| 712 | } |
| 713 | if result.AdditionalContext != "" { |
| 714 | out.Println(result.AdditionalContext) |
| 715 | } |
| 716 | if !result.Allowed { |
| 717 | msg := result.Message |
| 718 | if msg == "" { |
| 719 | msg = "a worktree_create hook blocked the run" |
| 720 | } |
| 721 | return fmt.Errorf("worktree_create hook aborted the run: %s", msg) |
| 722 | } |
| 723 | return nil |
| 724 | } |
| 725 | |
| 726 | func (f *runExecFlags) loadAgentFrom(ctx context.Context, req runtime.LoadTeamRequest) (*teamloader.LoadResult, error) { |
| 727 | opts := append(loaderdefaults.Opts(), teamloader.WithModelOverrides(req.ModelOverrides)) |
no test coverage detected