# `CMDC.Plugin.Builtin.Planning`
[🔗](https://github.com/tupleyun/cmdc/blob/v0.5.3/lib/cmdc/plugin/builtin/planning.ex#L1)

P2 规划插件 — 在用户 prompt 提交前强制 LLM 先生成结构化 Plan。

实现「先规划后执行」模式，核心能力：

1. **plan_first**：`:before_prompt` hook 返回 `{:intervene, directive, state}`，
   让 LLM 在执行任务前先产出 markdown checklist
2. **自动解析**：`:after_response` hook 尝试用 `CMDC.Plan.from_markdown/2`
   把 LLM 回复解析成 `CMDC.Plan` 结构
3. **持续感知**：成功解析后通过 `{:update_system_context, :plan, section}`
   emit，把 `Plan.to_prompt_section/1` 注入到 `state.dynamic_context_sections`，
   后续每轮 LLM 调用 system prompt 都会包含 plan 的当前进度
4. **事件广播**：emit `{:plan_generated, plan}` 供外部监控/UI 订阅

## 配置

    {CMDC.Plugin.Builtin.Planning,
      plan_first: true,                 # 是否在 prompt 前强制生成 plan
      min_prompt_length: 20,            # prompt 长度阈值（太短不触发）
      directive: "请先把任务拆成 markdown checklist...",  # 可选，覆盖默认指令
      max_plan_attempts: 2              # 解析失败最多尝试次数
    }

## 触发 Hook

- `:before_prompt` — 用户 prompt 提交前，注入规划指令
- `:after_response` — 收到 LLM 回复，解析为 Plan

## 与 Reflection 插件的配合

典型组合：先 Planning（priority 200）→ 执行 →  Reflection（priority 400）
评审，整体形成 ADP 推荐的 **Plan → Execute → Reflect → Replan** 闭环。

---

*Consult [api-reference.md](api-reference.md) for complete listing*
