> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/harness-ai/untitled-1/use-harness-ai/ai-agents/code-agent.md).

# Code Quality Agents

Harness provides three AI-powered agents focused on code quality: **Code Review** for intelligent PR feedback, **Code Coverage** for automated test generation, and **Autofix** for automatic CI failure remediation. These agents use Claude AI to analyze code, generate improvements, and submit changes via pull requests.

***

### Code Review Agent <a href="#code-review-agent" id="code-review-agent"></a>

The Code Review agent automatically reviews code changes in pull requests and posts intelligent feedback directly to the PR. It uses a three-stage process for comprehensive analysis.

#### How It Works <a href="#how-it-works" id="how-it-works"></a>

1. **Review Prompt Generation** — An analysis agent examines the PR diff and generates a targeted review prompt.
2. **AI Code Review** — Claude AI (up to 50 iterations) performs comprehensive code review following the generated prompt.
3. **Comment Posting** — Review comments are posted directly to the PR via Harness SCM.

#### Key Inputs <a href="#key-inputs" id="key-inputs"></a>

| Input          | Type   | Description                        |
| -------------- | ------ | ---------------------------------- |
| `anthropicKey` | secret | Anthropic API key for Claude AI    |
| `harnessKey`   | secret | Harness API key for SCM operations |
| `repo`         | string | Repository name                    |
| `pullReq`      | string | Pull request number                |

#### Pipeline Configuration <a href="#pipeline-configuration" id="pipeline-configuration"></a>

```yaml
pipeline:
  clone:
    depth: 1000
    ref:
      type: pull-request
      number: <+inputs.pullReq>
    repo: <+inputs.repo>
  stages:
    - name: review
      steps:
        - name: review_prompt_generation_agent
          run:
            container:
              image: abhinavharness/drone-ai-review:latest
            with:
              output_file: /harness/.../task.txt
              review_output_file: /harness/.../review.json
              working_directory: /harness
        - name: coding_agent
          run:
            container:
              image: anewdocker25/mydockerhub:coding-agent
            with:
              detailed_logging: "true"
              max_iterations: "50"
              task_file_path: /harness/.../task.txt
              working_directory: /harness
              show_diff: "false"
            env:
              ANTHROPIC_API_KEY: <+inputs.anthropicKey>
        - name: post_comments
          run:
            container:
              image: abhinavharness/comment-plugin:latest
            with:
              comments_file: /harness/.../review.json
              repo: <+inputs.repo>
              pr_number: <+inputs.pullReq>
            env:
              TOKEN: <+inputs.harnessKey>
      platform:
        os: linux
        arch: arm64
  inputs:
    anthropicKey:
      type: secret
      default: account.autofix_anthropic_api_key
    harnessKey:
      type: secret
      default: account.harness_api_key
    repo:
      type: string
      required: true
    pullReq:
      type: string
      required: true
  name: codereview
```

{% hint style="info" %}
**DEEP CLONE FOR CONTEXT**

The Code Review agent uses a clone depth of 1000 commits to give the AI full history context when analyzing the PR diff.
{% endhint %}

***

### Code Coverage Agent <a href="#code-coverage-agent" id="code-coverage-agent"></a>

The Code Coverage agent analyzes codebases and generates comprehensive unit tests to achieve 90%+ code coverage. It creates a detailed `COVERAGE.md` report, pushes changes to a new branch, and posts coverage results as a PR comment.

#### How It Works <a href="#how-it-works" id="how-it-works"></a>

1. **Codebase Analysis** — Scans source files and identifies coverage gaps.
2. **Test Generation** — Claude AI (up to 300 iterations) generates comprehensive unit tests.
3. **Coverage Verification** — Runs tests and verifies coverage targets are met.
4. **Report Generation** — Creates a `COVERAGE.md` with per-file coverage breakdown.
5. **PR Creation** — Pushes to a unique branch and creates a PR with the coverage report as a comment.

#### Coverage Targets <a href="#coverage-targets" id="coverage-targets"></a>

* 90% overall code coverage target
* 80% minimum per-file coverage
* Auto-posted coverage reports on PR
* Go language support with verification enabled

#### Key Inputs <a href="#key-inputs" id="key-inputs"></a>

| Input          | Type      | Description                         |
| -------------- | --------- | ----------------------------------- |
| `llmConnector` | connector | LLM connector for AI operations     |
| `harnessKey`   | secret    | Harness API key                     |
| `gitConnector` | connector | Git connector for repository access |
| `repo`         | string    | Repository name                     |
| `branch`       | string    | Target branch                       |

#### Pipeline Configuration <a href="#pipeline-configuration" id="pipeline-configuration"></a>

```yaml
pipeline:
  clone:
    depth: 1
    ref:
      name: <+inputs.branch>
      type: branch
    repo: <+inputs.repo>
    connector: "<+inputs.gitConnector != null ? inputs.gitConnector.id : ''>"
  stages:
    - name: code-coverage
      steps:
        - name: coding_agent
          run:
            container:
              image: himanshu6956/codecov:coding-agent-with-go
            with:
              detailed_logging: "true"
              max_iterations: "300"
              working_directory: /harness
              show_diff: "false"
              prompt: "Analyze the current codebase and identify test coverage..."
              code_coverage: "true"
              verify: "true"
            env:
              ANTHROPIC_API_KEY: <+inputs.llmConnector.token>
        - name: show_git_diff
          run:
            shell: bash
            script: |-
              git add -A
              git diff --cached
        - name: fix_env_variables
          run:
            shell: bash
            script: |-
              # Detect SCM provider and set credentials
              # Exports SCM_PROVIDER, TOKEN, NETRC_USERNAME, etc.
            env:
              HARNESS_API_KEY: <+inputs.harnessKey>
              SCM_TOKEN: <+inputs.gitConnector.token>
        - name: push_and_create_pr
          run:
            container:
              image: himanshu6956/create-pr-plugin:latest
            env:
              PLUGIN_SCM_PROVIDER: <+pipeline.stages.codecoverage_1.steps.fix_env_variables_1.output.outputVariables.SCM_PROVIDER>
              PLUGIN_TOKEN: <+pipeline.stages.codecoverage_1.steps.fix_env_variables_1.output.outputVariables.TOKEN>
              PLUGIN_REPO: ${{inputs.repo}}
              PLUGIN_SOURCE_BRANCH: ${{inputs.branch}}
              PLUGIN_BRANCH_SUFFIX: code-coverage-agent
              PLUGIN_COMMIT_MESSAGE: "Code coverage: automated test additions by Harness AI"
              PLUGIN_CREATE_PR: "true"
              PLUGIN_PR_TITLE: "Code Coverage: Automated coverage increase by Harness AI"
        - name: prepare_coverage_comment
          run:
            shell: bash
            script: |-
              # Search for COVERAGE.md and prepare PR comment body
              COVERAGE_FILE=$(find . -name "COVERAGE.md" | head -n 1)
              if [ -n "$COVERAGE_FILE" ]; then
                COMMENT_FILE="/harness/coverage_comment_body.txt"
                echo "## Code Coverage Report" > "$COMMENT_FILE"
                cat "$COVERAGE_FILE" >> "$COMMENT_FILE"
                echo "COVERAGE_COMMENT_FILE=$COMMENT_FILE" >> $DRONE_OUTPUT
              fi
        - name: post_coverage_comment
          run:
            container:
              image: abhinavharness/comment-plugin:latest
            shell: sh
            script: |-
              # Post COVERAGE.md content as PR comment
            env:
              PLUGIN_REPO: ${{inputs.repo}}
              PLUGIN_PR_NUMBER: <+pipeline.stages.codecoverage_1.steps.push_and_create_pr_1.output.outputVariables.PR_NUMBER>
      platform:
        os: linux
        arch: arm64
  inputs:
    llmConnector:
      type: connector
    harnessKey:
      type: secret
      default: harness_api_key
    gitConnector:
      type: connector
    repo:
      type: string
    branch:
      type: string
      default: main
      description: The branch to clone from and create PR against (defaults to main)
```

{% hint style="info" %}
**THOROUGH ANALYSIS**

The Code Coverage agent uses up to 300 AI iterations — the highest of any agent — making it well-suited for deeply analyzing complex codebases and generating meaningful tests rather than superficial coverage.
{% endhint %}

***

### Autofix Agent <a href="#autofix-agent" id="autofix-agent"></a>

The Autofix agent automatically diagnoses and fixes CI pipeline failures. When a CI build fails, the agent fetches execution logs, generates a root-cause diagnosis, applies code fixes using Claude AI, and creates a PR with the solution.

#### How It Works <a href="#how-it-works" id="how-it-works"></a>

1. **Log Analysis** — Fetches execution logs from the failed CI pipeline.
2. **Diagnosis** — A remediation agent analyzes the failure and identifies the root cause.
3. **Fix Generation** — A coding agent (Claude AI, up to 50 iterations) applies code changes to fix the issue.
4. **Branch & PR** — Pushes fixes to an `ai-autofix` branch and creates a pull request.

#### Two-Stage AI Process <a href="#two-stage-ai-process" id="two-stage-ai-process"></a>

The Autofix agent uses two specialized containers working in sequence: a **Remediation Agent** that specializes in diagnosing CI failures from logs and error messages, and a **Coding Agent** that takes the diagnosis and applies targeted code fixes.

#### Key Inputs <a href="#key-inputs" id="key-inputs"></a>

| Input          | Type      | Description                         |
| -------------- | --------- | ----------------------------------- |
| `llmConnector` | connector | LLM connector for AI operations     |
| `harnessKey`   | secret    | Harness API key                     |
| `gitConnector` | connector | Git connector for repository access |
| `repo`         | string    | Repository name                     |
| `branch`       | string    | Target branch                       |
| `executionId`  | string    | Failed pipeline execution ID        |

#### Pipeline Configuration <a href="#pipeline-configuration" id="pipeline-configuration"></a>

```yaml
pipeline:
  clone:
    depth: 1
    ref:
      name: <+inputs.branch>
      type: branch
    repo: <+inputs.repo>
    connector: "<+inputs.gitConnector != null ? inputs.gitConnector.id : ''>"
  stages:
    - name: autofix
      steps:
        - name: remediation_agent
          run:
            container:
              image: anewdocker25/mydockerhub:remediation-agent
            with:
              detailed_logging: "true"
              harness_api_key: <+inputs.harnessKey>
              output_dir: /harness/.../autofix
              working_directory: /harness
              harness_execution_id: <+inputs.executionId>
            env:
              ANTHROPIC_API_KEY: <+inputs.llmConnector.token>
        - name: coding_agent
          run:
            container:
              image: anewdocker25/mydockerhub:coding-agent
            with:
              detailed_logging: "true"
              max_iterations: "50"
              task_file_path: /harness/.../autofix/task.txt
              working_directory: /harness
              show_diff: "false"
            env:
              ANTHROPIC_API_KEY: <+inputs.llmConnector.token>
        - name: show_git_diff
          run:
            shell: bash
            script: |-
              git add -A
              git diff --cached
        - name: fix_env_variables
          run:
            shell: bash
            script: |-
              # Detect SCM provider and set credentials
              # Exports SCM_PROVIDER, TOKEN, NETRC_USERNAME, etc.
            env:
              HARNESS_API_KEY: <+inputs.harnessKey>
              SCM_TOKEN: <+inputs.gitConnector.token>
        - name: push_and_create_pr
          run:
            container:
              image: himanshu6956/create-pr-plugin:latest
            env:
              PLUGIN_SCM_PROVIDER: <+pipeline.stages.autofix_1.steps.fix_env_variables_1.output.outputVariables.SCM_PROVIDER>
              PLUGIN_TOKEN: <+pipeline.stages.autofix_1.steps.fix_env_variables_1.output.outputVariables.TOKEN>
              PLUGIN_REPO: ${{inputs.repo}}
              PLUGIN_SOURCE_BRANCH: ${{inputs.branch}}
              PLUGIN_BRANCH_SUFFIX: ai-autofix
              PLUGIN_COMMIT_MESSAGE: "Autofix: harness-auto-fix created this fix"
              PLUGIN_CREATE_PR: "true"
              PLUGIN_PR_TITLE: "Autofix: AI automated fixes by Harness"
      platform:
        os: linux
        arch: arm64
  inputs:
    llmConnector:
      type: connector
    harnessKey:
      type: secret
      default: harness_api_key
    gitConnector:
      type: connector
    repo:
      type: string
    branch:
      type: string
      default: main
    executionId:
      type: string
```

{% hint style="info" %}
**SELF-HEALING WORKFLOWS**

Configure the Autofix agent to trigger automatically when a CI pipeline fails for a seamless self-healing workflow. Connect it to pipeline failure notifications for hands-free remediation.
{% endhint %}
