---
description: Research with task constraints then implement
agent: build
---

# Research & Implement: $ARGUMENTS

## For Simple Edits (No Task Needed)

**Proceed directly if:**

- Single file change
- Bug fix or small enhancement
- Add clear comments explaining the change

**Example:**

```typescript
// Fix: Handle null case in error handler (PR #123)
if (!value) {
  return null;
}
```

Then commit with: `git commit -m "fix: clear description of the fix"`

## For Structured Work (Task File Required)

**Use task files if:**

- Multi-file changes
- Research + implementation combined
- Complex decision-making needed
- New dependencies or architecture

Then follow gates below.

---

## ANTI-HALLUCINATION: MANDATORY GATE (for task-based work)

**ABORT IF ANY FAILED:**

1. **Task exists in Backlog MCP:**

   ```typescript
   backlog_task_view({ id: "$ARGUMENTS" });
   ```

   If task not found: STOP. Create with `/task-with-spec $ARGUMENTS` first.

2. **Constraint files exist:**
   - Does `@backlog/tasks/$ARGUMENTS/prd.md` exist with "Success Criteria" section?
   - Does `@backlog/tasks/$ARGUMENTS/spec.md` exist with "Constraints" and "Edge Cases"?
3. **Files are complete:**
   - Is PRD goal measurable (not vague)?
   - Does spec have architecture plan?

**IF NO TO ANY:** STOP. User must complete task setup first.

## Step 1: Validate & Load Task

Read `@backlog/tasks/$ARGUMENTS/prd.md` and `@backlog/tasks/$ARGUMENTS/spec.md`. Extract:

- **PRD Goal:** What is the task trying to achieve?
- **PRD Scope (In/Out):** What's explicitly in-scope? What's not?
- **PRD Success Criteria:** What checkboxes must be [x] marked at end?
- **Spec Constraints:** What rules must be followed?
- **Spec Edge Cases:** What corner cases must be handled?
- **Spec Architecture:** What's the implementation plan?

**Abort if any section is missing or incomplete.**

## Step 2: Research Within Scope

**ONLY research what the task requires.** Use these tools in order:

1. **Internal Patterns:** Search codebase for existing patterns matching PRD goal
2. **Framework Docs:** Get docs for libraries/frameworks listed in spec
3. **Real-world Examples:** Find implementation examples from GitHub/Exa
4. **Task Precedent:** Check memory for previous research on same topic

**CONSTRAINT:** Stop researching if any finding suggests off-scope work needed.

## Step 3: Create Implementation Plan

Based on spec architecture, create step-by-step plan:

- **Phase 1:** [Setup - what files/structure to create]
- **Phase 2:** [Core Logic - main implementation]
- **Phase 3:** [Integration - how to connect to existing code]
- **Phase 4:** [Testing - how to verify success criteria]

**VALIDATE:** Does plan require ONLY in-scope changes per PRD?

## Step 4: Implement Within Constraints

Execute plan with continuous validation:

```
BEFORE EACH CODE CHANGE:
- Is this change in PRD scope?
- Does it follow spec constraints?
- Does it handle a spec edge case or implement core logic?

IF NO TO ANY QUESTION: STOP and report constraint violation
```

**IMPLEMENT:** Write only the code needed for this task. No extra features, no refactoring outside scope.

## Step 5: Complete Review.md

After implementation, fill `@backlog/tasks/$ARGUMENTS/review.md`:

- **What Changed:** List every file modified, every function added/changed
- **What Skipped:** What was considered but NOT done (and why)
- **Inconsistencies:** Any deviations from spec, any unexpected challenges
- **Validation:** Which success criteria are now [x] checked?

**ABORT:** If review.md incomplete, task cannot be marked done.

## Step 6: Mark Task Complete

Update task status in Backlog MCP and run validation:

```typescript
backlog_task_edit({
  id: "$ARGUMENTS",
  status: "Done",
});
```

Then run `/complete-task $ARGUMENTS` which validates:

- review.md is complete
- All success criteria are [x] marked
- No uncommitted changes remain

**ABORT:** If any validation fails, fix then retry.

---

**ANTI-HALLUCINATION: Simple edits proceed with comments; complex work stays within task boundaries**
