Skip to content

Feat: Implement worker for follow-up PR changes #41

Closed

Description

@integry

General Goal

This is the second stage of implementing follow-up changes from PR comments. This issue focuses on creating a new worker task to handle these requests.

This issue is dependent on Issue #40 . Check the last commit for current implementation details.

Task Description

A new worker processor needs to be created in src/worker.js to handle jobs of type processPullRequestComment. This worker will check out the existing PR branch, use an LLM to generate code changes based on the comment, and then commit and push these changes to the same branch.

Implementation Steps

  1. Update src/worker.js:

    • Add a new case in the job processing logic to handle processPullRequestComment jobs. You might want to refactor the main createWorker call to handle different job types more elegantly.
    • Create a new async function, processPullRequestCommentJob(job).
  2. Implement processPullRequestCommentJob:

    • This function should perform the following steps:
      1. Extract pullRequestNumber, commentBody, branchName, etc., from the job data.
      2. Clone/update the repository and check out the existing branchName. Do not create a new branch.
      3. Generate a new, specific prompt for the LLM. This prompt should instruct the agent to:
        • Analyze the existing code on the branch.
        • Implement the changes requested in the commentBody.
        • Not create a new pull request.
        • The prompt could be something like: \"Given the existing changes on this branch, please implement the following additional request: {commentBody}. Commit the changes to the current branch.\"
      4. Execute claudeService.executeClaudeCode with this new prompt. The LLM to use can be determined from the llm field in the job data.
      5. After the agent runs, check for changes in the worktree.
      6. If there are changes, create a new commit and push it to the branchName. The commit message should reference the original issue and the follow-up comment.
      7. Add a comment to the pull request to confirm that the follow-up changes have been applied, referencing the new commit hash.

Acceptance Criteria

  • The worker correctly processes processPullRequestComment jobs.
  • The correct PR branch is checked out.
  • The LLM is called with the correct prompt and implements the requested changes.
  • A new commit is successfully pushed to the PR branch.
  • A confirmation comment is posted on the PR.

Activity

  1. integry commented on Jul 21, 2025

    @integry
    OwnerAuthor

    Suggested Implementation for `src/worker.js`

    Here's a high-level structure for the new worker processor. This would be added to src/worker.js and integrated into the main worker logic.

    // src/worker.js
    
    // ... existing imports
    
    /**
     * Processes a follow-up change request from a PR comment.
     * @param {import('bullmq').Job} job - The job to process
     * @returns {Promise<Object>} Processing result
     */
    async function processPullRequestCommentJob(job) {
        const {
            pullRequestNumber,
            commentBody,
            branchName,
            repoOwner,
            repoName,
            llm, // The requested LLM model
            correlationId
        } = job.data;
        const correlatedLogger = logger.withCorrelation(correlationId);
        correlatedLogger.info({ pullRequestNumber, branchName }, 'Processing PR comment job...');
    
        // You will need to get an authenticated octokit instance here
        const octokit = await getAuthenticatedOctokit();
        const githubToken = await octokit.auth();
        const repoUrl = getRepoUrl({ repoOwner, repoName });
    
        let localRepoPath;
        let worktreeInfo;
    
        try {
            // 1. Set up the Git environment
            localRepoPath = await ensureRepoCloned(repoUrl, repoOwner, repoName, githubToken.token);
    
            // For simplicity, we can create a worktree from the existing branch
            // This keeps the logic similar to the issue processing flow.
            worktreeInfo = await createWorktreeForIssue(
                localRepoPath,
                pullRequestNumber, // Use PR number for uniqueness
                'follow-up',
                repoOwner,
                repoName,
                branchName // Use the existing branch as the base
            );
    
    
            // 2. Generate the prompt for the LLM
            const prompt = `
                The current branch already contains changes for a pull request.
                A user has requested the following follow-up change.
                Please implement it.
    
                Request: \"\${commentBody}\"
    
                Instructions:
                - Analyze the existing code on the branch.
                - Implement the requested changes.
                - Commit your changes to the current branch.
                - DO NOT create a new pull request.
            `;
    
            // 3. Execute Claude
            const claudeResult = await executeClaudeCode({
                worktreePath: worktreeInfo.worktreePath,
                issueRef: { number: pullRequestNumber, repoOwner, repoName },
                githubToken: githubToken.token,
                customPrompt: prompt,
                // You might want to pass the 'llm' parameter to executeClaudeCode
            });
    
            if (!claudeResult.success) {
                throw new Error('Claude execution failed for follow-up task.');
            }
    
            // 4. Commit and Push
            const commitResult = await commitChanges(
                worktreeInfo.worktreePath,
                `feat(ai): Apply follow-up changes from PR comment`,
                { name: 'GitFix', email: '[email protected]' },
                pullRequestNumber,
                'Follow-up changes'
            );
    
            if (commitResult) {
                await pushBranch(worktreeInfo.worktreePath, worktreeInfo.branchName, {
                    repoUrl,
                    authToken: githubToken.token
                });
    
                // 5. Add a confirmation comment to the PR
                await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
                    owner: repoOwner,
                    repo: repoName,
                    issue_number: pullRequestNumber,
                    body: `Applied the requested follow-up changes in commit \${commitResult.commitHash}.`,
                });
            } else {
                 await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
                    owner: repoOwner,
                    repo: repoName,
                    issue_number: pullRequestNumber,
                    body: 'I analyzed the follow-up request, but no code changes were necessary.',
                });
            }
    
            return { status: 'complete', commit: commitResult?.commitHash };
    
        } catch (error) {
            handleError(error, 'Failed to process PR comment job', { correlationId });
            // Potentially add a comment to the PR indicating failure
            await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
                owner: repoOwner,
                repo: repoName,
                issue_number: pullRequestNumber,
                body: `I tried to apply the requested follow-up changes, but an error occurred: \${error.message}`,
            });
            throw error;
        } finally {
            if (localRepoPath && worktreeInfo) {
                await cleanupWorktree(localRepoPath, worktreeInfo.worktreePath, worktreeInfo.branchName);
            }
        }
    }
    
    // In startWorker, you'd update the createWorker call:
    // const worker = createWorker(GITHUB_ISSUE_QUEUE_NAME, async (job) => {
    //     if (job.name === 'processGitHubIssue') {
    //         return processGitHubIssueJob(job);
    //     } else if (job.name === 'processPullRequestComment') {
    //         return processPullRequestCommentJob(job);
    //     }
    // });
  2. propr-dev commented on Jul 22, 2025

    @propr-dev
    Contributor

    🤖 AI processing has started for this issue using opus model.

    I'll analyze the problem and work on a solution. This may take a few minutes.

    Processing Details:

    • Model: opus
    • Branch: ai-fix/41-feat-implement-worker-for-20250722-1039-opus-o9c
    • Base Branch: main
    • Worktree: issue-41-20250722-1039-opus-o9c
  3. added a commit that references this issue on Jul 22, 2025
  4. added a commit that references this issue on Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions