Skip to content

Enhanced PR Review System - #2

Open
blacksyan wants to merge 43 commits into
devfrom
enhance/pr-agent-capabilities-v2
Open

Enhanced PR Review System#2
blacksyan wants to merge 43 commits into
devfrom
enhance/pr-agent-capabilities-v2

Conversation

@blacksyan

@blacksyan blacksyan commented Jan 2, 2026

Copy link
Copy Markdown

Enhanced PR Review System

A context-aware, multi-phase AI code review system for GitHub PRs using OpenCode.


🎯 Problem & Solution

The Problem

Current OpenCode capabilities for PR reviews have some limitations:

  • Limited project context - Reviews may lack deep alignment with specific project conventions.
  • Strict enforcement everywhere - High friction for experimental or non-protected branches.
  • One-way feedback - Minimal opportunity to clarify intent before the AI issues a decision.
  • Single-line focus - Feedback is often isolated to single lines, making multi-line refactors harder to suggest.

The Solution

This system enhances OpenCode with a context-aware review layer that:

  • Reads your actual code using OpenCode's native file-reading power.
  • Asks questions first before reviewing (Phase 1 → Phase 2).
  • Branch-aware strictness - Strict for main, relaxed for feature branches.
  • Multi-line committable suggestions - One-click apply for complex fixes.

📋 Table of Contents


✨ Features

🔍 OpenCode File Reading

The system leverages OpenCode's native capability to read files from your repository. You can verify this in the GitHub Actions logs:

Sending message to opencode...

|   Read     {"filePath":"src/services/VideoPostLikeService.js"}
|   Read     {"filePath":"src/services/VideoPostCommentService.js"}
|   Read     {"filePath":"src/models/VideoPost.js"}

This ensures the AI analyzes your actual source files to understand the broader context beyond just the diff.

🎯 Multi-Phase Review Flow

Phase Trigger Purpose
Phase 1 /oc AI asks clarifying questions to understand your intent
Phase 2 Answer Context-aware review based on your detailed answers
Direct Review /oc! Skip questions and get an immediate review
Recheck /oc recheck Request a re-review after you've pushed fixes

🎛 Branch-Aware Strictness

The review logic adapts to your target branch:

Target Branch Behavior
main, staging, production Strict - Missing concurrent dependencies (e.g., migrations vs models) are marked as FAIL and block the PR.
feature/*, trial/*, develop Relaxed - Missing dependencies are marked as INFO/SKIPPED. The AI can APPROVE while providing reminders for later.

Configure via: STRICT_REVIEW_BRANCHES=main,staging,production

📝 Multi-Line Committable Suggestions

Comments can now span multiple lines (e.g., lines 42-65) and include GitHub's suggestion syntax:

📍 Lines 42-65 in src/services/MyService.js

⚠️ Warning: This block needs a transaction wrapper to ensure atomicity.

```suggestion
async myMethod() {
  return transaction(async (t) => {
    // your fixed code here
  });
}
```

Users can click "Commit suggestion" directly on GitHub to apply the fix.

📚 Context Injection System

  • 89+ auto-detected patterns to trigger relevant context loading.
  • RAKAMIND.md / OPENCODE.md support - Define per-repo AI rules.
  • Concurrent implementation checks - Automatically detects missing cross-layer logic (model-migration-sync, service-transaction-checks).

🔄 How It Works

  1. User Triggers Review: Comment /oc on a PR.
  2. OpenCode Reads Files: The system identifies relevant files and reads them via OpenCode's tools.
  3. Phase 1 (Clarification): AI asks questions if information is missing.
  4. Phase 2 (Review): AI issues a structured review with a checklist, inline comments, and a decision.

🚀 Quick Start

1. Add Workflow to Your Repo

Create .github/workflows/opencode.yml in your target repository:

name: opencode

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]

jobs:
  opencode:
    if: |
      (github.event_name == 'issue_comment' && github.event.issue.pull_request) ||
      github.event_name == 'pull_request_review_comment'
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4
        with:
          ref: enhance/pr-agent-capabilities-v2
          repository: rakamindev/opencode

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: cd packages/opencode && bun install

      - name: Run opencode
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GOOGLE_GENERATIVE_AI_API_KEY: ${{ secrets.GOOGLE_GENERATIVE_AI_API_KEY }}
          STRICT_REVIEW_BRANCHES: main,staging,production
        run: |
          cd packages/opencode && bun run build
          cd packages/opencode && ./bin/opencode github run

2. Add Required Secrets

In your repo Settings → Secrets → Actions:

  • GOOGLE_GENERATIVE_AI_API_KEY: Your Gemini API key.
  • GITHUB_TOKEN: Automatically handled if using default permissions.

3. Add Project Rules (Optional)

Create RAKAMIND.md (or OPENCODE.md) in your repo root to guide the AI:

# RAKAMIND.md

## Project
- Node.js 20 / Express 4 / Sequelize 6
- Layers: controllers → services → models

## Code Rules
- Services: multi-step writes MUST use transactions.
- Models: use snake_case, paranoid: true.

📊 Test Results

Verified on rakamindev/paragon-api:

  • PR #414 (Migrations only): APPROVE ✅ - Correctly skipped models as out of scope for a feature branch.
  • PR #415 (Models): REQUEST_CHANGES ❌ - Found missing hooks in model files.
  • PR #416 (Services): REQUEST_CHANGES ❌ - Identified missing transactions and provided multi-line committable suggestions.

📝 Changelog

  • Multi-line suggestions - Better UX with committable code blocks.
  • Branch-aware strictness - Reduced friction for development branches.
  • RAKAMIND.md support - Custom per-repo instructions.
  • Context injection - Automated deep learning of the codebase.
  • Phase 1/2 flow - Improved accuracy through developer collaboration.

@blacksyan
blacksyan marked this pull request as ready for review January 3, 2026 14:39
- Disabled auto-commit and push after AI response
- Any file changes made by AI are discarded (git checkout -- .)
- OpenCode now only reviews and posts comments, never modifies code
- Parse Git patch to extract valid line ranges from @@ hunk headers
- Filter comments to only include lines present in the diff
- Add prompt guidance: only comment on added/modified lines
- Invalid comments are shown in summary instead of causing API errors
- Prevents GitHub API 422 'Line could not be resolved' errors
- Accepts null from LLM but transforms to undefined for cleaner type
- Type is now 'string | undefined' instead of 'string | null | undefined'
…her PR

- Added schema definitions to list of skippable concurrent deps
- Explicitly states static schema(), column definitions are PART OF migrations
- Updated all 3 prompts consistently
- AI now correctly marks these as Skipped instead of Fail
- Added prompt rule: suggestion must be RAW CODE ONLY
- Added Zod transform to strip markdown code blocks if AI includes them
- Prevents 'Unterminated string' JSON parse errors
AI sometimes uses 'warning' or other strings for passed field.
Added defensive preprocess to coerce non-boolean values to null.
- Re-implemented line-range validation against PR diff (fixes 422 'Line could not be resolved')
- Added defensive schema handling to normalize 'null' suggestions and 'warning' statuses
- Added markdown sanitization for code suggestions to prevent JSON parse errors
- Enhanced non-strict branch rules to explicitly skip schema/model internals when handled in concurrent PRs
- Updated Recheck, Direct Review, and Phase 2 prompts
- Explicitly instructs AI to mark Previous Feedback as 'Skipped' (passed: null) if fix is in a separate PR
- Strengthens the non-strict override over the global 'track previous issues' rule
- Upgraded regex to handle escaped quotes: (?:[^"\\]|\\.)*
- Applies to ALL string fields, not just 'suggestion'
- Handles both escaped (\n) and actual newlines in code blocks
- Implemented character-aware JSON repair scanner
- Automatically escapes unescaped control chars (newlines) in strings
- Strips hallucinated triple-backtick blocks
- Normalizes trailing commas in objects/arrays
- This provides absolute resilience against common LLM formatting hallucinations.
- Added generateObject fallback when JSON repair fails
- Uses Gemini's responseMimeType: 'application/json' for bulletproof JSON extraction
- Two-tier approach: fast repair first, native structured output as fallback
- This implements Google's structured output spec per user request
Previous commit 5e3212a added these restrictions but they were
accidentally removed in a later refactor. Re-adding to ensure
AI cannot modify files during PR reviews.
- Zod transforms/preprocess cannot be represented in JSON Schema
- Added *Raw versions of schemas without transforms for generateObject
- generateObject uses ReviewOutputRaw, then validates with full schema
- Fixes: 'Transforms cannot be represented in JSON Schema' error
- Extracted repairAndParseJson to util/json-repair.ts with Enhanced Smart Quote logic
- Extracted parsePatchForValidLines to util/git-diff.ts
- Added comprehensive unit tests (github-review.test.ts) covering:
  - JSON repair edge cases (trailing commas, unescaped quotes/newlines, markdown blocks)
  - Diff parsing (hunks, context lines, deletions)
- Verified all tests pass
- Extracted renderReviewMarkdown and filterCommentsByDiff to github-review-logic.ts
- Achieved 100% line coverage for all extracted PR review modules
- Simplified github.ts orchestration logic
- Added unit tests for markdown rendering and diff-based filtering
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants