# mehr implement

Implement the specifications for the active task.

## Synopsis

```bash
mehr implement [flags]
```

**Aliases:** `impl`, `i`

## Description

The `implement` command runs the implementation phase where the AI agent:

1. Reads all specification files in the work directory
2. Reviews notes and context
3. Generates or modifies code
4. Creates a checkpoint for undo support

**Prerequisite:** At least one specification file must exist. Run `mehr plan` first.

## Flags

| Flag                | Short | Type   | Default | Description                                                                            |
|---------------------|-------|--------|---------|----------------------------------------------------------------------------------------|
| `--dry-run`         | `-n`  | bool   | false   | Preview changes without applying                                                       |
| `--verbose`         | `-v`  | bool   | false   | Show agent output in real-time                                                         |
| `--agent-implement` |       | string |         | Override agent for implementation                                                      |
| `--optimize`        |       | bool   | false   | Optimize prompt before sending to agent                                                |
| `--only`            |       | string |         | Only implement this component (e.g., backend, frontend, tests)                         |
| `--parallel`        |       | string |         | Run N agents in parallel, or comma-separated agent list (e.g., "3" or "claude,gemini") |
| `--library`         |       | bool   | false   | Include relevant library docs in prompts                                               |

## Examples

### Basic Implementation

```bash
mehr implement
```

Output:

```
Implementing task a1b2c3d4...
Reading 2 specifications...
Modified: src/api/handler.go
Created:  src/api/auth.go
Modified: src/api/routes.go
Implementation complete. 3 files changed.
```

### Dry Run

```bash
mehr implement --dry-run
```

Preview what would change without modifying files:

```
DRY RUN - No changes will be made
Would modify: src/api/handler.go
Would create: src/api/auth.go
Would modify: src/api/routes.go
```

### Verbose Output

```bash
mehr implement --verbose
```

See the agent's reasoning and code generation in real-time.

### Override Implementation Agent

```bash
mehr implement --agent-implement claude
```

Use a specific agent for code generation. See [AI Agents](/agents/index.md#per-step-agent-configuration).

### Optimize Prompt

```bash
mehr implement --optimize
```

Optimize the implementation prompt using an optimizer agent before sending to the working agent. This can improve clarity and effectiveness of the prompt, potentially leading to better code generation.

### Component Filtering

```bash
mehr implement --only tests
```

Implement only a specific component (e.g., tests, backend, frontend, api). This is useful when you've planned a full feature but want to iteratively implement parts of it. Specifications must have a `component` field in their frontmatter:

```yaml
---
component: tests
---
# Add unit tests for user authentication
```

### Parallel Execution

```bash
# Run 3 agents in parallel (same agent)
mehr implement --parallel 3

# Run specific agents in parallel
mehr implement --parallel claude,gemini
```

Run multiple agents simultaneously to implement specifications faster. Each agent receives the same specifications and produces independent results. This is useful for:
- Getting multiple approaches to the same problem
- Speeding up implementation on large codebases
- Cross-validation between different AI models

**Note:** Parallel execution creates an ad-hoc orchestration configuration without requiring YAML setup. For persistent orchestration, see [Multi-Agent Orchestration](/agents/orchestration.md).

## What Happens

1. **Validation**
   - Checks for existing specification files
   - Fails if no specifications found

2. **Context Preparation**
   - Reads all specification files
   - Includes notes from `notes.md`
   - Gathers relevant existing code

3. **Agent Execution**
   - Prompts agent with specifications and context
   - Agent generates code
   - Output parsed for file changes

4. **File Operations**
   - Creates new files
   - Modifies existing files
   - Reports all changes

5. **Checkpoint**
   - Git commit created
   - Added to undo stack

## File Changes

The agent outputs structured file changes:

```
<<FILE:src/api/handler.go>>
package api

func Handler(w http.ResponseWriter, r *http.Request) {
    // Implementation
}
<<END FILE>>
```

Mehrhof parses this and applies changes safely.

## Iterating

Implementation can be run multiple times:

```bash
mehr implement
mehr undo
mehr note "Use interfaces instead of concrete types"
mehr implement
```

## Error Handling

If implementation fails:

```
Error: Implementation failed
Reason: Agent timeout after 300 seconds
Tip: Increase agent.timeout in .mehrhof/config.yaml
```

No changes are applied on error. Your code remains unchanged.

## After Implementation

Review the changes:

```bash
git diff
```

Run tests:

```bash
make test
```

If satisfied:

```bash
mehr finish
```

If not:

```bash
mehr undo
```

## Implementing Review Fixes

After running `mehr review`, you can explicitly implement fixes from that review:

```bash
mehr implement review 1            # Implement fixes from review 1
mehr implement review 1 --dry-run  # Preview without applying
```

This tells the agent to focus specifically on fixing issues identified in that review,
rather than implementing specifications. Use this when review feedback needs to be addressed.

### Example Workflow

```bash
mehr implement          # Initial implementation
mehr review             # Review finds issues (creates REVIEW-1.txt)
mehr implement review 1 # Fix issues from review 1
mehr review             # Verify fixes (creates REVIEW-2.txt)
mehr finish             # Complete the task
```

## Web UI

Prefer a visual interface? See [Web UI: Implementing](/web-ui/implementing.md).

## See Also

- [plan](cli/plan.md) - Create specifications first
- [undo](cli/undo.md) - Revert changes
- [finish](cli/finish.md) - Complete the task
