<purpose>
Design a structured, wave-based implementation plan (`PLAN.md`) outlining tasks, dependency order, test cases, must-haves, and threat models for the target phase.
</purpose>

<required_reading>
@.opencode/references/hm-coordination-contracts.md
@.opencode/references/hm-gate-triad.md
</required_reading>

<downstream_awareness>
Outputs generated by this workflow are consumed by:
1. **hm-execute**: Directly executes tasks wave-by-wave, resolving the defined code changes and TDD checks.
2. **hm-verify**: References must-haves (truths, artifacts, key links) in the plan to perform verification.
</downstream_awareness>

<scope_guardrail>
**CRITICAL: Task alignment.** Every task in the plan must trace back to a requirement in `CONTEXT.md` or `SPEC.md`. Do not add tasks that are unrelated to the target phase goal.
</scope_guardrail>

<process>

<step name="initialize" priority="first">
Load phase details. Read `CONTEXT.md` (and `SPEC.md` / `RESEARCH.md` if present) to ingest locked decisions, requirements, and codebase scouts.
</step>

<step name="wave_partitioning">
Decompose the implementation into sequential, dependency-ordered waves.
- Wave 0: Foundations (types, interfaces, mock data, schema definitions).
- Wave 1+: Core features, sequential implementation of modules.
- Final Wave: Verification, wiring, and cleanup.
Enforce concurrency limits: **max 2 parallel tasks** in any execution wave.
</step>

<step name="define_tasks">
For each task:
- Detail exact file paths affected.
- Define expected behaviors and edge cases.
- Specify exact test files to run.
- Write falsifiable completion criteria.
</step>

<step name="must_haves_definition">
Define explicit phase must-haves in the plan frontmatter:
1. **truths**: Clear, falsifiable statements of expected behavior.
2. **artifacts**: File paths that must exist and be non-empty.
3. **key_links**: Logical relationships and pattern matches between files.
</step>

<step name="threat_model_mapping">
Construct the STRIDE threat register:
- Define trust boundaries.
- Catalog potential threats, category, component, and mitigation plan.
</step>

<step name="write_plan">
Write the completed plan to `${phase_dir}/${padded_phase}-PLAN.md` using the standard `hm-plan` template structure.
</step>

<step name="git_commit">
Stage and commit the generated `PLAN.md` file atomically to git.
</step>

<step name="update_state">
Record plan details in `.planning/STATE.md` and commit.
</step>

</process>

<success_criteria>
- Plan partitioned into sequential waves.
- No wave exceeds 2 parallel tasks.
- Must-haves (truths, artifacts, links) defined in frontmatter.
- STRIDE threat model mapped to components.
- Atomic commit succeeded.
</success_criteria>
