Class: XAeonAgents::Agents::TaskStarterAgent

Inherits:
ComposableAgents::Agent
  • Object
show all
Includes:
XAeonAgents::AgentDefaults
Defined in:
lib/x_aeon_agents/agents/task_starter_agent.rb

Overview

Agent responsible for opening a new git worktree for a task.

Defined Under Namespace

Classes: TaskStarterError

Instance Method Summary collapse

Methods included from XAeonAgents::AgentDefaults

#new_agent, prepended, singleton_session_id

Instance Method Details

#input_artifacts_contractsHash{Symbol => Object}

Define input artifacts contracts

Returns:

  • (Hash{Symbol => Object})

    Set of input artifacts description, per artifact name



16
17
18
# File 'lib/x_aeon_agents/agents/task_starter_agent.rb', line 16

def input_artifacts_contracts
  { branch_name: 'The name of the git branch to create worktree for' }
end

#output_artifacts_contractsHash{Symbol => Object}

Define output artifacts contracts

Returns:

  • (Hash{Symbol => Object})

    Set of output artifacts description, per artifact name



23
24
25
# File 'lib/x_aeon_agents/agents/task_starter_agent.rb', line 23

def output_artifacts_contracts
  { worktree_dir: 'The directory where the worktree was created' }
end

#run(branch_name:) ⇒ Hash{Symbol => Object}

Execute the agent to open a new git worktree for a feature branch.

Parameters:

  • branch_name (String)

    Name of the git branch to create worktree for

Returns:

  • (Hash{Symbol => Object})

    Output artifacts content



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/x_aeon_agents/agents/task_starter_agent.rb', line 31

def run(branch_name:)
  dir = ".worktrees/#{branch_name.tr('/', '_')}"
  puts "Setting worktree #{dir} to work on branch #{branch_name}..."
  # Create the branch if it does not exist (without checking it out)
  Helpers.git.branch(branch_name).create unless Helpers.git.branches.any? { |branch| branch.name == branch_name }
  # Create the git worktree only if it does not exist yet (idempotent)
  if File.directory?(dir)
    # The directory already exists: it must be a git worktree for the requested branch.
    # A git worktree has a `.git` file (a gitdir pointer), not a `.git` directory.
    unless File.file?(File.join(dir, '.git'))
      raise TaskStarterError, <<~EO_MSG.strip
        Directory '#{dir}' already exists but is not a git worktree (no '.git' pointer file found).
        Please remove it or choose a different branch name.
      EO_MSG
    end

    # The directory is a worktree: ensure it tracks the requested branch.
    worktree_branch = Git.open(dir).current_branch
    if worktree_branch != branch_name
      raise TaskStarterError, <<~EO_MSG.strip
        Directory '#{dir}' is already a git worktree on branch '#{worktree_branch}', which differs from the requested branch '#{branch_name}'.
        Please choose a different branch name or remove the existing worktree.
      EO_MSG
    end
  else
    # Call git worktree add on existing branches only
    Helpers.git.lib.worktree_add(dir, branch_name)
  end
  # Push to remote if branch doesn't exist there yet
  Helpers.git.push(Helpers.github_remote, branch_name, set_upstream: true)
  Helpers.run_cmd("VSCodium.exe \"#{dir}\"")
  { worktree_dir: dir }
end