Skip to content
27 changes: 17 additions & 10 deletions apps/frontend/src/main/ipc-handlers/task/execution-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,12 @@ export function registerTaskExecutionHandlers(
console.warn(`[TASK_START] Reset ${resetResult.resetCount} stuck subtask(s) before starting`);
}

// Start file watcher for this task
// Start file watcher for this task - use worktree path if available
const specsBaseDir = getSpecsDir(project.autoBuildPath);
const specDir = path.join(
project.path,
specsBaseDir,
task.specId
);
const worktreePath = findTaskWorktree(project.path, task.specId);
const specDir = worktreePath
? path.join(worktreePath, specsBaseDir, task.specId)
: path.join(project.path, specsBaseDir, task.specId);
Comment on lines 208 to +212
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic to determine the specDir by checking for a worktree is duplicated in three places in this file (here, lines 712-716, and lines 1153-1157). To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, this should be extracted into a single helper function.

For example, you could create a function like getSpecDirForWatcher:

function getSpecDirForWatcher(project: Project, task: Task): string {
  const specsBaseDir = getSpecsDir(project.autoBuildPath);
  const worktreePath = findTaskWorktree(project.path, task.specId);
  const basePath = worktreePath || project.path;
  return path.join(basePath, specsBaseDir, task.specId);
}

And then call it in all three places:
const specDir = getSpecDirForWatcher(project, task);

This makes the code cleaner and avoids potential bugs if this logic needs to be updated in the future.

fileWatcher.watch(taskId, specDir);

// Check if spec.md exists (indicates spec creation was already done or in progress)
Expand Down Expand Up @@ -709,8 +708,13 @@ export function registerTaskExecutionHandlers(
console.warn(`[TASK_UPDATE_STATUS] Reset ${resetResult.resetCount} stuck subtask(s) before starting`);
}

// Start file watcher for this task
fileWatcher.watch(taskId, specDir);
// Start file watcher for this task - use worktree path if available
const specsBaseDirForWatcher = getSpecsDir(project.autoBuildPath);
const worktreePath = findTaskWorktree(project.path, task.specId);
const specDirForWatcher = worktreePath
? path.join(worktreePath, specsBaseDirForWatcher, task.specId)
: path.join(project.path, specsBaseDirForWatcher, task.specId);
fileWatcher.watch(taskId, specDirForWatcher);
Comment on lines +716 to +717

This comment was marked as outdated.


// Check if spec.md exists
const specFilePath = path.join(specDir, AUTO_BUILD_PATHS.SPEC_FILE);

This comment was marked as outdated.

Expand Down Expand Up @@ -1145,9 +1149,12 @@ export function registerTaskExecutionHandlers(
}

// Start the task execution
// Start file watcher for this task
// Start file watcher for this task - use worktree path if available
const specsBaseDir = getSpecsDir(project.autoBuildPath);
const specDirForWatcher = path.join(project.path, specsBaseDir, task.specId);
const worktreePath = findTaskWorktree(project.path, task.specId);
const specDirForWatcher = worktreePath
? path.join(worktreePath, specsBaseDir, task.specId)
: path.join(project.path, specsBaseDir, task.specId);
fileWatcher.watch(taskId, specDirForWatcher);

// Check if spec.md exists to determine whether to run spec creation or task execution
Expand Down
Loading