# Autonomous AI Agent Operational Guidelines (Agile Monster)

This document provides mandatory operational protocols for AI coding assistants (**Cursor, Claude Code, Antigravity, Codex, Hermes Agent, Windsurf, Aider**) working inside repositories integrated with **Agile Monster** ([https://app.agilemonster.com](https://app.agilemonster.com)).

---

## 🧭 Core Principles

1. **The Agile Monster Board is the Source of Truth**: 
   All non-trivial feature additions, bug fixes, refactors, and deployments must correspond to a task on the project's Agile Monster board.
2. **One Active Assignee**: 
   Work on cards assigned specifically to your agent provider (`cursor`, `claude_code`, `antigravity`, `codex`, `hermes`, `windsurf`, `aider`, or `custom`). Never claim or alter tasks assigned to other agents or human teammates without explicit user direction.
3. **No Hallucinated Requirements**: 
   When a specification, API schema, or environment variable is ambiguous, stop and call `agilemonster_ask_question` or `agilemonster_block_task` rather than guessing.
4. **Dogfooding & Continuous App Improvement**:
   We use Agile Monster to build and improve Agile Monster. Any friction, UI bug, confusing error, or missing feature discovered while dogfooding the app must be recorded in `MEMORY.md` and tracked on the board.
5. **Testing & Deployment Gates**: 
   Autonomous agents must move code to **Testing / Review** first (`agilemonster_submit_for_review`). Once verified and passing, advance to **Done** (`agilemonster_complete_task`), logging explicit deployment status in the completion comment (Automated CI/CD run ID/commit or User-Controlled manual trigger instructions).
6. **Universal Plan Synchronization**:
   Whenever any plan attribute (pricing, limits like boards per workspace, workspaces, members, storage, API limits, or feature entitlements) changes, it MUST be synchronized across all 5 system layers in the same task/commit:
   - Backend logic & limits (`backend-php/src/Services/PlanGate.php` and `PlanCatalog.php`)
   - Public pricing page (`frontend-nextjs/src/lib/plan-catalog.ts` and `pricing/page.tsx`)
   - Registration & sign-up (`frontend-nextjs/src/app/register/page.tsx`)
   - In-app billing UI (`frontend-flutter/lib/screens/billing_screen.dart` and `models/subscription.dart`)
   - Payment gateways & app stores (Stripe, Apple In-App Purchase, Google Play Billing, Amazon Appstore).
   Refer to `.agents/rules/plan-synchronization.md` for the full protocol.

---

## 🔄 Task Lifecycle Protocol

Follow this exact sequential protocol when working on any task:

```
┌────────────────────────────────────────────────────────┐
│ 1. Context & Discovery  ──> agilemonster_get_context   │
└───────────┬────────────────────────────────────────────┘
            ▼
┌────────────────────────────────────────────────────────┐
│ 2. Intake & Queue       ──> agilemonster_list_my_tasks │
└───────────┬────────────────────────────────────────────┘
            │
            ▼ Is requirement clear & unblocked?
           / \
     NO   /   \  YES
    ┌────       ────┐
    ▼               ▼
┌──────────────────────────┐  ┌──────────────────────────┐
│ Clarify / Block          │  │ 3. Claim Task            │
│ agilemonster_ask_question│  │ agilemonster_claim_task  │
│ agilemonster_block_task  │  └───────────┬──────────────┘
│ (Status: waiting_for_    │              ▼
│  input, reassigns human) │  ┌──────────────────────────┐
└───────────┬──────────────┘  │ 4. Implementation        │
            │                 │ agilemonster_add_progress│
      Human answers           └───────────┬──────────────┘
            └─────────────────────────────┤
                                          ▼
                              ┌──────────────────────────┐
                              │ 5. Testing & Review      │
                              │ agilemonster_submit_for_ │
                              │ review (moves to Testing)│
                              └───────────┬──────────────┘
                                          ▼ Testing passes
                              ┌──────────────────────────┐
                              │ 6. Complete & Deploy Gate│
                              │ agilemonster_complete_   │
                              │ task (moves to Done)     │
                              │ Comment: Auto-Deploy vs. │
                              │ User-Controlled CI/CD    │
                              └───────────┬──────────────┘
                                          ▼
                              ┌──────────────────────────┐
                              │ 7. Follow-up Tasks       │
                              │ agilemonster_create_     │
                              │ followup_tasks           │
                              └──────────────────────────┘
```

---

### Step 1: Context & Discovery
Before writing code or querying the board:
- Inspect `.agilemonster.json` in the project root if present.
- If not present or if you need current board metadata, invoke the tool:
  ```json
  agilemonster_get_context({})
  ```
- This retrieves the active board ID, list columns (`Backlog`, `In Progress`, `Review`, `Done`), and currently active agent tasks.

### Step 2: Task Intake
Check which tasks are queued for your agent:
```json
agilemonster_list_my_tasks({ "status": "queued" })
```
- If the user specifies a particular task or bug, search for it using `agilemonster_get_context` or `search_cards`.
- Confirm the card is assigned to your provider before proceeding.

### Step 3: Claiming the Task
Before editing files or running build tools, claim the task to notify team members that work is actively underway:
```json
agilemonster_claim_task({ "card_id": "<card_uuid>" })
```
This automatically updates the card status to `in_progress` and logs an activity event.

### Step 4: Implementation, Questions & Progress
- **When Blocked or Unclear**:
  If acceptance criteria are missing or an architecture choice requires confirmation:
  ```json
  agilemonster_ask_question({
    "card_id": "<card_uuid>",
    "question": "Should the authentication token be stored in SecureStorage or SharedPreferences?"
  })
  ```
  *This moves the task to `waiting_for_input` and alerts the user.*
- **Logging Progress**:
  For multi-step or long-running work, communicate progress to the board:
  ```json
  agilemonster_add_progress({
    "card_id": "<card_uuid>",
    "update": "Database schema migration executed successfully. Now implementing REST controller endpoints."
  })
  ```

### Step 5: Submission for Human Review & Testing
When code is written, compiled, and tested locally:
1. Move the card to **Testing / Review** first.
2. Submit the card for human sign-off:
   ```json
   agilemonster_submit_for_review({
     "card_id": "<card_uuid>",
     "summary": "Implemented refresh token rotation and added tests in test/auth_test.dart.",
     "verification_steps": "1. Run flutter test\n2. Log in with test account\n3. Wait 15 minutes or trigger token expiration in DevTools\n4. Confirm session refreshes silently without logging out."
   })
   ```
This automatically:
- Moves the card into the board's **Testing / QA / Review** column (e.g. `Testing`, `In Review`, `QA`).
- Reassigns the card back to the human who created or requested it (`assignee_kind: "member"` and adds them to `card_members`).
- Updates `agent_status` to `review`.
- Posts a formatted comment with your exact testing & verification instructions for the human tester.

### Step 6: Completion & Deployment Gate
When testing passes and the human confirms (or automated testing pipeline verifies):
Advance the task to **Done**:
```json
agilemonster_complete_task({
  "card_id": "<card_uuid>",
  "comment": "<DEPLOYMENT_NOTE>"
})
```
#### Documenting the Deployment in Card Comments:
- **If Automated CI/CD Deployments are Active** (e.g. push to `main` auto-deploys via GitHub Actions):
  Make a note in the completion comment detailing the deployment:
  - Commit hash & branch
  - GitHub Actions Workflow Run ID (e.g., `Deploy to cPanel #42142786411`)
  - Live production URLs (`https://agilemonster.com`, `https://app.agilemonster.com`, `https://api.agilemonster.com`)
  - Summary of verified features
- **If User-Controlled / Manual CI/CD is Required**:
  Make a note on the completion comment detailing exact trigger instructions:
  - "Verified & ready for deployment (User-controlled / Manual CI/CD)"
  - Specific PR to merge or CI/CD workflow command to trigger (e.g. `gh workflow run deploy-cpanel.yml`).

### Step 7: Suggest Follow-Up Tasks
After submitting the review or completing the task, anticipate operational and pipeline needs by suggesting follow-up cards:
```json
agilemonster_create_followup_tasks({
  "card_id": "<card_uuid>",
  "tasks": [
    {
      "title": "Deploy update to Test / Staging environment",
      "description": "Trigger automated CI/CD pipeline or push release build for QA team."
    },
    {
      "title": "Deploy to Production",
      "description": "Perform production release once QA signs off on staging build."
    }
  ]
})
```

---

## 🛠️ MCP Tool Quick Reference

| MCP Tool Name | Purpose | When to Call |
| :--- | :--- | :--- |
| `agilemonster_get_context` | Inspect board ID, lists, and active tasks | Start of session or when switching tasks |
| `agilemonster_init_project` | Generate `.agilemonster.json` repository binding | Setup / initialization |
| `agilemonster_list_my_tasks` | Query assigned tasks (`queued`, `in_progress`, `review`) | Finding assigned work |
| `agilemonster_claim_task` | Mark task `in_progress` | Immediately before starting code edits |
| `agilemonster_ask_question` | Pause with `waiting_for_input` and post question | When blocked, ambiguous, or needing approval |
| `agilemonster_block_task` | Mark task `blocked` with explanation | When blocked by upstream dependencies |
| `agilemonster_add_progress` | Add implementation progress note | After completing significant milestones |
| `agilemonster_submit_for_review` | Move to Review and reassign to human with test steps | When local development is complete |
| `agilemonster_complete_task` | Move task to Done with deployment note | When testing and verification pass |
| `agilemonster_create_followup_tasks`| Create follow-up tasks linked to parent | Immediately after submitting review |
| `agilemonster_record_spend` | Track LLM token and cost consumption | During or after model inference loops |
| `agilemonster_set_budget` | Configure task spend & token circuit breaker limits | Start of task to avoid infinite billing loops |

---

## 🤖 Headless Autonomous Worker Daemon (`agilemonster-worker`)

For continuous autonomous background development without active developer terminal sessions:
```bash
# Run worker in background daemon mode
npx agilemonster-worker --runner=simulated --interval=15 --max-budget=5.00

# Or execute with headless Aider or Claude Code CLI
npx agilemonster-worker --runner=claude --interval=30
```
The autonomous worker:
1. Automatically polls and claims tasks assigned to your agent provider.
2. Checks out an isolated task branch (`agilemonster/task-<card_id>`).
3. Sends continuous heartbeat progress updates to Agile Monster.
4. Enforces spend/token circuit-breaker safeguards.
5. Automatically submits completed work for human review (never marks `Done` directly).

---

## 💬 Slack HITL & GitHub 2-Way Synchronization

- **Interactive Slack Approval**: When agents submit work for review, a rich Block Kit notification is dispatched to Slack with `[Approve & Complete]` and `[Request Changes]` interactive buttons for team members.
- **GitHub PR & Issue Sync**: Merging a linked Pull Request (`agilemonster/task-<id>` or `[AM-<id>]`) automatically advances the card to the board's `Done` list and awards team member points.

