*** Settings ***
Documentation    End-to-end verification of M6 success criteria:
...              Firefox-scale autonomous porting via action creation,
...              plan use/execute/apply, hierarchical decomposition
...              (4+ levels of subplans), decision correction on
...              affected subtree only, parallel execution scaling
...              to 10+ concurrent subplans, and full lifecycle
...              completion of a realistic porting task.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment With Database Isolation
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_m6_e2e_verification.py

*** Test Cases ***
Porting Action Created From YAML Config
    [Documentation]    Create a porting action from YAML config via the
    ...                ``agents action create --config`` CLI command.
    ...                Verifies the lifecycle service is called with the
    ...                expected action attributes.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    action-create-porting    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-action-create-porting-ok

Plan Use And Execute On Large Project
    [Documentation]    Use a porting action on a large project and execute
    ...                the plan via CLI. Verifies both ``plan use`` and
    ...                ``plan execute`` commands invoke the lifecycle service.
    ...                Missing provider configuration is treated as controlled
    ...                non-crash output; tracebacks and unexpected internals fail.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-use-execute    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-plan-use-execute-ok

Hierarchical Decomposition Creates Four Plus Levels
    [Documentation]    Verify hierarchical decomposition creates 4+ levels
    ...                of subplans: root -> L1 (4 children) -> L2 (8) ->
    ...                L3 (4) -> L4 (4 leaves). Verifies identity flags
    ...                and root_plan_id propagation through all levels.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    hierarchical-decomposition    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-hierarchical-decomposition-ok

Decision Correction Recomputes Only Affected Subtree
    [Documentation]    Verify that decision correction targets only the
    ...                affected subtree. Creates a decision tree, targets
    ...                one child for correction, and verifies the
    ...                CorrectionImpact excludes the root and sibling
    ...                decisions. Tests rollback_tier and dry-run report.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    correction-affected-subtree    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-correction-affected-subtree-ok

Parallel Execution Scales To Ten Plus Concurrent Subplans
    [Documentation]    Verify parallel execution with SubplanConfig
    ...                in PARALLEL mode and max_parallel=15. Creates 15
    ...                concurrent SubplanStatuses and verifies the plan
    ...                model handles them correctly.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    parallel-execution-scale    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-parallel-execution-scale-ok

Realistic Porting Task Completes Autonomously
    [Documentation]    Simulate the full autonomous porting lifecycle:
    ...                ACTION -> STRATEGIZE -> EXECUTE -> APPLY with
    ...                10 subplans, verifying phase transitions,
    ...                subplan completion, and total files changed.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    porting-task-autonomous    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-porting-task-autonomous-ok

Plan Apply Via Lifecycle CLI
    [Documentation]    Verify ``agents plan apply`` transitions
    ...                the plan to APPLIED state via the lifecycle service.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-apply-lifecycle    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-plan-apply-lifecycle-ok

Subplan Failure Handler Retry And Stop Logic
    [Documentation]    Verify SubplanFailureHandler: fail_fast stops others,
    ...                SEQUENTIAL mode stops others, retriable errors are
    ...                retried, non-retriable errors are skipped, and max
    ...                retries exceeded prevents retry.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    failure-handler-logic    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-failure-handler-logic-ok

Subplan Config Supports All Execution Modes
    [Documentation]    Verify SubplanConfig accepts all execution modes
    ...                (SEQUENTIAL, PARALLEL, DEPENDENCY_ORDERED) and all
    ...                merge strategies. Tests bounds for max_parallel and
    ...                timeout configuration.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    subplan-config-modes    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-subplan-config-modes-ok

Decision Tree Structure For Porting Task
    [Documentation]    Verify decision tree with PROMPT_DEFINITION root and
    ...                SUBPLAN_PARALLEL_SPAWN children. Tests parent-child
    ...                links, downstream_plan_ids, and superseded_by for
    ...                the correction flow.
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decision-tree-porting    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m6-decision-tree-porting-ok
