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

{% block extra_constraints %}
        <rule priority="critical">Do NOT fix failures yourself.</rule>
        <rule priority="critical">Do NOT commit changes.</rule>
        <rule priority="high">Include verbatim failure output in current_failures.</rule>
{% endblock %}

{% block extra_context %}
        {% include '_project_discovery.xml.j2' %}
{% endblock %}

{% block instructions %}
        <phase name="get_bearings">
            <commands><![CDATA[
pwd
cat .claudesprint/project/current_issue.json
            ]]></commands>
        </phase>

        <phase name="discover_and_run_tests">
            <description>
                Examine the project to determine how to run tests, then run them.
            </description>
            <steps>
                <step>Read package.json, pyproject.toml, Makefile, or other relevant config files</step>
                <step>Identify the appropriate test command for this project</step>
                <step>Consider if there's a "validate" script that runs lint + typecheck + tests</step>
                <step>Run the test command and capture the output</step>
            </steps>
            <examples>
                <example project="typical Node.js">npm test</example>
                <example project="Node.js with validate script">npm run validate</example>
                <example project="NestJS">npm run test or npm run test:e2e</example>
                <example project="Python with pytest">pytest or uv run pytest</example>
                <example project="Rust">cargo test</example>
                <example project="Go">go test ./...</example>
                <example project="Project with Makefile">make test</example>
            </examples>
            <note>These are just examples - read the actual project files to determine the correct command.</note>
        </phase>

        <phase name="analyze_results" required="true">
            <description>CRITICAL: Output analysis BEFORE updating state:</description>
            <format><![CDATA[
<analysis>
Test Command Used: [the command you ran]
Test Result: [PASS | FAIL]

{% if failures %}
Failure Summary: [number of failures]
Error Messages: [key error messages]

Root Cause Assessment:
- [code bug indicators OR test bug indicators]

Verdict: [FAIL_CODE | FAIL_TEST]
Reasoning: [evidence for this verdict]
{% endif %}

Next Step: [browser-validation | implement | fix-tests]
</analysis>
            ]]></format>
            <enforcement>Do NOT update state until analysis is output.</enforcement>
        </phase>

        <phase name="update_current_issue_json_pass" condition="tests pass">
            <update field="step">browser-validation</update>
            <update field="current_failures">Clear</update>
            <update field="retry_count">Reset to 0</update>
        </phase>

        <phase name="update_current_issue_json_fail" condition="tests fail">
            <description>Determine cause of failure:</description>
            <cause name="code_bug" route_to="implement">
                <indicator>Test expectation correct but code produces wrong result</indicator>
                <indicator>Runtime errors in application code</indicator>
                <indicator>Missing implementation</indicator>
            </cause>
            <cause name="test_bug" route_to="fix-tests">
                <indicator>Test has wrong expectation</indicator>
                <indicator>Test setup broken</indicator>
                <indicator>Test flaky/non-deterministic</indicator>
            </cause>
            <default>When uncertain, default to implement.</default>
            <updates>
                <update field="step">implement or fix-tests (based on cause)</update>
                <update field="current_failures">Verbatim output (truncated ~200 lines)</update>
                <update field="retry_count">Increment</update>
            </updates>
            <note>If retry_count > 5, add: "Multiple retries failed - may need human intervention"</note>
        </phase>

        <phase name="log_and_exit">
            <commands><![CDATA[
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] STEP: run-tests -> <next> (<PASS/FAIL>)" >> .claudesprint/project/current_issue.log
echo "  Test command: <command_used>" >> .claudesprint/project/current_issue.log
echo "  Analysis: <why routing to implement or fix-tests>" >> .claudesprint/project/current_issue.log
            ]]></commands>
        </phase>
{% endblock %}

{% block termination %}
        <required>true</required>
        <description>End your response with exactly one of these routing signals:</description>
        <signal value="pass">All tests passed</signal>
        <signal value="fail_code">Tests fail due to code bug</signal>
        <signal value="fail_test">Tests fail due to test bug</signal>
        <format><![CDATA[<routing_signal>value</routing_signal>]]></format>
{% endblock %}
