Claude-Agent-Code-Review

 view release on metacpan or  search on metacpan

lib/Claude/Agent/Code/Review.pm  view on Meta::CPAN


    return <<"END_PROMPT";
Review the following diff for issues:

```diff
$diff
```

Categories to check: $categories
Minimum severity to report: $severity
$focus

For each issue found in the changed code, provide:
- File path and line number
- Severity (critical, high, medium, low, info)
- Category (bugs, security, style, performance, maintainability)
- Clear description of the problem
- Suggested fix when possible

Return your findings as a structured JSON response.
END_PROMPT
}

# Internal: Run the review query
async sub _run_review {
    my ($prompt, $options, $loop) = @_;

    # Create MCP server with review tools
    my $tools_server = Claude::Agent::Code::Review::Tools->create_server();

    # Build the JSON schema for structured output
    my $review_schema = _get_review_schema();

    # Build Claude options
    my %claude_args = (
        allowed_tools => [
            'Read', 'Glob', 'Grep',
            @{$tools_server->tool_names},
        ],
        mcp_servers     => { review => $tools_server },
        permission_mode => $options->permission_mode,  # Uses Options default ('default')
        system_prompt   => _get_system_prompt($options),
        output_format   => {
            type   => 'json_schema',
            schema => $review_schema,
        },
    );
    $claude_args{model} = $options->model if defined $options->model;

    my $claude_options = Claude::Agent::Options->new(%claude_args);

    # Run query
    my $iter = query(
        prompt  => $prompt,
        options => $claude_options,
        loop    => $loop,
    );

    # Collect result asynchronously with iteration limit
    my $result;
    my $max_iterations = 1000;  # Prevent infinite loops
    my $iterations = 0;

    while (my $msg = await $iter->next_async) {
        $iterations++;
        if ($msg->isa('Claude::Agent::Message::Result')) {
            $result = $msg;
            last;
        }
        if ($iterations >= $max_iterations) {
            $iter->cleanup();  # Cleanup SDK server sockets
            return Claude::Agent::Code::Review::Report->new(
                summary => 'Review timed out: exceeded maximum iterations',
                issues  => [],
            );
        }
    }

    # Cleanup SDK server sockets before returning
    $iter->cleanup();

    # Handle case where loop exits without finding a Result message
    unless (defined $result) {
        return Claude::Agent::Code::Review::Report->new(
            summary => 'Review failed - no result received from Claude',
            issues  => [],
        );
    }

    return _build_report($result, $options);
}

# Internal: Get system prompt for review
sub _get_system_prompt {
    my ($options) = @_;

    my $categories = join(', ', @{$options->categories});
    my $focus = '';
    if ($options->has_focus_areas && @{$options->focus_areas}) {
        $focus = "\n\nPay special attention to: " . join(', ', @{$options->focus_areas});
    }

    return <<"END_PROMPT";
You are an expert code reviewer. Your task is to systematically analyze code for issues.

IMPORTANT: Follow this SYSTEMATIC methodology for consistent, reproducible results:

1. ENUMERATE: First, list ALL files to be reviewed using Glob
2. FOR EACH FILE: Read the entire file, then check for issues in this EXACT order:
   a. Security issues (injection, XSS, auth, data exposure)
   b. Bugs (logic errors, null handling, race conditions, off-by-one)
   c. Performance (inefficient algorithms, unnecessary operations)
   d. Style (naming, organization) - only report if clearly problematic
   e. Maintainability (complexity, duplication)
3. VERIFY: Before reporting an issue, check the surrounding code context to avoid false positives
4. SKIP: Do not report issues about documented limitations (comments with "Note:", "TODO:", etc.)

Categories to check: $categories

Severity guidelines:
- critical: Security vulnerabilities, data loss, crashes
- high: Bugs that will cause incorrect behavior
- medium: Potential bugs, minor security issues
- low: Code quality issues
- info: Suggestions for improvement

For each issue:
- Provide exact file path and line number
- Explain the actual problem (not theoretical)
- Suggest a concrete fix
- Only report issues you are confident about$focus
END_PROMPT
}



( run in 1.461 second using v1.01-cache-2.11-cpan-71847e10f99 )