<!-- "Mastermind — Research ideas from a prompt, evaluate them with a PM lens, elaborate, and decompose into subtasks saved to docs/ideas/ (default) or monotask boards (--monotask flag)" -->

**First — extract repeat flags:** Follow the REPEAT PREAMBLE from `mastermind-repeat/SKILL.md`. Extracts `--repeat`, `--tillend`, `--maxruns`, `--wait`, `--rep`, `--loop` from `$ARGUMENTS` before all other parsing. If `is_continuation = true`, skip the empty-arguments check below.

If `$ARGUMENTS` is empty, output this and STOP:

> **Usage:** `/mastermind:ideate <prompt>`
>
> Example: `/mastermind:ideate add real-time collaboration to the editor`
>
> By default, ideas are saved to `docs/ideas/YYYY-MM-DD-<slug>.md`. Add `--monotask` to write to a monotask board instead.

Do NOT proceed further if no arguments were provided.

**Extract `--monotask` flag:** If present, set `USE_MONOTASK=true` and remove from `$ARGUMENTS`. Default: `USE_MONOTASK=false`.

---

## Step 0: Check monotask CLI *(skip unless `--monotask` was passed)*

**Only run this step if `USE_MONOTASK=true`.**

```bash
command -v monotask || (command -v cargo && cargo install monotask)
```
If neither exists, tell user to install Rust + monotask and STOP.

---

## Step 1: Gather Project Context

Collect ALL of the following in parallel (skip any that error):

1. **Repo name**: Run `git remote get-url origin`, extract the last path segment, strip `.git`. Fallback: `basename` of the current working directory. Store as `REPO_NAME`.

2. **README**: Read `README.md` (first 200 lines). Skip if missing.

3. **Package manifest**: Read whichever exists first: `package.json`, `Cargo.toml`, `pyproject.toml`, `go.mod`. Extract name, description, and keywords/tags.

4. **Knowledge graph**: Call `mcp__monomind__monograph_suggest` with the user's prompt (`$ARGUMENTS`). Skip if it errors or returns empty.

5. **Memory search**: Call `mcp__monomind__memory_pattern-search` with the user's prompt (`$ARGUMENTS`). Use the top 5 results.

Bundle all gathered information into a single `PROJECT_CONTEXT` string for downstream agents.

---

## Step 2: Setup Storage

**File mode (default):**
```bash
DATE=$(date +%Y-%m-%d)
SLUG=$(echo "$ARGUMENTS" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g; s/-\+/-/g; s/^-//; s/-$//' | cut -c1-40)
IDEA_FILE="docs/ideas/${DATE}-${SLUG}.md"
mkdir -p docs/ideas
```
Write the file header using the Write tool:
```markdown
---
source: mastermind:ideate
repo: <REPO_NAME>
created: <DATE>
prompt: <first 100 chars of $ARGUMENTS>
---

# Ideas: <$ARGUMENTS>

```
Store `IDEA_FILE`.

---

**Monotask mode:**
- Find or create space `$REPO_NAME` → store `SPACE_ID`
- Find or create `monomind-idea` board with columns: `New`, `Evaluated`, `Elaborated`, `Tasked`, `Iced`, `Rejected` → store `BOARD_ID` and all column IDs

---

## Step 3: Research Swarm -- Generate Ideas

Spawn 3-5 `researcher` agents in parallel using the Agent tool (mesh topology). Each agent receives:

- The user's prompt: `$ARGUMENTS`
- The `PROJECT_CONTEXT` string from Step 1

Each agent should generate ideas as a JSON array. There is no hard cap — generate as many or as few as the topic warrants. Ideas should span **two categories**:

1. **Feature/capability ideas** — what to build, what problem it solves, why it matters.
2. **Technical stack & baseline decisions** — foundational choices that must be made before implementation (e.g., "Use PostgreSQL vs SQLite for persistence", "Adopt event-driven architecture vs request-response", "Choose WebSocket vs SSE for real-time updates", "Define the API versioning strategy"). These are the "how" decisions that shape everything downstream.

```json
[
  {
    "title": "Short idea title",
    "description": "2-3 sentence description of the idea, what it solves, and why it matters.",
    "category": "feature | technical-baseline"
  }
]
```

After all agents complete:
1. Merge all idea arrays.
2. Deduplicate by title similarity (fuzzy match — drop ideas whose titles are >80% similar to an already-kept idea).
3. Persist each unique idea:

**File mode:** Append to `IDEA_FILE` using the idea section format from `mastermind-taskfile/SKILL.md`:
```markdown
### <Idea Title>
> status: new
> category: <feature|technical-baseline>

<description>

---
```

**Monotask mode:**
```bash
monotask card create $BOARD_ID $COL_NEW "<title>" --json
monotask card comment add $BOARD_ID $CARD_ID "<description>"
monotask card label add $BOARD_ID $CARD_ID "monomind-idea"
monotask card label add $BOARD_ID $CARD_ID "<category>"
```

If zero ideas were generated after deduplication, report "No ideas generated by the research swarm." and STOP.

---

## Step 4: Evaluator -- Assess Each Idea

Spawn a single `Product Manager` agent via the Agent tool. Provide it with:
- All ideas (titles + descriptions)
- The `PROJECT_CONTEXT`
- The user's original prompt: `$ARGUMENTS`

For EACH idea, the agent must return one of three verdicts, along with an **impact** score (0-10) and an **effort** score (0-10):

| Verdict | Action |
|---------|--------|
| **evaluated** | Idea is worth pursuing. Include a `skipElaboration` boolean: `true` if the idea is straightforward and needs no further research, `false` if edge cases should be explored. Include a 1-2 sentence value statement. Include `impact` (0-10, how much value it adds) and `effort` (0-10, how much work it requires). |
| **iced** | Idea might have potential but needs a question answered first. Include the blocking question. Include `impact` and `effort` estimates. |
| **rejected** | Idea is out of scope, infeasible, or low value. Include a 1-sentence reason. |

For each verdict, update the storage:

**File mode:** Use the Edit tool on `IDEA_FILE` to update the `> status:` line for the idea, then add the relevant fields. For `evaluated`: change to `> status: evaluated`, add `> impact: N`, `> effort: N`, `> skip_elaboration: true|false`, append `**Value:** <value statement>`. For `iced`: change to `> status: iced`, append `**Blocked by:** <question>`. For `rejected`: change to `> status: rejected`, append `**Rejected reason:** <reason>`.

**Monotask mode:**
- *evaluated:* `monotask card move $BOARD_ID $CARD_ID $COL_EVALUATED --json` + set impact/effort + add value comment
- *iced:* `monotask card move $BOARD_ID $CARD_ID $COL_ICED --json` + set impact/effort + add blocking question comment
- *rejected:* `monotask card move $BOARD_ID $CARD_ID $COL_REJECTED --json` + add reason comment

If ALL ideas were iced or rejected, output a summary table of verdicts and STOP.

---

## Step 5: Elaborator -- Add Edge Cases (conditional)

Check if ANY evaluated ideas have `skipElaboration: false`.

**If ALL evaluated ideas have `skipElaboration: true`:**
- *File mode:* Change each idea's `> status: evaluated` → `> status: elaborated` in `IDEA_FILE`
- *Monotask mode:* `monotask card move $BOARD_ID $CARD_ID $COL_ELABORATED --json`
- Skip spawning the elaborator agent entirely.

**Otherwise**, spawn two agents in parallel via the Agent tool:

1. A `feature-dev:code-explorer` agent — deep-dives into the existing codebase to trace execution paths, map dependencies, and surface internal constraints relevant to each idea.
2. A `researcher` agent (with WebSearch capability) — searches the internet for prior art, external edge cases, and potential technical pitfalls.

Provide both agents with:
- All evaluated ideas that have `skipElaboration: false`
- The `PROJECT_CONTEXT`

After both agents complete, merge their findings per idea. For each idea needing elaboration:
1. Research edge cases, prior art, codebase constraints, and technical pitfalls.
2. Persist findings:
   - *File mode:* Edit `IDEA_FILE` — append `**Edge cases:** <findings>` and `**Technical notes:** <notes>` under the idea section. If no blocking issues: change `> status: evaluated` → `> status: elaborated`. If blocking issue found: change to `> status: iced` and append `**Blocked by:** <issue>`.
   - *Monotask mode:* `monotask card comment add` for findings. Move to `Elaborated` or `Iced` accordingly.

Also mark `skipElaboration: true` ideas as `elaborated`.

---

## Step 6: Task Decomposer — Break Ideas into Subtasks

### Generate the TASKS Array

Spawn a single `Software Architect` agent via the Agent tool. Provide it with:
- All ideas in the `Elaborated` column (titles, descriptions, and all comments)
- The `PROJECT_CONTEXT`
- **The Task Grouping Rules and Card Format from `/mastermind:createtask` Step 5** — include them verbatim in the agent prompt so it produces correctly structured tasks

The agent MUST produce a `TASKS` array following the `/mastermind:createtask` task card format (Step 5). Each task must comply with all 7 grouping rules. For each elaborated idea, decompose into 2-6 subtasks.

**If the architect has doubts** about decomposing an idea (unclear scope, missing info), they should:
- Add a comment with the question
- Move the idea to `Iced` instead of `Tasked`

Store the result as `TASKS` array.

### Persist Task Decomposition

**File mode:** Follow `/mastermind:createtask` Steps 6–7 (file mode) to write a new task file. Use:
- `SOURCE_TAG` = `"mastermind:ideate"`
- `SOURCE_SUMMARY` = first 100 chars of `$ARGUMENTS`

After writing the task file, update `IDEA_FILE`:
- For each parent idea: change `> status: elaborated` → `> status: tasked`, append `**Subtasks file:** <TASK_FILE> (N tasks created)`

**Monotask mode:** Follow `/mastermind:createtask` Steps 6–7 (monotask mode) with:
- `SOURCE_TAG` = `"mastermind:ideate"`
- After cards created: `monotask card comment add $BOARD_ID $IDEA_CARD_ID "Subtasks created:\n- <title>..."` and `monotask card move $BOARD_ID $IDEA_CARD_ID $COL_TASKED --json`

Then follow `/mastermind:createtask` Step 8 (Final Dependency Review) and report results.

---

## Step 7: Final Summary

Output a summary table:

```
| # | Idea                        | Category           | Status   | Subtasks | Total Effort | Agents |
|---|-----------------------------|--------------------|----- ----|----------|--------------|--------|
| 1 | <title>                     | feature            | Tasked   | 4        | 18/40        | coder, backend-dev, tester |
| 2 | <title>                     | technical-baseline | Iced     | --       | --           | --     |
| 3 | <title>                     | feature            | Rejected | --       | --           | --     |
```

Then output:
- Total ideas generated: N
- Ideas tasked: N (with M total subtasks)
- Total effort points: X
- Ideas iced: N
- Ideas rejected: N
- *File mode:* Idea file: `<IDEA_FILE>` | Task file: `<TASK_FILE>`
- *Monotask mode:* Space: `$REPO_NAME` | Idea board: `monomind-idea` | Task board: `monomind-task`

---

## Step 8: Offer to Execute Tasks

If there are todo tasks:

*File mode:*
```
Skill("mastermind-do", "--file <TASK_FILE> --mode <parallel|minimal|sequential>")
```

*Monotask mode:*
```
Skill("mastermind-do", "--monotask --space $SPACE_ID --board $TASK_BOARD_ID --mode <parallel|minimal|sequential>")
```

To repeat this command on a schedule, wrap it with `/mastermind:repeat` or use `--repeat N` / `--tillend` directly.

Invoke `Skill("mastermind-repeat")` now to execute the REPEAT POSTAMBLE. This is a required tool call — do not skip it.
