*** Settings ***
Documentation    End-to-end verification of M3 (v3.2.0) acceptance criteria:
...              decision tree recording, context snapshots,
...              decision tree persistence and rendering,
...              invariant enforcement during strategize,
...              dry-run correction via impact analysis,
...              and live revert correction re-execution.
...
...              This suite is the final gate before closing milestone v3.2.0.
...              All acceptance criteria from the v3.2.0 milestone description
...              must pass before the milestone can be closed.
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment
Force Tags       m3    acceptance_gate    v3.2.0

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

*** Test Cases ***
Plan Execution Generates Decisions During Strategize
    [Documentation]    Execute ``agents plan use`` and ``agents plan execute``
    ...                through the CLI command path, then verify decisions
    ...                were recorded during Strategize before execution.
    ...
    ...                Validates: plan use + plan execute generate decisions
    ...                during Strategize phase.
    [Tags]    success_criteria    decision_recording    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-generates-decisions    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-plan-generates-decisions-ok

Decision Tree View Via Plan Tree
    [Documentation]    Invoke ``agents plan tree <plan_id>`` and validate
    ...                nested JSON output for root/child/grandchild
    ...                relationships from the rendered tree.
    ...
    ...                Validates: plan tree displays the decision tree correctly.
    [Tags]    success_criteria    decision_tree    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decision-tree-view    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-decision-tree-view-ok

Decision Explain Shows Full Context
    [Documentation]    Invoke ``agents plan explain <decision_id>`` with
    ...                ``--show-context`` and ``--show-reasoning`` and
    ...                verify full decision context fields are present.
    ...
    ...                Validates: plan explain shows full decision context.
    [Tags]    success_criteria    decision_explain    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decision-explain    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-decision-explain-ok

Invariant Add And List Via CLI And Service
    [Documentation]    Validate project-scoped invariant commands:
    ...                ``agents invariant add --project local/large-project``
    ...                and ``agents invariant list --project local/large-project``.
    ...
    ...                Validates: invariant add and invariant list CLI commands.
    [Tags]    success_criteria    invariant_management    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    invariant-add-list    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-invariant-add-list-ok

Correction Dry Run Via Plan Correct
    [Documentation]    Perform a dry-run correction and verify impact
    ...                analysis output. Tests both the CorrectionService
    ...                directly and the ``agents plan correct --dry-run``
    ...                CLI command with mocked services.
    ...
    ...                Validates: plan correct with --dry-run performs
    ...                impact analysis without modifying state.
    [Tags]    success_criteria    correction_dry_run    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    correction-dry-run    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-correction-dry-run-ok

Correction Live Revert Executes And Re-Creates Decisions
    [Documentation]    Execute ``agents plan correct --mode revert``
    ...                through the CLI path (with ``--yes``) and verify
    ...                reverted decisions and applied status in output.
    ...
    ...                Validates: plan correct with --mode revert executes
    ...                live correction.
    [Tags]    success_criteria    correction_live_revert    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    correction-live-revert    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-correction-live-revert-ok

Decisions Recorded With Full Context Snapshot
    [Documentation]    Verify every decision in the tree has a complete
    ...                context snapshot and that model_dump / model_validate
    ...                round-trips preserve all snapshot fields.
    ...
    ...                Technical criterion: decisions recorded during
    ...                Strategize with full context snapshot.
    [Tags]    technical_criteria    context_snapshot
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decisions-context-snapshot    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-decisions-context-snapshot-ok

Decision Tree Persists To Database And Renders
    [Documentation]    Persist decisions through ``DecisionService`` with
    ...                ``UnitOfWork`` backing, then verify round-trip
    ...                retrieval and ``agents plan tree`` rendering.
    ...
    ...                Technical criterion: decision tree persists to
    ...                database and renders correctly.
    [Tags]    technical_criteria    persistence    tdd_issue    tdd_issue_4299    tdd_expected_fail
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    decision-tree-persistence    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-decision-tree-persistence-ok

Correction Revert Re-Executes From Decision Point
    [Documentation]    Verify that a revert correction invalidates the
    ...                target subtree (child + grandchild) and allows
    ...                new decisions to be spawned from the corrected
    ...                point, forming a new valid subtree.
    ...
    ...                Technical criterion: correction in revert mode
    ...                re-executes from decision point.
    [Tags]    technical_criteria    correction_reexecution
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    correction-revert-reexecutes    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-correction-revert-reexecutes-ok

Invariants Enforced During Strategize
    [Documentation]    Verify invariants at global, project, and plan
    ...                scopes are merged with correct precedence,
    ...                de-duplicated case-insensitively, and enforcement
    ...                records are created with decision IDs. Also
    ...                verifies InvariantSet.merge produces correct output.
    ...
    ...                Technical criterion: invariants are enforced
    ...                during strategize.
    [Tags]    technical_criteria    invariant_enforcement
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    invariants-enforced-strategize    cwd=${WORKSPACE}    timeout=300s    on_timeout=kill
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m3-invariants-enforced-strategize-ok
