{% extends '_base.xml.j2' %}

{% block extra_constraints %}
        <rule priority="critical">Do NOT commit changes.</rule>
        <rule priority="critical">Do NOT run tests.</rule>
        <rule priority="critical">Do NOT mark issue complete.</rule>
        <rule priority="high">Leave working tree dirty.</rule>
        <rule priority="high">Make minimal changes for acceptance criteria only.</rule>
{% endblock %}

{% block instructions %}
        <phase name="analyze_first" required="true">
            <description>BEFORE any implementation, output your analysis:</description>
            <format><![CDATA[
<analysis>
Issue: [issue_id] - [title]

Acceptance Criteria:
- [AC1]
- [AC2]

Current State:
- [what exists]
- [what's missing]

Approach:
- [implementation strategy]
- [files to modify]
</analysis>
            ]]></format>
            <enforcement>Do NOT write any code until analysis is complete.</enforcement>
        </phase>

        <phase name="get_bearings">
            <commands><![CDATA[
pwd
cat .claudesprint/project/current_issue.json
ISSUE_ID=$(cat .claudesprint/project/current_issue.json | jq -r '.issue_id')
claudesprint-tools sprint details "$ISSUE_ID"
git log --oneline -5 2>/dev/null || echo "Not a git repo"
            ]]></commands>
            <extract>issue_id, issue_title, context.acceptance_criteria, current_failures</extract>
            <validation>If issue_id is empty, report the issue and exit.</validation>
        </phase>

        <phase name="implement">
            <guidelines>
                <guideline>Check existing code first - don't assume something isn't implemented</guideline>
                <guideline>Make minimal changes for acceptance criteria only</guideline>
                <guideline>Follow existing patterns in codebase</guideline>
                <guideline>Do NOT run tests yet</guideline>
            </guidelines>
        </phase>

        <phase name="fix_mode" condition="current_failures non-empty">
            <description>If routed here from run-tests, fix-tests, or browser-validation:</description>
            <tasks>
                <task>Read current_failures for specific issue to fix</task>
                <task>Focus on fixing that issue first</task>
                <task>Log fix approach to current_issue.log</task>
            </tasks>
        </phase>

        <phase name="update_state">
            <description>Update state via CLI tools:</description>
            <commands><![CDATA[
# Record all changed files
claudesprint-tools issue change "<file1>" "<summary1>"
claudesprint-tools issue change "<file2>" "<summary2>"
            ]]></commands>
            <note>Do NOT set the next step manually. Step transitions are handled automatically by the workflow engine based on routing rules and skip gates.</note>
        </phase>

        <phase name="log_and_exit">
            <commands><![CDATA[
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] STEP: implement -> write-tests" >> .claudesprint/project/current_issue.log
echo "  Changes: <files modified>" >> .claudesprint/project/current_issue.log
echo "  Decision: <key implementation decisions>" >> .claudesprint/project/current_issue.log
            ]]></commands>
        </phase>
{% endblock %}

{% block termination %}
        <note>No termination tag required for this step.</note>
{% endblock %}
