<purpose>
Gather phase requirements, technical decisions, and scope boundaries through interactive user discussion, culminating in the lock of `CONTEXT.md` for downstream planning.
</purpose>

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

<downstream_awareness>
Outputs generated by this workflow are consumed by:
1. **hm-plan**: Reads `CONTEXT.md` to identify locked decisions and boundaries before writing the plan.
2. **hm-research**: Reads `CONTEXT.md` to target technical investigations based on user decisions.
</downstream_awareness>

<scope_guardrail>
**CRITICAL: No scope creep.** Discussion clarifies HOW to implement what is scoped in ROADMAP.md, never WHETHER to add new features. Any requests for new capabilities must be logged in "Deferred Ideas" and not incorporated into active requirements.
</scope_guardrail>

<process>

<step name="initialize" priority="first">
Verify phase number from arguments. Resolve directory path: `.planning/phases/{{PADDED_PHASE}}-{{SLUG}}/`. Check for existing `.continue-here.md` anti-pattern indicators and display them before continuing.
</step>

<step name="check_existing">
Check if `CONTEXT.md` already exists. If yes:
- Ask user whether to update, view, or skip.
- If `--auto` flag is passed, default to update.
Check if an interrupted discussion checkpoint `*-DISCUSS-CHECKPOINT.json` exists. If yes:
- Ask user whether to resume or start fresh.
- Parse checkpoint state on resume.
</step>

<step name="load_prior_context">
Read project-level records (`PROJECT.md`, `REQUIREMENTS.md`, `STATE.md`).
Read up to **3** previous phase `CONTEXT.md` files to carry forward established architectural patterns and user preferences.
</step>

<step name="scout_codebase">
Perform a lightweight scan of the target codebase directories to identify existing modules, files, and utilities that can be reused for the phase.
</step>

<step name="analyze_phase">
Identify phase-specific gray areas (technical trade-offs, architecture choices, library selections).
If `SPEC.md` exists, treat requirements as locked and focus questions entirely on implementation choices.
</step>

<step name="discuss_areas">
Interact with the user to resolve selected gray areas:
- Group related questions using `--batch` if requested.
- Present trade-off tables using `--analyze` if requested.
- Save progress incrementally into `*-DISCUSS-CHECKPOINT.json` after completing each gray area.
- Log user selections to discussion history.
</step>

<step name="write_context">
Generate files in the phase directory:
1. **CONTEXT.md**: Contains boundaries, locked decisions, canonical references, and codebase maps.
2. **DISCUSSION-LOG.md**: Human-readable history of the conversation options and answers.
Clean up the checkpoint file upon successful write.
</step>

<step name="git_commit">
Commit the generated `CONTEXT.md` and `DISCUSSION-LOG.md` files atomically to git.
</step>

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

</process>

<success_criteria>
- Prior context and codebase scouted.
- Gray areas resolved without scope creep.
- `CONTEXT.md` written matching default schema requirements.
- Session checkpoint cleaned up.
- Atomic commit succeeded.
</success_criteria>
